1

Is there any way to reload properties from PropertyPlaceholderConfigurer and refresh @Value renderer ?

@Bean public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() throws Exception { final ClassPathResource classPathRessource = new ClassPathResource(PROPERTIES_FILE); final Properties fileProperties = loadDbPropertiesFromInputStream(classPathRessource.getInputStream()); // Instantiate properties dataSource final DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(fileProperties.getProperty(PROPERTY_KEY_DATABASE_DRIVER)); dataSource.setUrl(fileProperties.getProperty(PROPERTY_KEY_DATABASE_URL)); dataSource.setUsername(fileProperties.getProperty(PROPERTY_KEY_DATABASE_USERNAME)); dataSource.setPassword(fileProperties.getProperty(PROPERTY_KEY_DATABASE_PASSWORD)); // Init PropertyPlaceholderConfigurer final PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer(); propertyPlaceholderConfigurer.setLocation(classPathRessource); // Add properties from defined database propertyPlaceholderConfigurer.setPropertiesArray(ConfigurationConverter.getProperties(getDatabaseConfiguration(dataSource))); return propertyPlaceholderConfigurer; } 

Properties are loaded from Database and if database change, I should restart backend server.. So have any to avoid this ?

Thank you

1
  • You should take a look at spring-cloud. idk if it allows from db, but it is possible from versioned files Commented Nov 2, 2020 at 11:04

2 Answers 2

1

You have two choices :

  • Through Spring Boot Actuator
  • Through Spring Cloud Bus

Option 1 : Through Spring Boot Actuator.

Add the spring-boot-starter-actuator dependency in pom.xml :

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> 

When add new changes and you want see that, then you have to call this url :

http://ip:port/actuator/refresh 

But the problem is that the refresh happens at individual server.

Option 2 : Through Spring Cloud Bus

But for this you have to use some messaging queue system like Kafka, RabbitMQ and so on.

I will show with RabbitMQ only.

  • First install Rabbit MQ and run it

  • Add configuration in application.properties to connect to Rabbit MQ.

Read here : https://www.rabbitmq.com/tutorials/tutorial-one-spring-amqp.html

Add the spring-boot-starter-amqp & spring-boot-starter-actuator in the pom.xml :

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> 

When add new changes and you want see that, then you have to call this url :

http://ip:port/actuator/bus-refresh 

Plus Point : The refresh happens on all the servers.

Note: You can use @RefreshScope to auto refresh the @Value renderers. Example :

@Configuration @RefreshScope public class AppConfig { @Value("${some.value}") private String value; @Bean public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() throws Exception { final ClassPathResource classPathRessource = new ClassPathResource(PROPERTIES_FILE); final Properties fileProperties = loadDbPropertiesFromInputStream(classPathRessource.getInputStream()); // Instantiate properties dataSource final DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(fileProperties.getProperty(PROPERTY_KEY_DATABASE_DRIVER)); dataSource.setUrl(fileProperties.getProperty(PROPERTY_KEY_DATABASE_URL)); dataSource.setUsername(fileProperties.getProperty(PROPERTY_KEY_DATABASE_USERNAME)); dataSource.setPassword(fileProperties.getProperty(PROPERTY_KEY_DATABASE_PASSWORD)); // Init PropertyPlaceholderConfigurer final PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer(); propertyPlaceholderConfigurer.setLocation(classPathRessource); // Add properties from defined database propertyPlaceholderConfigurer.setPropertiesArray(ConfigurationConverter.getProperties(getDatabaseConfiguration(dataSource))); return propertyPlaceholderConfigurer; } } 
Sign up to request clarification or add additional context in comments.

Comments

0

Yes, Spring provides Spring cloud bus for such case, you can use it to refresh data without restarting the server, moreover you can use it to refresh multiple instances for your application.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.