【问题标题】:Is it possible to autowire configuration of one spring project configuration to other spring project是否可以将一个 spring 项目配置的配置自动连接到另一个 spring 项目
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      1. xml 配置 - 将 &lt;context:component-scan base-package="com.example.conf" /&gt; 添加到您的 xml 配置中。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-25
        • 2014-04-16
        • 1970-01-01
        • 2021-10-09
        • 1970-01-01
        相关资源
        最近更新 更多