Java intellij 错误地说没有为自动装配的存储库找到类型的 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26889970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
intellij incorrectly saying no beans of type found for autowired repository
提问by Robbo_UK
I have created a simple unit test but IntelliJ is incorrectly highlighting it red. marking it as an error
我创建了一个简单的单元测试,但 IntelliJ 错误地将其突出显示为红色。将其标记为错误
No beans?
没有豆子?
As you can see below it passes the test? So it must be Autowired?
正如你在下面看到的,它通过了测试?所以它必须是自动装配的?
采纳答案by Abimbola Esuruoso
I had this same issue when creating a Spring Bootapplication using their @SpringBootApplication
annotation. This annotation represents @Configuration
, @EnableAutoConfiguration
and @ComponentScan
according to the spring reference.
我在使用他们的注释创建Spring Boot应用程序时遇到了同样的问题@SpringBootApplication
。这个注解代表@Configuration
,@EnableAutoConfiguration
并且@ComponentScan
根据spring的参考。
As expected, the new annotation worked properly and my application ran smoothly but, Intellijkept complaining about unfulfilled @Autowire
dependencies. As soon as I changed back to using @Configuration
, @EnableAutoConfiguration
and @ComponentScan
separately, the errors ceased. It seems Intellij 14.0.3(and most likely, earlier versions too) is not yet configured to recognise the @SpringBootApplication
annotation.
正如预期的那样,新的注释工作正常,我的应用程序运行顺利,但是Intellij一直抱怨未满足的@Autowire
依赖项。一旦我改回使用@Configuration
,@EnableAutoConfiguration
并@ComponentScan
单独使用,错误就停止了。似乎Intellij 14.0.3(也很可能是早期版本)尚未配置为识别@SpringBootApplication
注释。
For now, if the errors disturb you that much, then revert back to those three separate annotations. Otherwise, ignore Intellij...your dependency resolution is correctly configured, since your test passes.
现在,如果这些错误对您造成如此大的困扰,那么请返回到这三个单独的注释。否则,请忽略Intellij...您的依赖项解析已正确配置,因为您的测试通过了。
Always remember...
总记得...
Man is always greater than machine.
人永远大于机器。
回答by Alpha
I encountered this issue too, and resolved it by the removing Spring Facet:
我也遇到了这个问题,并通过删除 Spring Facet 解决了这个问题:
- File -> Project Structure
- Select
Facets
- Remove Spring
- 文件 -> 项目结构
- 选择
Facets
- 移除弹簧
Good luck!
祝你好运!
回答by mkczyk
Add Spring annotation @Repository
over the repository class.
@Repository
在存储库类上添加 Spring 注释。
I know it should work without this annotation. But if you add this, IntelliJ will not show error.
我知道它应该在没有这个注释的情况下工作。但是如果你添加这个,IntelliJ 不会显示错误。
@Repository
public interface YourRepository ...
...
If you use Spring Data with extending Repository
class it will be conflict pagkages. Then you must indicate explicity pagkages.
如果您将 Spring Data 与扩展Repository
类一起使用,它将是冲突页面。然后你必须明确指出页面。
import org.springframework.data.repository.Repository;
...
@org.springframework.stereotype.Repository
public interface YourRepository extends Repository<YourClass, Long> {
...
}
And next you can autowired your repository without errors.
接下来,您可以毫无错误地自动装配您的存储库。
@Autowired
YourRepository yourRepository;
It probably is not a good solution (I guess you are trying to register repositorium twice). But work for me and don't show errors.
这可能不是一个好的解决方案(我猜您正在尝试注册存储库两次)。但为我工作,不要显示错误。
Maybe in the new version of IntelliJ can be fixed: https://youtrack.jetbrains.com/issue/IDEA-137023
也许在新版本的 IntelliJ 中可以修复:https: //youtrack.jetbrains.com/issue/IDEA-137023
回答by U.V.
My solution to this issue in my spring boot application was to open the spring application context and adding the class for the missing autowired bean manually!
我在 spring boot 应用程序中解决这个问题的方法是打开 spring 应用程序上下文并手动添加缺少的自动装配 bean 的类!
(access via Project Structure menu or spring tool window... edit "Spring Application Context")
(通过项目结构菜单或弹簧工具窗口访问...编辑“弹簧应用程序上下文”)
So instead of SpringApplicationContext just containing my ExampleApplication spring configuration it also contains the missing Bean:
因此,SpringApplicationContext 不仅包含我的 ExampleApplication spring 配置,它还包含缺少的 Bean:
SpringApplicationContext:
弹簧应用上下文:
- ExampleApplication.java
- MissingBeanClass.java
- 示例应用程序.java
- MissingBeanClass.java
et voilà: The error message disappeared!
等等:错误信息消失了!
回答by wired00
Putting @Component
or @configuration
in your bean config file seems to work, ie something like:
将@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");
}
}
回答by HankCa
And one last piece of important information - add the ComponentScan
so that the app knows about the things it needs to wire. This is not relevant in the case of this question. However if no @autowiring
is being performed at all then this is likely your solution.
还有最后一条重要信息 - 添加ComponentScan
以便应用程序知道它需要连接的东西。这与此问题无关。但是,如果根本没有@autowiring
执行任何操作,那么这可能是您的解决方案。
@Configuration
@ComponentScan(basePackages = {
"some_package",
})
public class someService {
回答by Xiao Peng - ZenUML.com
If you don't want to make any change to you code just to make your IDE happy. I have solved it by adding all components to the Spring facet.
如果您不想对代码进行任何更改只是为了让您的 IDE 满意。我已经通过将所有组件添加到 Spring facet 来解决它。
- Create a group with name "Service, Processors and Routers" or any name you like;
- Remove and recreate "Spring Application Context" use the group you created previously as a parent.
- 创建一个名为“Service, Processors and Routers”或任何你喜欢的名字的组;
- 删除并重新创建“Spring Application Context”,使用您之前创建的组作为父组。
回答by Jensen Buck
This seems to still be a bug in the latest IntelliJ and has to do with a possible caching issue?
这似乎仍然是最新 IntelliJ 中的一个错误,并且与可能的缓存问题有关?
If you add the @Repository annotation as mk321 mentioned above, save, then remove the annotation and save again, this fixes the problem.
如果将@Repository 注释添加为上述mk321,保存,然后删除注释并再次保存,这解决了问题。
回答by sfwn
What you need to do is add
你需要做的是添加
@ComponentScan("package/include/your/annotation/component")
in AppConfiguration.java
.
@ComponentScan("package/include/your/annotation/component")
在AppConfiguration.java
。
Since I think your AppConfiguraion.java
's package is deeperthan your annotation component (@Service, @Component...)'s package,
因为我认为你AppConfiguraion.java
的包比你的注释组件(@Service,@Component...)的包更深,
such as "package/include/your/annotation/component/deeper/config"
.
比如"package/include/your/annotation/component/deeper/config"
.
回答by K. Gol
I had a similar problem in my application. When I added annotations incorrect highliting dissapeared.
我在我的应用程序中遇到了类似的问题。当我添加注释时,不正确的高亮消失了。
@ContextConfiguration(classes = {...})