我创建了一个简单的单元测试,但IntelliJ不正确地将其突出显示为红色,并标记为错误。
没有bean?
正如您在下面所看到的,测试通过了吗? 所以必须是自动装配(Autowired)?
我创建了一个简单的单元测试,但IntelliJ不正确地将其突出显示为红色,并标记为错误。
没有bean?
正如您在下面所看到的,测试通过了吗? 所以必须是自动装配(Autowired)?
当使用@SpringBootApplication
注解创建一个基于Spring Boot的应用程序时,我也遇到了同样的问题。据spring参考文档所述,该注解代表@Configuration
、@EnableAutoConfiguration
和@ComponentScan
三个注解。
预期,新的注解可以正常工作,我的应用程序也顺利运行,但是,Intellij一直抱怨无法满足@Autowire
依赖项。只要我改回分别使用@Configuration
、@EnableAutoConfiguration
和@ComponentScan
,错误就停止了。看起来Intellij 14.0.3(以及很可能是早期版本)尚未配置识别@SpringBootApplication
注解。
目前,如果这些错误让你很烦恼,那么请回到这三个单独的注解。否则,忽略Intellij,因为你的依赖关系是正确配置的,因为你的测试通过了。
永远记住...
人始终比机器更伟大。
在存储库类上添加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
有时需要指定 @ComponentScan 扫描组件的位置。您可以通过将要扫描的包作为该注解的参数传递来实现,例如:
@ComponentScan(basePackages={"path.to.my.components","path.to.my.othercomponents"})
然而,正如已经提到的那样,@SpringBootApplication注解替代了@ComponentScan,因此在这种情况下,您必须做相同的事情:
@SpringBootApplication(scanBasePackages={"path.to.my.components","path.to.my.othercomponents"})
至少在我的情况下,Intellij停止了抱怨。
@SpringBootApplication(scanBasePackages={"com.a.b, com.a.c"})
,尽管应用程序运行良好,但Intellij不喜欢它。将其更改为@SpringBootApplication(scanBasePackages={"com.a.b", "com.a.c"})
对我有帮助! - GriffoGoes我正在使用spring-boot 2.0和Intellij 2018.1.1 ultimate版本,我遇到了同样的问题。
我通过将@EnableAutoConfiguration放置在主应用程序类中解决了这个问题。
@SpringBootApplication
@EnableAutoConfiguration
class App{
/**/
}
冗余声明:@SpringBootApplication已经应用了@EnableAutoConfiguration
¯\(ツ)/¯ - Michael@EnableAutoConfiguration
。 - Arefe@SpringBootApplication
,以修复所有错误。 - Jos请检查您的服务类是否缺少@Service注释,这也是我遇到的情况。
@Component
在类级别上注释,同时使用@EnableAutoConfiguration
。它将解决此问题。
例如:@Component
@EnableAutoConfiguration
public class ItemDataInitializer {
@Autowired
private ItemReactiveRepository itemReactiveRepository;
@Autowired
private MongoOperations mongoOperations;
}
你是否确认在你的服务实现类上使用了 @Service
注解?这对我有用。
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserServices {}