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