4

I have server.yml file that contains Spring Framework only properties like port number, context root path, application name.

And, I have a applicationContext.xml that has the following:

<util:properties id="springProperties" location="classpath*:my.properties"> <context:property-placeholder location="classpath*:my.properties" local-override="true" properties-ref="springProperties" /> 

my.properties file resides in src/main/resources dir of the project.

So then, I can access properties from my java classes like:

@Autowired @Qualifier("springProperties") private Properties props; public String getProperty(String key){ return props.getProperty(key); } or like `${my.prop}` 

When I build war and run Spring Boot (java -jar server.war), internal my.properties resolves, and everything works as expected.

However, I wanted to override that file with the external my.properties. I read https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

I tried to run something like:

java -jar server.jar --spring.config.location=classpath:/my.properties or

java -jar server.jar --spring.config.location=my.properties

But the only properties I can override by this are from my server.yml. Meaning, I can override port number, or application name. But the internal my.properties is never affected.

Am I doing something wrong? I understand that external my.property simply should be in a classpath, then it overrides internal my.property. But it never happens.

1
  • Have you tried to do this without the qualifier of springProperties for this configuration. The copy of my.properties you are loading externally likely is not affecting that context. Commented Oct 17, 2017 at 21:23

1 Answer 1

2

You can use @PropertySource({ "classpath:override.properties" }) to add extra files from classpath and then use Environment object to get the values or @Value annotations for the values

@PropertySource({ "classpath:other.properties" }) @Configuration public class Config{ @Autowired private Environment env; //use env.getProperty("my.prop") @Value("${my.prop}") private String allowedpatterns; } @Component public OthenClass{ @Autowired //If this class is not component or Spring annotated class then get it from ApplicationContext private Environment env; //use env.getProperty("my.prop") @Value("${my.prop}") private String allowedpatterns; } 

If you are using Spring Boot below code can be used to get ApplicationContext

ConfigurableApplicationContext ctx = app.run(args); Environment env = ctx.getBean(Environment.class); 
Sign up to request clarification or add additional context in comments.

2 Comments

After above, I had to just run: java -jar server.jar --spring.config.location=my.properties
You can put properties files in the resources directory as well. Spring boot will pick up files from the class path.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.