[英]NoSuchBeanDefinitionException: No qualifying bean of type available: expected at least 1 bean which qualifies as autowire candidate
I am trying to migrate a Spring 4.xx to Spring boot and it has a dependency on a class in external spring 2.5 jar. 我正在尝试将Spring 4.xx迁移到Spring引导,并且它依赖于外部spring 2.5 jar中的类。 I have made all the autowiring changes and below is my application class 我已完成所有自动装配更改,以下是我的应用程序类
@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 ? 是因为DependentClass正在扩展一个类吗? Can someone help me figure out the reason for the error ? 有人可以帮我弄清楚错误的原因吗?
The DependentClass
does not have @Component
annotation on it. DependentClass
上没有@Component
注释。 So, you need to create a bean of DependentClass
yourself either via XML or Java config. 因此,您需要通过XML或Java配置自己创建DependentClass
的bean。
And it is not necessary that you place your main class under the same package as DependentClass
. 并且您不必将主类放在与DependentClass
相同的包中。
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 depClass inside 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 annotaion,如: -
@Bean
public DependentClass depClass(){
return new DependentClass();
}
After that Autowired the class you get the instance finally. 在Autowired the class之后,您最终得到了实例。
DependentClass is not defined in your current Spring Context.DependentClass is not annotated with a bean (@Bean).Hence nosuchbeandefinitionexception occurs. DependentClass没有在您当前的Spring Context中定义.DependentClass没有使用bean(@Bean)注释.Hence nosuchbeandefinitionexception发生。
@Bean
public class DependentClass extends ResourceClass<Map<String, Double>> {
// Methods and logic
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.