How to reload the bean configuration with properties from the database

I need to create a spring javamail bean initialized with values โ€‹โ€‹from the database for each message sent. Based on this article How to load application properties from a database

I configured my PropertyPlaceholderConfigurer to load values โ€‹โ€‹from the properties file and the database. I have the following bean ( mailSender ) in my java configuration class to send mail from my application, which loads the 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; } } 

But my problem is changing the values โ€‹โ€‹of the database. mailSender bean has old values โ€‹โ€‹that are provided when the application context starts. For any changes that should occur in the bean, I need to restart the server to update the bean values.

I enter this bean into my controller, where I need to send such mail,

 @Autowired private JavaMailSender mailSender; 

Based on some suggestion, I tried using @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) , but did not create a new definition for bean (still has old values).

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

Any help is appreciated. Thanks.

Similar question: Spring: update a bean created using code that reads a database

+1
source share
2 answers

I created a custom implementation of the mail service and removed the bean mail sender, as suggested by JB Nizet. In the implementation of the service, I read properties from the database and create a new instance each time mail is sent from the application. As 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); 

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

0
source

Try this instead

 public class CustomProperties extends Properties { private final AppLogger logger = AppLogger.getInstance(); private static final long serialVersionUID = 1L; private static final String GET_QUERY = "select config_value from config_params where config_key = ?"; private final JdbcTemplate jdbcTemplate; public CustomProperties(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } @Override public String getProperty(String key) { return jdbcTemplate.queryForObject(GET_QUERY, new Object[]{key}, String.class); } } 
0
source

Source: https://habr.com/ru/post/986906/


All Articles