是否可以将一个弹簧项目配置的配置自动装配到其他弹簧项目中

时间:2016-03-01 14:39:24

标签: java spring maven spring-mvc autowired

我必须提供春季服务。在一个我正在配置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]类型的限定bean用于依赖:预期至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

解决方案 我尝试了@Import(“”)和@ComponentScan(“”)。我没有得到任何编译时错误,但在运行时我得到JavaMailSender对象的NullPointerException。

2 个答案:

答案 0 :(得分:2)

您还可以从spring批处理应用程序的配置类中导入EmailConfiguration类。

@Import(EmailConfiguration.class)
@Configuration
public class BatchConfig {
 ....
}

答案 1 :(得分:1)

您必须“告诉”您的上下文,您希望将此bean添加到您的应用程序上下文中。

可以通过扫描该类所在的包(EmailConfiguration)来完成。

  1. xml config - 将<context:component-scan base-package="com.example.conf" />添加到您的xml配置中。

  2. java config - 将@ComponentScan("com.example.conf")添加到您的某个配置类中。

相关问题