1

This is a part of my config class:

@Configuration
@PropertySource("classpath:properties.properties")
public class DataBaseConfig {
    @Resource
    private Environment env;

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        //some properties getting
        String userName = env.getProperty("username");

        dataSource.setUsername(userName);

        return dataSource;
    }
}

The problem is that SystemEnvironmentPropertySource contains "username" property too, and it's getting processed before ResourcePropertySource, based on my property file, because of propertySources list order. So the wrong value of "username" property is loaded. What's the way to get the properties in file to be processed before the system environment properties?

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
funny-funny
  • 41
  • 1
  • 6
  • Environment variables are always consulted last so if it isn't working like that there is another problem in your environment. – M. Deinum Mar 16 '15 at 13:37
  • @Deinum "Properties added in this way have precedence over any added using the default locations, but have lower priority than system properties, environment variables or the command line." http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html – funny-funny Mar 16 '15 at 13:45
  • You didn't mention that you used Spring Boot, which is quite essential in this case... – M. Deinum Mar 16 '15 at 13:51
  • 1
    Instead of defining your own just add it to the `application.properties`file instead. – M. Deinum Mar 16 '15 at 13:52
  • @Deinum And what if I want to use custom file? – funny-funny Mar 17 '15 at 08:25
  • Then you run into this issue when using `@PropertySource`. You can specify the name of the file to load but that changes the default `application.properties` to your `properties.properties` file. Why don't you want to use the default? – M. Deinum Mar 17 '15 at 09:44
  • @Deinum sure about that? according to the documentation http://docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/htmlsingle/#boot-features-external-config "OS environment variables" have also higher precedence than the default application,properties file – ci_ Mar 18 '15 at 13:36
  • @funny-funny can you not just rename the property in your file? `username` is rather generic. Or remove the OS environment variable before starting your application. – ci_ Mar 18 '15 at 13:37

0 Answers0