0

I am implementing the spring cloud config to externalise configurations in my application. I have spring boot application with property placeholder configurer already implemented in it. As later I have done changes related to spring cloud config, it is not reading the property files from the remote repository. It always reads from the static properties given in property placeholder configurer. How can I make spring cloud config to work along with property placeholder configurer? I want my application to read properties from remote repository as per profile given and override the property placeholder configurer values. Kindly help me with this issue. Thanks in advance!

Property placeholder configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" value="classpath:application.properties" />
    </bean>
    <bean id="dtSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="url" value="**test_url**" />
    </bean>
</beans> 

Spring Cloud config application.properties:

spring.application.name=<appname>
spring.cloud.config.server.git.uri=https://github.com/{username}/test.git
spring.cloud.config.server.git.username=<username>
spring.cloud.config.server.git.password=<password>
profiles.active=${spring.profiles.active}
spring.cloud.config.server.bootstrap=true
spring.config.name=<appname>

I have written one controller to check whether it's reading properties from remote repository.

@RefreshScope
@RestController
class HelloController {

    @Value("${db.url:Hello default}")
    private String message;

    @RequestMapping("/message")
    String getMessage() {
        return this.message;
    }
}

When I run and check for db url, it always returns "test_url" which is given in Property placeholder configuration and it do not read from the remote repository.

Let me know if I am missing something to get it working or else if it is not possible to combine both Spring cloud config and property placeholder configurer.

Sapna Maid
  • 89
  • 11
  • Just remove the `PropertyPlaceHolderConfigurer`... You are interfering with the way spring boot is creating and delegating loading of properties. Spring Boot already loads the `application.properties` no need to load it yourself. – M. Deinum Mar 06 '17 at 09:28
  • Hi M.Deinum, The PropertyPlaceHolderConfigurer is the legacy code in my application and it has dependencies on local test suite and I can not remove it. I want spring cloud config to work along with this. – Sapna Maid Mar 06 '17 at 10:28
  • As M.Deinum said remove the PropertyPlaceHolderConfigurer it will work...For test purpose you can use @TestPropertySource annotation to load the properties... – VelNaga Mar 23 '17 at 09:23
  • Hi VelNaga M.Deinum, I removed property placeholder configurer still it was not working. Later on I found out that I need to remove org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration exclusion and got it working. Thanks! – Sapna Maid Mar 23 '17 at 09:43

0 Answers0