简体   繁体   中英

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?

在此处输入图像描述

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

在此处输入图像描述

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.

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

My version of IntelliJ IDEA Ultimate (2016.3.4 Build 163) seems to support this. The trick is that you need to have enabled the Spring Data plugin.

在此处输入图像描述

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, eg:

@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.

I always solve this problem doing de following.. Settings>Inspections>Spring Core>Code than you shift from error to warning the severity option

在此处输入图像描述

I am using spring-boot 2.0, and intellij 2018.1.1 ultimate edition and I faced the same issue.

I solved by placing @EnableAutoConfiguration in the main application class

@SpringBootApplication
@EnableAutoConfiguration
class App{
/**/
}

检查您是否错过了服务类中的 @Service 注释,对我来说就是这种情况。

Putting @Component or @configuration in your bean config file seems to work, ie something like:

@Configuration
public class MyApplicationContext {
    @Bean
    public DirectoryScanner scanner() {
        return new WatchServiceDirectoryScanner("/tmp/myDir");
    }
}

@Component
public class MyApplicationContext {
    @Bean
    public DirectoryScanner scanner() {
        return new WatchServiceDirectoryScanner("/tmp/myDir");
    }
}

Configure application context and all will be ok.

在此处输入图像描述

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 {}

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;
}

If you don't want to make any change to you code just to make your IDE happy. I have solved it by adding all components to the Spring facet.

  1. Create a group with name "Service, Processors and Routers" or any name you like;
  2. Remove and recreate "Spring Application Context" use the group you created previously as a parent.

在此处输入图像描述

For me the solution was to place @EnableAutoConfiguration in the Application class under the @SpringBootApplication its going to underline it because its redundant. Delete it and voila all you warnings regarding missing beans are vanished! Silly Spring...

And one last piece of important information - add the ComponentScan so that the app knows about the things it needs to wire. This is not relevant in the case of this question. However if no @autowiring is being performed at all then this is likely your solution.

@Configuration
@ComponentScan(basePackages = {
    "some_package",
})
public class someService {

只要您的测试通过,您就很好,通过将光标移到错误上并在第一个项目的子菜单中点击alt + enter ,您会发现Disable Inspection选择

simple you have to do 2 steps

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

当它出现在 IntelliJ v.14 中时,我正在使用此注释来隐藏此错误:

@SuppressWarnings("SpringJavaAutowiringInspection")

I had similar issue in Spring Boot application. The application utilizes Feign (HTTP client synthetizing requests from annotated interfaces). Having interface SomeClient annotated with @FeignClient , Feign generates runtime proxy class implementing this interface. When some Spring component tries to autowire bean of type SomeClient , Idea complains no bean of type SomeClient found since no real class actually exists in project and Idea is not taught to understand @FeignClient annotation in any way.

Solution: annotate interface SomeClient with @Component . (In our case, we don't use @FeignClient annotation on SomeClient directly, we rather use metaannotation @OurProjectFeignClient which is annotated @FeignClient and adding @Component annotation to it works as well.)

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.

What you need to do is add

@ComponentScan("package/include/your/annotation/component") in AppConfiguration.java .

Since I think your AppConfiguraion.java 's package is deeper than your annotation component (@Service, @Component...)'s package,

such as "package/include/your/annotation/component/deeper/config" .

I had a similar problem in my application. When I added annotations incorrect highliting dissapeared.

@ContextConfiguration(classes = {...})

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!

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

just add below two annotations to your POJO.

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

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!

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.

All you need to do to make this work is the following code:

@ComponentScan
public class PriceWatchTest{

    @Autowired
    private PriceWatchJpaRepository priceWatchJpaRepository;
...
...
}

Sometimes - in my case that is - the reason is a wrong import. I accidentally imported

import org.jvnet.hk2.annotations.Service

instead of

import org.springframework.stereotype.Service

by blindly accepting the first choice in Idea's suggested imports. Took me a few minutes the first time it happend :-)

我只需要使用@EnableAutoConfiguration 来解决它,但是这个错误对功能没有影响。

可以通过在 Spring Boot 应用程序主类上放置 @EnableAutoConfiguration 来解决。

在我的情况下,我的班级文件夹的地址错误,因此请检查您的班级是否在正确的包中。

将注释@Service添加到您的 Repository 类中,它应该可以工作。

我遇到了同样的问题,并使用“文件”菜单下的“无效缓存...”解决了它。

I was having the issue. Just use

@SpringBootTest

@AutoConfigureMockMvc

annotation with the test class.

Surprisingly, A Feign oriented project that successfully ran with Eclipse could not run in InteliJ. When started the application, InteliJ complained about the Feign client I tried to inject to the serviceImpl layer saying: field personRestClient (my Feign client) in ... required a bean of type ... that could not be found. Consider defining a bean of type '....' in your configuration.

I wasted a long time trying to understand what is wrong. I found a solution (for InteliJ) which I do not completely understand:

  1. Alt Shift F10 (or run menu)
  2. Select 'Edit configuration'
  3. In configuration window, Check the checkbox 'include dependencies with "Provided" scope'
  4. Run your application

Or choose Eclipse :)

Check if the package of your bean is written correctly

//Check if this is written right 
package com.package1.package2.package3


import ...

@Service
class ServiceX {

  ...

}

使用 @AutoConfigureMockMvc 进行测试类。

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

I have a ServerService.java class. That's not been assigned as a @Bean , due to that have got this issue.

@SpringBootApplication
public class HelloServerApplication {

public static void main(String[] args) {
    SpringApplication.run(HelloServerApplication.class, args);
}
@Bean
public ServerService getServerService() {
    return new ServerService();
 }
}

In order to create bean and inject it class in spring framework, Class should be marked with @Componet, @Service or @Repository in class level accordingly. Make sure you have used it.

文件 -> 设置(Ctrl+Alt+S) -> 插件 -> Spring Boot 助手 -> 安装 -> 确定

I removed this code. no more errors

在此处输入图像描述


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

As for intelliJ version 2021.2.3 (Ultimate Edition) I changed "incorrect injection point autowiring in spring bean compontents to warning which works for me在此处输入图像描述

@Autowired(required = false) 将关闭 intellij

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

在此处输入图像描述

就我而言,我将@Autowired更改为@Resource ,并且错误的提示刚刚消失了

I encountered this issue too, and resolved it by the removing Spring Facet:

  • File -> Project Structure
  • Select Facets
  • Remove Spring

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM