【发布时间】:2016-03-01 14:39:23
【问题描述】:
我必须启动服务。在一个中,我将 javaMailSender 配置如下:
@Configuration
@PropertySource("classpath:application.properties")
public class EmailConfiguration {
@Value("${mail.host}")
String host;
@Value("${mail.username}")
String username;
@Value("${mail.password}")
String password;
@Value("${mail.smtp.auth}")
String auth;
@Value("${mail.smtp.port}")
String port;
@Value("${mail.smtp.starttls.enable}")
String enable;
@Value("${mail.smtp.fallback}")
String fallback;
@Value("${mail.smtp.ssl.enable}")
String ssl;
@Bean
public JavaMailSender javaMailSender()
{
JavaMailSenderImpl msender=new JavaMailSenderImpl();
Properties mailProperties=new Properties();
mailProperties.put("mail.smtp.auth",auth);
mailProperties.put("mail.smtp.ssl.enable",ssl);
mailProperties.put("mail.smtp.fallback",fallback);
mailProperties.put("mail.smtp.starttls.enable",enable);
msender.setJavaMailProperties(mailProperties);
msender.setHost(host);
msender.setPort(Integer.parseInt(port));
msender.setUsername(username);
msender.setPassword(password);
return msender;
}
}
现在在其他基本上是 spring bacth 工作的 spring 服务中,我正在自动装配依赖项,如下所示:
public class NotificationItemProcessor implements ItemProcessor<NotificationInstance, NotificationInstance> {
private static final Logger logger = LoggerFactory.getLogger(Application.class.getName());
@Autowired
JavaMailSender javaMailSender;
@Autowired
Connection connection;
@Autowired
Queue queue;
@Override
public NotificationInstance process(NotificationInstance notificationInstance)
{
if(notificationInstance !=null)
{
NotificationChannel channelAdaptor;
NotificationChannel channel = NotificationChannelFactory.getSingleton().getChannelInstance(NotificationChannelType.valueOf(notificationInstance.getTargetType().toUpperCase()));
notificationInstance.setRetries(notificationInstance.getRetries()+1);
logger.info("Trying to send notification for id {}",notificationInstance.getNotificationId());
Boolean status=channel.send(notificationInstance, javaMailSender);
updateStatus(notificationInstance, status);
return notificationInstance;
}
else
return null;
}
public void updateStatus(NotificationInstance notificationInstance, Boolean status)
{
if(status)
notificationInstance.setStatus(NotificationStatus.SENT.toString());
else if (notificationInstance.getRetries() < 5)
notificationInstance.setStatus(NotificationStatus.QUEUED.toString());
else
notificationInstance.setStatus(NotificationStatus.FAILED.toString());
}
}
另外我已经在第二个服务 pom.xml 中包含了第一个服务的依赖关系。 运行第一个运行良好的服务后,当我启动第二个服务时,出现如下错误:
framework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为 [org.springframework.mail.javamail.JavaMailSender] 的合格 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
解决方案 我尝试了@Import("") 和@ComponentScan("")。我没有收到任何编译时错误,但在运行时我收到了 JavaMailSender 对象的 NullPointerException。
【问题讨论】:
标签: java spring maven spring-mvc autowired