我需要彈簧服務。在一個我配置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;
}
}
其他春季服務,這基本上是一個春天bacth工作,我自動裝配的依賴性如下:
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]找到依賴關係:預計至少有1個bean有資格作爲此依賴關係的自動連線候選。依賴註解:{@ org.springframework.beans.factory.annotation.Autowired(所需=真)}
解 我與兩個@Import( 「」)和@ComponentScan( 「」)嘗試。我沒有得到任何編譯時錯誤,但在運行時,我得到了JavaMailSender對象的NullPointerException。