Rob*_*_UK 117 java spring annotations intellij-idea autowired
我创建了一个简单的单元测试,但IntelliJ错误地将其突出显示为红色.将其标记为错误
没豆子?
如下所示,它通过了测试?那么必须是Autowired?
Abi*_*oso 151
在使用注释创建Spring Boot应用程序时,我遇到了同样的问题@SpringBootApplication
.这个注释代表@Configuration
,@EnableAutoConfiguration
并@ComponentScan
根据春季参考.
正如所料,新的注释工作正常,我的应用程序运行顺利但是,Intellij一直在抱怨未实现的@Autowire
依赖项.一旦我改回使用@Configuration
,@EnableAutoConfiguration
并且@ComponentScan
单独地,错误就停止了.似乎Intellij 14.0.3(很可能也是早期版本)尚未配置为识别@SpringBootApplication
注释.
现在,如果错误打扰你那么多,那么回到那三个单独的注释.否则,忽略Intellij ...您的依赖项解析已正确配置,因为您的测试通过了.
总记得...
人永远比机器大.
mkc*_*zyk 46
@Repository
在存储库类上添加Spring注释.
我知道它应该没有这个注释.但是如果你添加它,IntelliJ将不会显示错误.
@Repository
public interface YourRepository ...
...
Run Code Online (Sandbox Code Playgroud)
如果你使用Spring Data扩展Repository
类,那将是冲突的问题.然后你必须指出明确的pagkages.
import org.springframework.data.repository.Repository;
...
@org.springframework.stereotype.Repository
public interface YourRepository extends Repository<YourClass, Long> {
...
}
Run Code Online (Sandbox Code Playgroud)
接下来,您可以无错误地自动装配您的存储库.
@Autowired
YourRepository yourRepository;
Run Code Online (Sandbox Code Playgroud)
它可能不是一个好的解决方案(我猜你试图两次注册repositorium).但为我工作,不要显示错误.
也许在新版本的IntelliJ中可以修复:https://youtrack.jetbrains.com/issue/IDEA-137023
Nat*_*tix 24
我的IntelliJ IDEA Ultimate版本(2016.3.4 Build 163)似乎支持这一点.诀窍是你需要启用Spring Data插件.
Joã*_*tos 19
有时您需要指示@ComponentScan应扫描组件的位置.您可以通过将包作为此注释的参数传递来执行此操作,例如:
@ComponentScan(basePackages={"path.to.my.components","path.to.my.othercomponents"})
Run Code Online (Sandbox Code Playgroud)
但是,如前所述,@ SpringBootApplication注释替换了@ComponentScan,因此在这种情况下,您必须执行相同的操作:
@SpringBootApplication(scanBasePackages={"path.to.my.components","path.to.my.othercomponents"})
Run Code Online (Sandbox Code Playgroud)
至少在我的情况下,Intellij停止了抱怨.
cri*_*ess 17
我总是通过以下方式解决这个问题. 设置>检查> Spring Core>代码,而不是从错误转移到警告严重性选项
小智 14
我使用的是spring-boot 2.0和intellij 2018.1.1终极版,我遇到了同样的问题.
我通过将@EnableAutoConfiguration放在主应用程序类中来解决
@SpringBootApplication
@EnableAutoConfiguration
class App{
/**/
}
Run Code Online (Sandbox Code Playgroud)
小智 12
@Service
您是否检查过您是否在服务实现之上使用了注释?这对我有用。
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserServices {}
Run Code Online (Sandbox Code Playgroud)
小智 7
简单,您只需执行 2 个步骤
@Autowired
@Resource
==>> change @Autowired to @Resource
Run Code Online (Sandbox Code Playgroud)
小智 6
在类级别使用@EnableAutoConfiguration
注释@Component
。它将解决这个问题。
例如:
@Component
@EnableAutoConfiguration
public class ItemDataInitializer {
@Autowired
private ItemReactiveRepository itemReactiveRepository;
@Autowired
private MongoOperations mongoOperations;
}
Run Code Online (Sandbox Code Playgroud)
把@Component
或者@configuration
在bean的配置文件似乎工作,像IE的东西:
@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");
}
}
Run Code Online (Sandbox Code Playgroud)
如果您不想仅仅为了让您的 IDE 满意而对代码进行任何更改。我通过将所有组件添加到 Spring 方面解决了这个问题。
小智 5
当此错误出现在 IntelliJ v.14 中时,我使用此注释来隐藏此错误:
@SuppressWarnings("SpringJavaAutowiringInspection")
Run Code Online (Sandbox Code Playgroud)
小智 5
对我来说,解决方案是将它放在@EnableAutoConfiguration
Application 类中@SpringBootApplication
,因为它是多余的,所以要给它下划线。删除它,瞧,所有关于丢失豆子的警告都消失了!愚蠢的春天...
归档时间: |
|
查看次数: |
141935 次 |
最近记录: |