Home > Blockchain >  How to solve Unsatisfied Dependency Exception? Problem with Annotation Qualifiers
How to solve Unsatisfied Dependency Exception? Problem with Annotation Qualifiers

Time:10-18

I trying to implemet a singleton design pattern in Spring. I am running in an error wwhen running my code.

my configuration file is:

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("springdemo3") 
public class AppConfig {

}

I have created a service interface as follow:

package springdemo3;

public interface Service {
  void doSomething();
}

I have created interface called Pump as follow;

public interface Pump {
 void features();
}

Created two types of pumps electric and water pump both implementing pump interface, as follow:

package springdemo3;

import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Component
public class ElectricPump implements Pump{

@Override
@Primary
   public void features() {
      System.out.println("This pump is an "
              "electric pump");
   }    
}

package springdemo3;

import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Component
public class WaterPump implements Pump{

   @Override    
   public void features() {
       System.out.println("This pump is pumping"
              " water");
   }    
}

My serviceB bean is created as follow:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component //This is a bean
public class ServiceB implements Service{
   @Autowired  
   @Qualifier("electricpump")      
   private Pump mPump; 

   public Pump getMpump() {
     return mPump;
   }
   public void setMpump(Pump mPump) {
     this.mPump = mPump;
   }

   public void doSomething() {
     System.out.println("We have a water"
              " pump running here");
     mPump.features();
   }    
}

While my main function is as follow:

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/*** Here I'm not using XML configuration and using Singleton Design Pattern */
public class SpringDemo3 {

public static void main(String[] args) {        
           
   ApplicationContext context = 
           new  AnnotationConfigApplicationContext(AppConfig.class);

   ServiceB mServiceB = context.getBean(ServiceB.class);
   mServiceB.doSomething();
 
 }    
}

When I run it, I get the following error:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'serviceB': Unsatisfied dependency expressed through field 'mPump'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'springdemo3.Pump' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier("electricpump")}

I'm trying to understand Spring Framework. Please note all these classes are in the same package. What is it that I'm doing wrong here?

But this worked enter image description here

CodePudding user response:

Please bear in mind that default naming convetion for spring beans is camel case, so in fact the proper name for your bean should be electricPump instead of electricpump. Then, you should be able to autowire the bean.

Other than that, field autowiring is not recommended as it makes unit testing of your service more difficult. Use constructor autowiring preferably. Also, you've placed @Primary in the incorrect position, as it is the annotation which marks the bean to be the first preference for autowiring. It can be used on method level but it only makes sense when the method is defining a bean and is annotated with @Bean annotation as well.

CodePudding user response:

As mentioned in my comment @Qualifier("electricpump") needed a capital P for Pump.

I created a branch and made a pull request to your project:

  • Restructured your project
  • Added a maven pom.xml (because I couldn't run your project)
  • Fixed the bean name 'electricPump'

Consider always using some project management tool. This way among other things, people can easily set up you project and help you out.

  • Related