user
user

Reputation: 81

Java Main class overriding properties file at runtime

I am using Apache Maven and Spring. In the src/main/resources folder I have a properties file. These property values can have different values.

I am using PropertyPlaceholderConfigurer.

@Configuration
public class ResourceConfig {

@Bean
public PropertyPlaceholderConfigurer properties( ) {
    PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
    ppc.setIgnoreResourceNotFound(true);
    ppc.setLocations(new ClassPathResource[] {new ClassPathResource("propertiesFile")});
    return ppc;
}
 }

I replace these values at runtime:

@Configuration
public class DataSourceConfig {

@Value("${jdbc.url}")
private String jdbcUrlDefault;
}

This is just a sample. I have a main method:

public static void main(String[] args) {
    // accept a properties file and replace those values defined in DataSourceConfig class
}

When Apache Maven builds the application the properties file will be on the classpath. The properties file are used during the unit testing. I want to some how replace the properties with a new properties file before the main program is launched for production.

I have seen some example of Properties.load(), but I don't want to do this. I want to accept a properties file through the main program that gets replaced, so the Spring side starts the PropertyPlaceholderConfigurer.

How can this be achieved?

Upvotes: 1

Views: 525

Answers (1)

Adi
Adi

Reputation: 2394

you can place your test properties files in src/test/resources/. In test classes, it will use properties file from this location.

properties file placed here above location will not included in your classpath in final build.

use src/main/resources to place resource files that you want in main program

Upvotes: 1

Related Questions