Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class required a single bean, but 2 were found:

I have the below interface:

public interface MailSender {
    void sender(String to, String subject,String body);
}

With 2 imlementation of it:

public class SmtpkMailSender implements MailSender   {

    static Log log=LogFactory.getLog(MailSender.class); 

    public void sender(String to, String subject,String body){
        log.info("SMTP To: "+to);
        log.info("SMTP Subjecy: "+subject);
        log.info("SMTP body: "+body);
    }

}

and the second one is:

@Primary
public class MockMailSender implements MailSender   {

    static Log log=LogFactory.getLog(MailSender.class); 

    public void sender(String to, String subject,String body){
        log.info("To: "+to);
        log.info("Subject: "+subject);
        log.info("body: "+body);
    }

}

I used the dependency injection into the controller class which is as following:

@RestController
public class MailController {

    @Autowired
    private MailSender smtpkMailSender;

    @RequestMapping("/send")
    public String send(){
        smtpkMailSender.sender("Person", "Important", "Take Care");
        return "mail is sent";
    }
}

At the end i have a configuration class which contains my Beans:

@Configuration
public class MailConfig {

    @Bean
    public SmtpkMailSender getSmtpkMailSender(){
        return new SmtpkMailSender();
    }

    @Bean
    public MockMailSender getMockMailSender(){
        return new MockMailSender();
    }

}

Unfortunatly, when i run my application it complains with:

Description:

Field smtpkMailSender in com.example.demo.MailController required a single bean, but 2 were found:
    - getSmtpkMailSender: defined by method 'getSmtpkMailSender' in class path resource [com/example/mail/MailConfig.class]
    - getMockMailSender: defined by method 'getMockMailSender' in class path resource [com/example/mail/MailConfig.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

As you see, although i have specified the MockMailSender as Primary the spring still complains, and cannot identify it

like image 725
Jeff Avatar asked Sep 21 '17 12:09

Jeff


People also ask

Can we use @component and @bean in same class?

@Component is a class level annotation whereas @Bean is a method level annotation and name of the method serves as the bean name. @Component need not to be used with the @Configuration annotation where as @Bean annotation has to be used within the class which is annotated with @Configuration.

Can we use @qualifier and @primary together?

We can use @Qualifier and @Primary for the same bean. Use @Qualifier to inject specific bean otherwise Spring injects bean by default which is annotated with @Primary.

What is the use of @qualifier and @primary?

@Primary indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency. @Qualifier indicates specific bean should be autowired when there are multiple candidates. For example, we have two beans both implement the same interface.

What is @primary annotation in Spring boot?

Spring @Primary annotation is used to give a higher preference to the marked bean when multiple beans of the same type exist. Spring, by default, auto-wires by type. And so, when Spring attempts to autowire and there are multiple beans of the same type, we'll get a NoUniqueBeanDefinitionException: Caused by: org.


1 Answers

You can use @Qualifier annotation for specify which specific type of your implementation, you want to autowire.

@RestController
public class MailController {

   @Autowired
   @Qualifier("smtpkMailSender")
   private MailSender smtpkMailSender;

   @RequestMapping("/send")
   public String send(){
      smtpkMailSender.sender("Person", "Important", "Take Care");
      return "mail is sent";
  }

}

like image 162
Yogi Avatar answered Sep 21 '22 07:09

Yogi