0

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"), new ClassPathResource("myapp.properties")});
    return ppc;
}
 }

I replace these values at runtime:

@Configuration
public class DataSourceConfig {

@Value("${jdbc.url}")
private String jdbcUrlDefault;
      // there are more @Value
}

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

public static void main(String[] args) {
    // accept a properties file and override those values defined in DataSourceConfig class
    ApplicationContext application = new AnnotationConfigApplicationContext("packaageThatContainsConfiguration");
    DataSourceConfig dataSourceConfig = (DataSourceConfig) application.getBean("dataSourceConfig");
    System.out.println(dataSourceConfig.getJdbcUrlDefault());
}


<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <archive>
                <manifest>
                        <mainClass>com.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <finalName>server-app</finalName>
</build>

When Apache Maven builds the application the properties file will be on the classpath. I want to override the properties value with the properties file provided.

I want to accept a properties file through the main program that gets replaced, so the Spring side starts the PropertyPlaceholderConfigurer.

I have a properties file containing the value that I want.

When I run the following command on the Command prompt:

java -jar server-app-jar-with-dependencies.jar -D myapp.properties "C:\Users\user\Desktop\myapp.properties"

It prints out the original value that is placed in src/main/resources/myapp.properties

I want to print the value that is on the Desktop myapp.properties.

What am I doing wrong?

user
  • 81
  • 1
  • 1
  • 9
  • Here you go, http://stackoverflow.com/questions/4097614/spring-configuration-with-system-property This should answer your question – Ernie Sep 23 '14 at 12:16
  • I don't think this answers my query. I still don't understand what I am doing wrong? Could you please provide some examples? – user Sep 23 '14 at 12:29
  • Keep Maven away from reach of children! It can be toxic! – nullptr Sep 23 '14 at 13:07

1 Answers1

0

Currently you have

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

What you can change if have properties accept a parameter say 'location' What you can then do is

    if(location) {
        Resource file = new FileSystemResource(location);
        ppc.setLocations(file);
    } else {
        ppc.setLocations(new ClassPathResource[] {new ClassPathResource("propertiesFile"), new ClassPathResource("myapp.properties")});
    }

Then if there is a location arg it will use that otherwise default to the one on the classpath

---- Adding link to relivant topic ----- Dealing with command line arguments and Spring

Community
  • 1
  • 1
Ernie
  • 567
  • 2
  • 6
  • 23
  • Thank you. How can the properties() method accept in a properties(location)? I understand the approach, but not how this will the location be injected. – user Sep 23 '14 at 13:15
  • You create the properties bean definition after the application has been run in the main method.. not actually the place but you get the idea. Ill quickly find you a link to more info – Ernie Sep 23 '14 at 14:19