Spring boot IntelliJ Idea+;无法自动连线。未找到类型为的bean

Spring boot IntelliJ Idea+;无法自动连线。未找到类型为的bean,spring-boot,intellij-idea,Spring Boot,Intellij Idea,在我的IntelliJ想法中,我一直看到下面的错误,但是代码在执行过程中运行良好 Could not autowire. No beans of 'PortfolioRequestHandler' type found. less... (Ctrl+F1) Inspection info:Checks autowiring problems in a bean class. 示例代码 我怎样才能摆脱这个?我使用的是IntelliJ Idea ULTIMATE 2018.2您确定Spring

在我的IntelliJ想法中,我一直看到下面的错误,但是代码在执行过程中运行良好

Could not autowire. No beans of 'PortfolioRequestHandler' type found. less... (Ctrl+F1) 
Inspection info:Checks autowiring problems in a bean class.
示例代码


我怎样才能摆脱这个?我使用的是IntelliJ Idea ULTIMATE 2018.2

您确定Spring Bean的连接正确并且是IDE问题吗

  • 检查您的
    PortfolioRequestHandler
    类是否用
    @Service
    @Component
    @Repository
    注释(通过组件扫描进行bean配置)

  • 否则,检查您的bean是否连接在
    @Configuration
    annotated类->在这种情况下,应该有一个方法返回类型为
    PortfolioRequestHandler
    的实例,并用
    @bean

  • 尝试添加一个配置类(如2所述),并将该类添加到
    @springbootest(classes={…}
    注释中;请参见下面的示例

  • @springbootest(classes={Application.class,CustomBeanConfig.class})

  • 看看这个,也许有帮助:

  • 确保为模块配置了Spring上下文:Try version from如果继续看到错误,但应用程序按预期工作,请使用示例项目在处报告错误。
    @ActiveProfiles("test")
    @RunWith(SpringRunner.class)
    @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
    @SpringBootTest(classes = {Application.class})
    public class PortfolioRequestHandlerTest {
    
        @Autowired
        private PortfolioRequestHandler portfolioRequestHandler;
    
        ...
        ...
    }
    
    @Configuration
    public class CustomBeanConfig {
    
       @Bean
       public PortfolioRequestHandler get PortfolioRequestHandler() {
           return new PortfolioRequestHandler();
       }
    }