NoSuchBeanDefinitionException: No qualifying bean of type available: expected at least 1 bean which qualifies as autowire candidate

21,557

Solution 1

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.

Solution 2

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.

Share:
21,557
Aarish Ramesh
Author by

Aarish Ramesh

Tech generalist. Problem solver. Algorithmic geek. Product Guy. Loves interesting discussions on technology Let's connect! https://www.twitter.com/aarishr https://www.linkedin.com/in/aarishramesh/ https://github.com/aarishramesh

Updated on July 18, 2020

Comments

  • Aarish Ramesh
    Aarish Ramesh almost 4 years

    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

    @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 ?