intellij incorrectly saying no beans of type found

2019-01-21 06:31发布

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

21条回答
男人必须洒脱
2楼-- · 2019-01-21 06:41

Sometimes you are required to indicate where @ComponentScan should scan for components. You can do so by passing the packages as parameter of this annotation, e.g:

@ComponentScan(basePackages={"path.to.my.components","path.to.my.othercomponents"})

However, as already mentioned, @SpringBootApplication annotation replaces @ComponentScan, hence in such cases you must do the same:

@SpringBootApplication(scanBasePackages={"path.to.my.components","path.to.my.othercomponents"})

At least in my case, Intellij stopped complaining.

查看更多
我命由我不由天
3楼-- · 2019-01-21 06:42

I just had to use @EnableAutoConfiguration to address it, however this error had no functional impact.

查看更多
看我几分像从前
4楼-- · 2019-01-21 06:44

I had this same issue when creating a Spring Boot application using their @SpringBootApplication annotation. This annotation represents @Configuration, @EnableAutoConfiguration and @ComponentScan according to the spring reference.

As expected, the new annotation worked properly and my application ran smoothly but, Intellij kept complaining about unfulfilled @Autowire dependencies. As soon as I changed back to using @Configuration, @EnableAutoConfiguration and @ComponentScan separately, the errors ceased. It seems Intellij 14.0.3 (and most likely, earlier versions too) is not yet configured to recognise the @SpringBootApplication annotation.

For now, if the errors disturb you that much, then revert back to those three separate annotations. Otherwise, ignore Intellij...your dependency resolution is correctly configured, since your test passes.

Always remember...

Man is always greater than machine.

查看更多
我想做一个坏孩纸
5楼-- · 2019-01-21 06:44

As long as your tests are passing you are good, hit alt + enter by taking the cursor over the error and inside the submenu of the first item you will find Disable Inspection select that

查看更多
beautiful°
6楼-- · 2019-01-21 06:44

This seems to still be a bug in the latest IntelliJ and has to do with a possible caching issue?

If you add the @Repository annotation as mk321 mentioned above, save, then remove the annotation and save again, this fixes the problem.

查看更多
来,给爷笑一个
7楼-- · 2019-01-21 06:44

My solution to this issue in my spring boot application was to open the spring application context and adding the class for the missing autowired bean manually!

(access via Project Structure menu or spring tool window... edit "Spring Application Context")

So instead of SpringApplicationContext just containing my ExampleApplication spring configuration it also contains the missing Bean:

SpringApplicationContext:

  • ExampleApplication.java
  • MissingBeanClass.java

et voilà: The error message disappeared!

查看更多
登录 后发表回答