4

In my sprint boot application, I have a configuration class to read property files: common.properties and dev.properties. I have the same key server.url in both the property files. The value is not overridden. As per the spring documentation, the last property file value should be taken. But it's not working. I am using the spring annoatation @PropertySource to read values.

ServerConfiguration class

@Component
@PropertySources(
{
    @PropertySource(value = "file:Common/config/common.properties", ignoreResourceNotFound = true),
    @PropertySource(value = "file:Dev/config/dev.properties", ignoreResourceNotFound = true)
})
public final class ServerConfiguration {


private final ApplicationContext applicationContext;

/**
 * The Server URL
 */
@Value("${server.url}")
private String serverUrl;

}

common.properties

server.url=ws://some ip

dev.properties

server.url=ws://localhost:8080

The value from common.properties is taken always. I tried changing the order, but still it's not working.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Krishnanunni P V
  • 689
  • 5
  • 18
  • 32
  • 3
    that isn't a configuration it is a component. Use `@Configuration` instead of what you have now. Also why are you trying to roll your own? Just use the spring boot support that already provides loading different files for different environments. – M. Deinum Nov 10 '16 at 11:51
  • 1
    I need this class as a bean only. – Krishnanunni P V Nov 10 '16 at 11:55
  • Then move your `@propertysource` somewhere else and don't call it configuration if it isn't configuration. Also `@Configuration` classes are also beans... – M. Deinum Nov 10 '16 at 11:59
  • If I annotate this class as `@Configuration`, what other modifications should I make in this class ? – Krishnanunni P V Nov 10 '16 at 12:07
  • Well about none.. Unless you are doing more with this class then you are showing here. – M. Deinum Nov 10 '16 at 12:10
  • But my question is why the value from `dev.properties` is not taken ? – Krishnanunni P V Nov 10 '16 at 12:20
  • Because, as I stated, @PropertySource on a non `@Configuraiton` is pretty useless. Next to that each property source is executed in order. As soon as the property is resolved it isn't tried again. Just put all your files in a single `@PropertySource` the `value` property takes a comma seperated list. – M. Deinum Nov 10 '16 at 12:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127807/discussion-between-krishnanunni-p-v-and-m-deinum). – Krishnanunni P V Nov 10 '16 at 12:55
  • @Deinum Not working as you suggested. Any idea ? – Krishnanunni P V Dec 07 '16 at 05:54

1 Answers1

3

you need to add in the application.properties file your active profile

spring.profiles.active=dev
Amir Serry
  • 326
  • 2
  • 13