我试图在ItemProcessorImpl中使用@Autowired EmailSerive,但是它抛出空指针异常
示例
@Component
public class PeopleItemProcessor implements ItemProcessor<People, People> {
private static final Logger log = LoggerFactory.getLogger(PeopleItemProcessor.class);
@Autowired
EmailService emailService;
@Override
public People process(People item) throws Exception {
People people = new People();
emailService.emailServiceHTML(item.getEmail(), "Test Email");
log.debug("-------mail trigger or not----");
people.setId(item.getId());
people.setDate(new Date());
people.setIsSent(Boolean.TRUE);
people.setEmail(item.getEmail());
people.setFirstName(item.getFirstName());
people.setLastName(item.getLastName());
return people;
}
}
EmailService
@Component
public class EmailService {
@Autowired
private JavaMailSender mailSender;
public void emailServiceHTML(String to, String subject) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setTo(to);
helper.setText("hi ", true); // set to html
helper.setSubject(subject);
mailSender.send(message);
}
}