简体   繁体   English

NoSuchBeanDefinitionException:没有可用类型的限定bean:预期至少有1个bean有资格作为autowire候选者

[英]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.

相关问题 NoSuchBeanDefinitionException:没有符合类型的合格bean <package> &#39;可用:至少有1个符合自动装配候选条件的bean - NoSuchBeanDefinitionException: No qualifying bean of type '<package>' available: expected at least 1 bean which qualifies as autowire candidate 原因:NoSuchBeanDefinitionException:xxx类型的合格bean至少应包含1个符合自动装配候选条件的bean。 - Caused by: NoSuchBeanDefinitionException: No qualifying bean of type xxx expected at least 1 bean which qualifies as autowire candidate 没有可用类型的合格Bean:预计至少有1个合格为自动装配候选的Bean - No qualifying bean of type available: expected at least 1 bean which qualifies as autowire candidate 没有可用的&#39;xxx.dao.AreaDao&#39;类型的合格Bean:预计至少有1个符合自动装配候选条件的Bean - No qualifying bean of type 'xxx.dao.AreaDao' available: expected at least 1 bean which qualifies as autowire candidate 没有可用的“Package.TestDaoRepo”类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选 - No qualifying bean of type 'Package.TestDaoRepo' available: expected at least 1 bean which qualifies as autowire candidate 没有可用的“javax.sql.DataSource”类型的合格 bean:预计至少有 1 个有资格作为自动装配候选者的 bean - No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate 没有可用的“ru.spb.repository.UserRepository”类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选 - No qualifying bean of type 'ru.spb.repository.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate 没有可用类型的合格 bean - 预计至少有 1 个 bean 有资格作为自动装配候选 - No qualifying bean of type available - expected at least 1 bean which qualifies as autowire candidate NoSuchBeanDefinitionException期望至少有一个bean可以作为此依赖项的autowire候选者 - NoSuchBeanDefinitionException expected at least 1 bean which qualifies as autowire candidate for this dependency NoSuchBeanDefinitionException: 没有为依赖找到 [Repository] ​​类型的合格 bean:预计至少有 1 个符合自动装配条件的 bean - NoSuchBeanDefinitionException: No qualifying bean of type [Repository] found for dependency: expected at least 1 bean which qualifies as autowire
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM