Robbo_UK
Robbo_UK

Reputation: 12149

intellij incorrectly saying no beans of type found for autowired repository

I have created a simple unit test but IntelliJ is incorrectly highlighting it red. marking it as an error

No beans?

enter image description here

As you can see below it passes the test? So it must be Autowired?

enter image description here

Upvotes: 286

Views: 679920

Answers (30)

boardtc
boardtc

Reputation: 1548

I had this exact error, however, it was not a test class, and it was because there was no constructor with constructor injection for the bean.

If you are using Lombok, you can use @RequiredArgsConstructor on the class which generates a constructor with 1 parameter for each field that requires special handling.

Upvotes: 0

Alex A
Alex A

Reputation: 63

IntelliJ Idea 2021.3.2 (Ultimate Edition)

I have such a service

import org.springframework.boot.info.BuildProperties;

@Service
public class MyVeryBestService {

    private final BuildProperties buildProperties;

    @Autowired
    public MyVeryBestService(BuildProperties buildProperties) {
        this.buildProperties = buildProperties;
    }
    //some other code
}

Application failed to start with this error

Parameter 0 of constructor in MyVeryBestService required a bean of type 'org.springframework.boot.info.BuildProperties' that could not be found

I execute maven goal clean and go to File -> Repair IDE... and it helps. I've tried Invalidate Caches... but it doesn't help.

Upvotes: 1

FiruzzZ
FiruzzZ

Reputation: 826

This and another kind of "Could not autowire. No Beans of ..." can be solved by installing the plugin, in my case was JobBuilderFactory from Spring Batch.

enter image description here

Upvotes: 5

Sandy Samir
Sandy Samir

Reputation: 1

i get the same error while i was using my first springbootapp but I solved with :

  1. @Configuration @EnableAutoConfiguration in java class that I used to call the implemented class 2.@Repository in the interface

but if I removed it I got the same error again

Upvotes: 0

anand krish
anand krish

Reputation: 4415

I got this error out of Repository layer. I missed a simple @Component for the class. This could be a problem. Add this annotation @Component on top of the corresponding Implementation class

Upvotes: 3

elmrabti
elmrabti

Reputation: 31

It may happen because you didn't add spring web dependency so the IDE considers your application as Spring application not Spring boot

Upvotes: 1

Tohid Makari
Tohid Makari

Reputation: 2494

Try to use @ComponentScan with basePackages attribute to scan related bean like this :

@ComponentScan(basePackages={"com.xxxx.yyyy.reportdistributor"})

Upvotes: 0

mateo cunha
mateo cunha

Reputation: 67

Yes I know, seven year later but this can help to others. This is my Test class

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public abstract class ApiTest

Everything's fine. but the problem was my folders, for example:

If you have the next structure -> main.java.com.api.service

So it's the same for the test -> test.java.com.api.service

Upvotes: 0

masudul alam
masudul alam

Reputation: 1

@ComponentScan(basePackages = "com.x.y") in your desired place. That's it.

Upvotes: 0

Alexander Borisov
Alexander Borisov

Reputation: 1199

Looks like you are trying to run your tests without actually activating your WebEnvironment.

The solution is quite simple, just add @SpringBootTest annotation with a required config.

Here is an example:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) // Run the server for testing.
public class mockTest {

    @Autowired
    // Should work now
}

Upvotes: 1

aylingumus
aylingumus

Reputation: 389

I have updated my Intellij IDEA version to 2022.2.3 like below:

IntelliJ IDEA 2022.2.3 (Ultimate Edition)

Build #IU-222.4345.14

Runtime version: 17.0.4.1+7-b469.62 aarch64

It solved my problem.

Upvotes: 0

Andrew
Andrew

Reputation: 876

@Autowired(required = false) will shut intellij up

Upvotes: 0

David Lee
David Lee

Reputation: 36

As for intelliJ version 2021.2.3 (Ultimate Edition) I changed "incorrect injection point autowiring in spring bean compontents to warning which works for meenter image description here

Upvotes: 1

mkczyk
mkczyk

