我已经创建了一个简单的单元测试,但是IntelliJ错误地将它突出显示为红色。将其标记为错误

没有豆子?

如你所见,它通过了测试?所以它一定是自动连接的?


当前回答

我删除了这段代码。不再有错误


另一种检查方法: 将@Service注释添加到Repository类中,它应该可以工作。

其他回答

在bean配置文件中放置@Component或@configuration似乎可以工作,例如:

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

简单,你只需要做2步

添加休眠内核依赖项 将@Autowired改为@Resource。

==>> change @Autowired to  @Resource

在类级别使用@EnableAutoConfiguration注释和@Component。它将解决这个问题。

例如:

@Component
@EnableAutoConfiguration  
public class ItemDataInitializer  {

    @Autowired
    private ItemReactiveRepository itemReactiveRepository;

    @Autowired
    private MongoOperations mongoOperations;
}

检查bean的包是否正确编写

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


import ...

@Service
class ServiceX {

  ...

}

我删除了这段代码。不再有错误


另一种检查方法: 将@Service注释添加到Repository类中,它应该可以工作。