简体   繁体   中英

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. 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

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

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

The definition of DependentClass is like below

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 ?

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.

And it is not necessary that you place your main class under the same package as 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.

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
   public DependentClass depClass(){
    return new DependentClass();
   }

After that Autowired the class you get the instance finally.

DependentClass is not defined in your current Spring Context.DependentClass is not annotated with a bean (@Bean).Hence nosuchbeandefinitionexception occurs.

@Bean
public class DependentClass extends ResourceClass<Map<String, Double>> {
            // Methods and logic
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM