ello我正在使用springboot创建一个web应用程序,在相关代码中,我允许单击一个按钮,从数据库中删除一个对象,然后向该对象的电子邮件地址发送一封电子邮件。在我添加电子邮件部分之前,当单击按钮时,它工作得很好,并且行从表中删除,但是现在我得到一个错误:
java.lang.illegalstateexception:没有为作用域名称“restart”注册作用域
我认为这与控制器中的上下文有关:控制器:
@RequestMapping("/rejectApplicant")
@ResponseBody
public String rejectApplicant(@RequestParam String id, HttpServletRequest request, HttpServletResponse response, Model model) {
Long athleteID = Long.parseLong(id);
Optional<Athlete> optionalAthlete = athleteAuditor.findAthleteById(athleteID);
Athlete athlete = optionalAthlete.get();
AbstractApplicationContext context = new AnnotationConfigApplicationContext(
EmailConfig.class);
MailService mailService = context.getBean("mailService", MailServiceImp.class);
System.out.println("////// " + mailService.toString());
String senderEmailId = "Z@gmail.com";
String receiverEmailId = athlete.getEmail();
System.out.println("////// " + receiverEmailId);
String subject = "Your time at Lyons";
String message = "Dear " + athlete.getName() + "How goes it?";
mailService.sendEmail(senderEmailId, receiverEmailId, subject, message);
context.close();
athleteAuditor.deleteAthlete(athleteID);
return "applicants";
}
//配置
@Configuration
@ComponentScan(basePackages = "xda")
public class EmailConfig {
u/Bean
public JavaMailSender getMailSender()
{
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
// Using Gmail SMTP configuration.
mailSender.setHost("smtp.gmail.com");
mailSender.setPort(587);
/*
* Use your gmail id and password
*/
mailSender.setUsername("X@gmail.com");
mailSender.setPassword("Y");
Properties javaMailProperties = new Properties();
javaMailProperties.put("mail.smtp.starttls.enable", "true");
javaMailProperties.put("mail.smtp.auth", "true");
javaMailProperties.put("mail.transport.protocol", "smtp");
javaMailProperties.put("mail.debug", "true");
mailSender.setJavaMailProperties(javaMailProperties);
return mailSender;
}
}
//邮件服务imp
@Service("mailService")
public class MailServiceImp implements MailService
{
u/Autowired
JavaMailSender mailSender;
public void sendEmail(final String senderEmailId, final String receiverEmailId,
final String subject, final String message)
{
MimeMessagePreparator prep = new MimeMessagePreparator()
{
public void prepare(MimeMessage mimeMessage) throws Exception
{
mimeMessage.setFrom(senderEmailId);
mimeMessage.setRecipient(Message.RecipientType.TO,
new InternetAddress(receiverEmailId));
mimeMessage.setSubject(subject);
mimeMessage.setText(message);
}
};
try
{
mailSender.send(prep);
System.out.println("Successfully sent message");
}
catch (Exception exe)
{
exe.printStackTrace();
}
}
}
//邮件服务
public interface MailService
{
public void sendEmail(final String senderEmailId, final String receiverEmailId,
final String subject, final String message);
}
抱歉,有这么多的代码,我不知道是什么原因,这是我第一次尝试使用SpringBootStarter邮件,谢谢任何帮助!
1条答案
按热度按时间ifsvaxew1#
我想问题是
@ComponentScan
. 我会检查一下@ComponentScan
只使用一次。从baeldung spring组件扫描:对于spring,我们使用@componentscan注解和@configuration注解来指定要扫描的包@没有参数的componentscan告诉spring扫描当前包及其所有子包。
注意,主应用程序类和配置类不一定相同。如果它们不同,那么将主应用程序类放在哪里并不重要。默认情况下,只有配置类的位置很重要,因为组件扫描从其包开始。