Java intellij错误地表示未找到自动连线存储库的类型bean

Java intellij错误地表示未找到自动连线存储库的类型bean,java,spring,intellij-idea,annotations,autowired,Java,Spring,Intellij Idea,Annotations,Autowired,我创建了一个简单的单元测试,但IntelliJ错误地将其高亮显示为红色。将其标记为错误 没有豆子 正如你在下面看到的,它通过了测试?所以它必须是自动连线的 我在使用SpringBootApplication的@SpringBootApplication注释创建SpringBoot应用程序时遇到了同样的问题。此注释表示符合以下条件的@Configuration、@EnableAutoConfiguration和@ComponentScan 正如预期的那样,新注释工作正常,我的应用程序运行顺利,但

我创建了一个简单的单元测试,但IntelliJ错误地将其高亮显示为红色。将其标记为错误

没有豆子

正如你在下面看到的,它通过了测试?所以它必须是自动连线的


我在使用SpringBootApplication的
@SpringBootApplication
注释创建SpringBoot应用程序时遇到了同样的问题。此注释表示符合以下条件的
@Configuration
@EnableAutoConfiguration
@ComponentScan

正如预期的那样,新注释工作正常,我的应用程序运行顺利,但是,Intellij不断抱怨未实现的
@Autowire
依赖关系。当我重新使用
@Configuration
@EnableAutoConfiguration
@ComponentScan
时,错误就停止了。Intellij 14.0.3(很可能也是早期版本)似乎尚未配置为识别
@SpringBootApplication
注释

现在,如果错误对您的影响如此之大,那么返回到这三个单独的注释。否则,忽略Intellij…您的依赖项解析配置正确,因为您的测试通过了

永远记住

人总是比机器伟大


在Repository类上添加Spring注释
@Repository

我知道没有这个注释它应该可以工作。但如果您添加此项,IntelliJ将不会显示错误

@Repository
public interface YourRepository ...
...
如果将Spring数据与扩展
存储库
类一起使用,则会出现冲突分页。那么你必须明确地指出pagkages

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

@org.springframework.stereotype.Repository
public interface YourRepository extends Repository<YourClass, Long> {
    ...
}
这可能不是一个好的解决方案(我想你正在尝试注册repositorium两次)。但是为我工作,不要显示错误


也许在IntelliJ的新版本中可以修复:

我在spring引导应用程序中解决这个问题的方法是打开spring应用程序上下文并手动为丢失的Autowiredbean添加类

(通过项目结构菜单或spring工具窗口访问…编辑“spring应用程序上下文”)

因此,除了SpringApplicationContext仅包含示例应用程序spring配置之外,它还包含缺少的Bean:

SpringApplicationContext:

  • ExampleApplication.java
  • MissingBeanClass.java

et voilá:错误消息消失了

@Component
@configuration
放入bean配置文件似乎是可行的,例如:

@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");
    }
}

最后一条重要信息是添加
组件扫描
,以便应用程序了解需要连接的内容。这与本问题无关。但是,如果根本没有执行
@autowiring
,则这可能是您的解决方案

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

如果您不想为了让IDE满意而对代码进行任何更改。我通过向Spring方面添加所有组件解决了这个问题

  • 创建一个名为“服务、处理器和路由器”或任意名称的组
  • 删除并重新创建“Spring应用程序上下文”,将之前创建的组用作父级

  • 这似乎仍然是最新IntelliJ中的一个bug,与可能的缓存问题有关


    如果将@Repository注释添加为上述mk321,保存,然后删除注释并再次保存,这将修复问题

    您需要做的是添加

    @ComponentScan(“package/include/your/annotation/component”)
    位于
    AppConfiguration.java

    因为我认为您的
    appconfiguration.java
    包比您的注释组件(@Service,@component…)包要更深


    例如
    “package/include/your/annotation/component/deepher/config”

    我在应用程序中遇到了类似的问题。 当我添加注释时,不正确的highliting消失了

    @ContextConfiguration(classes = {...})
    

    有时,您需要指出@ComponentScan应在何处扫描组件。可以通过将包作为此批注的参数传递来完成此操作,例如:

    @ComponentScan(basePackages={"path.to.my.components","path.to.my.othercomponents"})
    
    但是,如前所述,@SpringBootApplication annotation替换了@ComponentScan,因此在这种情况下,您必须执行相同的操作:

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

    至少就我而言,Intellij停止了抱怨

    我的IntelliJ IDEA Ultimate版本(2016.3.4版本163)似乎支持这一点。诀窍是您需要启用Spring数据插件


    只要您的测试通过,您就可以通过将光标移到错误上并在第一个项目的子菜单中,点击
    alt+enter
    ,您将发现
    禁用检查
    选择该项

    执行此操作所需的全部操作是以下代码:

    @ComponentScan
    public class PriceWatchTest{
    
        @Autowired
        private PriceWatchJpaRepository priceWatchJpaRepository;
    ...
    ...
    }
    

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

    @SuppressWarnings("SpringJavaAutowiringInspection")
    

    我总是通过做以下事情来解决这个问题。。 设置>检查>弹簧芯>代码然后从错误切换到警告严重性选项


    我正在使用spring boot 2.0和intellij 2018.1.1终极版,我面临着同样的问题

    我通过将@EnableAutoConfiguration放在主应用程序类中解决了这个问题

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

    有时,在我的情况下,原因是一个错误的进口。我不小心导入了

    import org.jvnet.hk2.annotations.Service
    
    而不是

    import org.springframework.stereotype.Service
    

    通过盲目地接受Idea建议进口中的第一选择。第一次发生时花了我几分钟时间:-)

    我在Spring Boot应用程序中遇到了类似的问题。该应用程序利用了FIGN(HTTP客户端从带注释的接口合成请求)。将接口
    SomeClient
    注释为
    @FeignClient
    ,Feign生成实现此接口的运行时代理类。当某个Spring组件尝试自动连接
    SomeClient
    类型的bean时,Idea抱怨没有找到
    SomeClient
    类型的bean,因为没有真正的clas
    import org.springframework.stereotype.Service
    
    ==>> change @Autowired to  @Resource
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserServiceImpl implements UserServices {}
    
    @ComponentScan
    @Configuration
    public class YourClass {
        //TODO
    }