Java NoSuchBeanDefinitionException: 没有可用类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51384800/
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
NoSuchBeanDefinitionException: No qualifying bean of type available: expected at least 1 bean which qualifies as autowire candidate
提问by Aarish Ramesh
I am trying to migrate a Spring 4.x.x to Spring boot and it has a dependency on a class in external spring 2.5 jar. I have made all the autowiring changes and below is my application class
我正在尝试将 Spring 4.xx 迁移到 Spring boot,它依赖于外部 spring 2.5 jar 中的一个类。我已经进行了所有自动装配更改,下面是我的应用程序类
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackages = { "com.xyz" })
public class MainApiApplication {
public static void main(String[] args) {
SpringApplication.run(MainApiApplication.class, args);
}
}
The dependent class in the external jar is present under the package com.xyz.abc because of which I have placed my main application class under com.xyz package and also added the component scan under the same package
外部jar中的依赖类存在于com.xyz.abc包下,因此我将我的主应用程序类放在com.xyz包下,并在同一个包下添加了组件扫描
Here are my component classes with the dependency autowired
这是我的组件类,具有自动装配的依赖项
@Component
public class ComponentClassA {
@Autowired
private ComponentClassB currencyService;
}
@Component
public class ComponentClassB {
@Autowired
private DependentClass depClass;
}
DependentClass is the class present in the external dependent jar which I have locally attached and built
DependentClass 是我在本地附加和构建的外部依赖 jar 中存在的类
When building the application, compilation of all files is fine and build is generated successfully. But when I start the application, I get the below error
构建应用程序时,所有文件的编译都很好,并且构建成功生成。但是当我启动应用程序时,出现以下错误
Field DependentClass in com.xyz.ComponentClassB required a bean of type 'com.xyz.common.util.DependentClass' that could not be found.
I don't understand the reason for the class from external jar being not found as I have added component scan for the package
我不明白没有找到来自外部 jar 的类的原因,因为我已经为包添加了组件扫描
The definition of DependentClass is like below
DependentClass 的定义如下
public class DependentClass extends ResourceClass<Map<String, Double>> {
// Methods and logic
}
Is it because DependentClass is extending a class ? Can someone help me figure out the reason for the error ?
是因为 DependentClass 正在扩展一个类吗?有人可以帮我找出错误的原因吗?
回答by Sanjeev Sachdev
The DependentClass
does not have @Component
annotation on it. So, you need to create a bean of DependentClass
yourself either via XML or Java config.
上面DependentClass
没有@Component
注释。因此,您需要DependentClass
通过 XML 或 Java 配置创建自己的 bean 。
And it is not necessary that you place your main class under the same package as DependentClass
.
并且没有必要将主类放在与DependentClass
.
回答by Bala Vignesh S
DependentClass is not defined in your current Spring Context.DependentClass is not annotated with a bean (@Bean).Hence nosuchbeandefinitionexceptionoccurs.
DependentClass 未在您当前的 Spring Context 中定义。DependentClass 未使用 bean (@Bean) 进行注释。因此不会发生此类 beandefinitionexception。
@Bean
public class DependentClass extends ResourceClass<Map<String, Double>> {
// Methods and logic
}
回答by Vishal Monga
Define your class as per below:-
按照以下定义您的课程:-
@Component("depClass")
public class DependentClass extends ResourceClass<Map<String, Double>> {
// Methods and logic
}
Component register it into your context defination if this package lie into your ScanBasePackages and the depClassinside the component annotation define the name of your bean.
如果此包位于您的 ScanBasePackages 中,并且组件注释中的depClass定义您的 bean 的名称,则组件将其注册到您的上下文定义中。
you can also call it by:-
您也可以通过以下方式调用它:-
@Autowired
@Qualifier("depClass")
private DependentClass dependentClass;
If that class define in your external class then use @Bean annotaion like:-
如果该类在您的外部类中定义,则使用 @Bean 注释,例如:-
@Bean
public DependentClass depClass(){
return new DependentClass();
}
After that Autowired the class you get the instance finally.
在 Autowired 类之后,你最终得到了实例。