刚开始学习spring 遇到个错误 求解 谢谢~~~~~~~
控制台报的错误信息:
======
[b]Error creating bean with name 'mailService': Injection of resource methods failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.mail.internet.MimeMessage] is defined: Unsatisfied dependency of type [class javax.mail.internet.MimeMessage]: expected at least 1 matching bean[/b]
======
MailServiceImpl:
======
@Component("mailService")
@Transactional
public class MailServiceImpl implements IMailService{
private JavaMailSender mailSender;
private MimeMessage mimeMessage;
@Override
public void sendMail(MailFormBeans mail) {
try {
MimeMessageHelper mailMessage = new MimeMessageHelper(this.mimeMessage, true, "UTF-8");
// 设置邮件消息的发送者
mailMessage.setFrom(mail.getMailSender());
// 设置邮件消息的主题
mailMessage.setSubject(mail.getMailSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// 设置邮件正文,true表示以html的格式发送
mailMessage.setText(mail.getMailContent(), true);
// 得到要发送的地址数组
String[] toAddresses = mail.getMailReciver().split(";");
for (int i = 0; i < toAddresses.length; i++) {
mailMessage.setTo(toAddresses[i]);
// 发送邮件
this.mailSender.send(this.mimeMessage);
System.out.println("Mail ServiceImpl执行成功");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public JavaMailSender getMailSender() {
return mailSender;
}
@Resource
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public MimeMessage getMimeMessage() {
return mimeMessage;
}
@Resource
public void setMimeMessage(MimeMessage mimeMessage) {
this.mimeMessage = mimeMessage;
}
}
======
Beans.xml:
======
<context:component-scan base-package="......." />
<!-- 就加了这个 -->
======
当我启动tomcat的时候就报错了~~~~~~~~~~~~~~~~
麻烦给点建议~~~~~~
纠结我好几天了
已解决: 漏了 <bean id="mimeMessage" class="javax.mail.internet.MimeMessage" factory-bean="javaMailSender" factory-method="createMimeMessage" />
控制台报的错误信息:
======
[b]Error creating bean with name 'mailService': Injection of resource methods failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.mail.internet.MimeMessage] is defined: Unsatisfied dependency of type [class javax.mail.internet.MimeMessage]: expected at least 1 matching bean[/b]
======
MailServiceImpl:
======
@Component("mailService")
@Transactional
public class MailServiceImpl implements IMailService{
private JavaMailSender mailSender;
private MimeMessage mimeMessage;
@Override
public void sendMail(MailFormBeans mail) {
try {
MimeMessageHelper mailMessage = new MimeMessageHelper(this.mimeMessage, true, "UTF-8");
// 设置邮件消息的发送者
mailMessage.setFrom(mail.getMailSender());
// 设置邮件消息的主题
mailMessage.setSubject(mail.getMailSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// 设置邮件正文,true表示以html的格式发送
mailMessage.setText(mail.getMailContent(), true);
// 得到要发送的地址数组
String[] toAddresses = mail.getMailReciver().split(";");
for (int i = 0; i < toAddresses.length; i++) {
mailMessage.setTo(toAddresses[i]);
// 发送邮件
this.mailSender.send(this.mimeMessage);
System.out.println("Mail ServiceImpl执行成功");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public JavaMailSender getMailSender() {
return mailSender;
}
@Resource
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public MimeMessage getMimeMessage() {
return mimeMessage;
}
@Resource
public void setMimeMessage(MimeMessage mimeMessage) {
this.mimeMessage = mimeMessage;
}
}
======
Beans.xml:
======
<context:component-scan base-package="......." />
<!-- 就加了这个 -->
======
当我启动tomcat的时候就报错了~~~~~~~~~~~~~~~~
麻烦给点建议~~~~~~
纠结我好几天了
已解决: 漏了 <bean id="mimeMessage" class="javax.mail.internet.MimeMessage" factory-bean="javaMailSender" factory-method="createMimeMessage" />