class required a single bean, but 2 were found:
You may simply name your bean using
@Bean(name="mockMailSender")
public MockMailSender getMockMailSender() { }
and then decorate the autowired field with
@Autowired
@Qualifier("mockMailSender")
private MailSender smtpkMailSender;
Since you're using java config you should mark config method with @Primary
annotation, and not a class:
@Configuration
public class MailConfig {
@Bean
public SmtpkMailSender getSmtpkMailSender(){
return new SmtpkMailSender();
}
@Bean
@Primary
public MockMailSender getMockMailSender(){
return new MockMailSender();
}
}
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";
}
}