如何使用数据库中的属性重新加载配置Bean [英] How to reload configuration bean with properties from database

查看:539
本文介绍了如何使用数据库中的属性重新加载配置Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为每个发送的邮件创建一个spring javamail bean,并使用数据库中的值进行初始化.根据本文,如何加载应用程序属性来自数据库

I need to create a spring javamail bean initialized with values from database for each mail sent. Based on this article, How to Load Application Properties from Database

我已将PropertyPlaceholderConfigurer配置为从属性文件和数据库中加载值.我的java配置类中有以下bean(mailSender),用于从应用程序发送邮件,该应用程序从数据库加载主机,端口,用户名和密码,

I've configured my PropertyPlaceholderConfigurer to load values from both properties file and database. I have the following bean(mailSender) in my java configuration class for sending mail from my application which loads host, port, username and password from the database,

@Configuration
public class MailSenderConfig {
    @Bean
    public JavaMailSender mailSender() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost(PropertiesUtils.getProperty("mail.server.host"));
        javaMailSender.setPort(Integer.parseInt(PropertiesUtils.getProperty("mail.server.port")));
        javaMailSender.setUsername(PropertiesUtils.getProperty("mail.server.username"));
        javaMailSender.setPassword(PropertiesUtils.getProperty("mail.server.password"));
        return javaMailSender;
    }
}

但是我的问题是,当数据库值更改时,mailSender bean具有启动应用程序上下文时提供的旧值.为了对Bean进行任何更改,我需要重新启动服务器以更新Bean值.

But my problem is when the database values changes the mailSender bean has the old values which is provided while starting the application context. For any changes to take place to the bean I need to restart the server for updating the bean values.

我将此bean注入我的控制器中,我需要在其中发送这样的邮件,

I inject this bean in my controller where I need to send a mail like this,

@Autowired
private JavaMailSender mailSender;

基于一些建议,我尝试使用@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE),但是它没有创建新的bean定义(仍然具有旧值).

Based on some suggestion, I tried to use @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) but it din't create a new bean definition(still has old values).

所以我需要的是,每次使用此mailSender bean发送邮件时,它都应该从数据库中选择值,而无需重新启动上下文或服务器.是否可能或如何做到?

So what I need is, every time a mail is sent using this mailSender bean it should pick the values from database without restarting the context or server. Is it possible or how it can be done?

感谢您的帮助.谢谢.

类似的问题: Spring:刷新创建的bean从读取数据库的代码中

推荐答案

我已经创建了一个自定义邮件服务实现,并按照JB Nizet的建议删除了Mail sender bean.在服务实现中,每次从应用程序发送邮件时,我都会从数据库中读取属性并创建一个新实例.像下面一样

I've create a custom mail service implementation and removed the Mail sender bean as JB Nizet suggested. In the service implementation I'm reading properties from the database and creating a new instance everytime a mail is sent from the application. Like below

@Autowired
private MailSender mailSender;

JavaMailSenderImpl jms = mailService.createMailSender();
MimeMessage mimeMessage = new MimeMessage(mailService.getMailSession(jms));
MimeMessageHelper message = null;
message.setSubject("Test mail");
message.setTo("email@domain.com");
message.setText(htmlContent, true);
Transport.send(message);

其中createMailSender()为您发送的每封邮件创建一个JavaMailSenderImpl的新实例.

where createMailSender() creates a new instance of JavaMailSenderImpl for every mail you send.

这篇关于如何使用数据库中的属性重新加载配置Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