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

没有豆子?

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


当前回答

@Autowired(required = false) 能让intellij闭嘴吗

其他回答

检查您是否在服务类中遗漏了@Service注释,我就是这种情况。

在存储库类上添加Spring注释@Repository。

我知道没有这个注释它也能工作。但是如果添加了这个,IntelliJ将不会显示错误。

@Repository
public interface YourRepository ...
...

如果您将Spring Data与扩展Repository类一起使用,则会产生冲突包。那么你必须直接标明包裹。

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

@org.springframework.stereotype.Repository
public interface YourRepository extends Repository<YourClass, Long> {
    ...
}

接下来,您可以自动连接存储库而不会出现错误。

@Autowired
YourRepository yourRepository;

这可能不是一个好的解决方案(我猜您试图注册存储库两次)。但是为我工作,不要出错。

也许在IntelliJ的新版本中可以修复:https://youtrack.jetbrains.com/issue/IDEA-137023

只需添加以下两个注释到您的POJO。

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

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.

解决方案:用@Component注解接口SomeClient。(在我们的例子中,我们没有直接在SomeClient上使用@FeignClient注释,而是使用metaannotation @OurProjectFeignClient,它是带注释的@FeignClient,并向其添加@Component注释也可以工作。)

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

例如:

@Component
@EnableAutoConfiguration  
public class ItemDataInitializer  {

    @Autowired
    private ItemReactiveRepository itemReactiveRepository;

    @Autowired
    private MongoOperations mongoOperations;
}