Reputation: 2710

Add Spring annotation @Repository over the repository class.

I know it should work without this annotation. But if you add this, IntelliJ will not show error.

@Repository
public interface YourRepository ...
...

If you use Spring Data with extending Repository class it will be conflict packages. Then you must indicate packages directly.

import org.springframework.data.repository.Repository;
...

@org.springframework.stereotype.Repository
public interface YourRepository extends Repository<YourClass, Long> {
    ...
}

And next you can autowired your repository without errors.

@Autowired
YourRepository yourRepository;

It probably is not a good solution (I guess you are trying to register repository twice). But work for me and don't show errors.

Maybe in the new version of IntelliJ can be fixed: https://youtrack.jetbrains.com/issue/IDEA-137023

Upvotes: 103

cng.buff
cng.buff

Reputation: 583

I removed this code. no more errors

enter image description here


Other way to check: add the annotation @Service to your Repository class and it should work.

Upvotes: 1

Meshu Deb Nath
Meshu Deb Nath

Reputation: 141

I was having the issue. Just use

@SpringBootTest

@AutoConfigureMockMvc

annotation with the test class.

Upvotes: 1

Ron Hiner
Ron Hiner

Reputation: 11

I was having the same problem and solved it using "Invalidate Caches..." under the File menu.

Upvotes: 1

davidmpaz
davidmpaz

Reputation: 1387

As most synchronisation errors between IntelliJ (IDE) and development environments.

Specially if you have automated tests or build that pass green all the way through.

Invalidate Cache and Restart solved my problem.

Upvotes: 4

Shanilka Fernandopulle
Shanilka Fernandopulle

Reputation: 141

Have you checked that you have used @Service annotation on top of your service implementation? It worked for me.

import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserServices {}

Upvotes: 13

chris dany
chris dany

Reputation: 450

add the annotation @Service to your Repository class and it should work.

Upvotes: 1

duyuanchao
duyuanchao

Reputation: 4313

just add below two annotations to your POJO.

@ComponentScan
@Configuration
public class YourClass {
    //TODO
}

Upvotes: 4

Er.Rk Yadav
Er.Rk Yadav

Reputation: 91

simple you have to do 2 steps

  1. add hibernate-core dependency
  2. change @Autowired to @Resource.
==>> change @Autowired to  @Resource

Upvotes: 8

Ajeet Yadav
Ajeet Yadav

Reputation: 131

Use @EnableAutoConfiguration annotation with @Component at class level. It will resolve this problem.

For example:

@Component
@EnableAutoConfiguration  
public class ItemDataInitializer  {

    @Autowired
    private ItemReactiveRepository itemReactiveRepository;

    @Autowired
    private MongoOperations mongoOperations;
}

Upvotes: 13

Master mj
Master mj

Reputation: 93

in my situation my class folder was in wrong address so check if your class is in correct package.

Upvotes: 4

Anton Bondarenko
Anton Bondarenko

Reputation: 639

IntelliJ IDEA Ultimate

Add your main class to IntelliJ Spring Application Context, for example Application.java

File -> Project Structure..

left side: Project Setting -> Modules

right side: find in your package structure Spring and add + Application.java

Upvotes: 4

Snkpty
Snkpty

Reputation: 21

I solved the problem by installing mybatis plugin in IDEA. When I installed Mybatis Plugin, it disappeared.

Upvotes: 0

Arayik Harutyunyan
Arayik Harutyunyan

Reputation: 151

Configure application context and all will be ok.

enter image description here

Upvotes: 15

Davel_AG
Davel_AG

Reputation: 136

in my Case, the Directory I was trying to @Autowired was not at the same level,

after setting it up at the same structure level, the error disappeared

hope it can helps some one!

Upvotes: 4

yankae
yankae

Reputation: 241

Check if you missed @Service annotation in your service class, that was the case for me.

Upvotes: 24

Harshad Thombare
Harshad Thombare

Reputation: 81

Use @AutoConfigureMockMvc for test class.

Upvotes: 0

Related Questions