I got the following problem when trying to run my application. Have debugged everything and still nothing.
The IDE is finding the bean without any issue so I'm very confused as to what is happening here.
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mailManager': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.testmail.app.service.implement.CustomMManagerService.setMailSender(org.springframework.mail.javamail.JavaMailSender); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSender] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
My bean generation is located in following file:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.testmail.app")
public class WebConfig extends WebMvcConfigurerAdapter {
//CODE CODE CODE
@Bean
public JavaMailSenderImpl mailSender() {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setProtocol("SMTP");
javaMailSender.setHost("127.0.0.1");
javaMailSender.setPort(25);
return javaMailSender;
}
// CODE CODE CODE
}
Code for CustomMManager:
public interface CustomMManager extends Serializable {
void sendMail(String mailFrom,String mailTo,String subject,String mailBody);
}
Finally the code for CustomMManagerService:
@Service("mailManager")
public class CustomMManagerService implements CustomMManager {
private JavaMailSender mailSender;
@Autowired
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
@Override
public void sendMail(final String mailFrom, final String mailTo, final String subject, final String mailBody) {
try {
mailSender.send(new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage)
throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage,
false, "UTF-8");
message.setFrom(mailFrom);
message.addTo(mailTo);
message.setSubject(subject);
message.setText(mailBody, true);
}
});
} catch (MailSendException e) {
// your codes;
}
}
}
Help is really appreciated.
to create Bean don't forgot to specify mail properties, with Java class or in application.properties file, exemple
Have you tried to declare your bean returning the interface implemented? Something like this:
check application.properties config, such as:
if you use spring-boot,can check should use @EnableAutoConfiguration this annotation
You may have forgotten to set the following properties:
As already stated you need to set at least
spring.mail.host
in your application properties. Spring Boot autoconfiguration only creates aJavaMailSender
bean if the property is set: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mail/MailSenderPropertiesConfiguration.java#L38As per comment from mserioli the answer is that the bean must be declared in the configuration file being called at root.
In this case: Move
to
which is called in:
Thus solving the problem. Thanks guys for assistance.