0

I am using Spring MVC(but no Spring Boot)

I have config application.properties as my property file

<context:property-placeholder location="classpath:application.properties" />

But I need to switch it to another one when debugging. Is there a way to set a different one easily without modify any source code.

Please note that I am not trying to switch log4j.properties but application.properties.

PS: I am using InteliJ

JaskeyLam
  • 15,405
  • 21
  • 114
  • 149
  • Possible duplicate of [Specifying a custom log4j.properties file for all of JUnit tests run from Eclipse](http://stackoverflow.com/questions/24231773/specifying-a-custom-log4j-properties-file-for-all-of-junit-tests-run-from-eclips) – Raedwald Mar 03 '16 at 09:39
  • @Raedwald , Not log4j , but application that will be loaded by spring. – JaskeyLam Mar 03 '16 at 09:46
  • I know not log4j, but the approach is the same: debug and normal runs use different classpaths, with the debug runs searching a debug folder (directory) first. – Raedwald Mar 03 '16 at 11:02
  • Possible duplicate of http://stackoverflow.com/questions/34845990/spring-use-one-application-properties-for-production-and-another-for-debug – Raedwald Mar 03 '16 at 11:04
  • @Raedwald as to you two more comments, I am afraid I still can't get my problem solved, #1, I do not run as unit test, so I no test classpath will be needed, 2#, I do not have spring boot , and I had tried the solution from:http://stackoverflow.com/questions/34845990/spring-use-one-application-properties-for-production-and-another-for-debug – JaskeyLam Mar 03 '16 at 12:07

1 Answers1

0

You can specify your properties based on a environment variable. For example:

<context:property-placeholder location="file:${app_config}/application.properties" />

So when you run your app you can specify this environment variable value, for example

For normal run:

java -Dapp_config="/Users/myuser/myproject/src/main/resources"

or for test or debug run:

java -Dapp_config="/Users/myuser/myproject/src/test/resources"

In your IDE you can specify this as a Debug or Run param in your Run or Debug config.

Ricardo Vila
  • 1,626
  • 1
  • 18
  • 34
  • But this needs more config options when running the app at prod. Can I just have more jobs when dev but nothing more needed when at prod? – JaskeyLam Mar 03 '16 at 12:09
  • Well you need a way to specify in wich environment the application is running. Maybe you can add to your main() a method that checks for the environment variable (something like System.getenv("app_config")) , if it is not defined you can define it with a default value. – Ricardo Vila Mar 03 '16 at 13:11
  • I hope only more options set when dev, but nothing more to the source code. – JaskeyLam Mar 04 '16 at 02:28
  • So you only need to pass extra params when running in 'dev mode'. It's easy in Eclipse or other IDEs to define different run configurations so you can have 'Run in dev mode' which will have defined the environmental variable or 'Run in production mode' that will not. – Ricardo Vila Mar 07 '16 at 09:51