Spring-boot: Better support to externalize application.properties for container deployed war

Created on 28 May 2014  路  11Comments  路  Source: spring-projects/spring-boot

Spring boot is great to generate jars with embedded tomcat but I find it very lacking for deploying to existing application servers (for example tomcat).

The problem is how to externalize the application.properties file: the only viable option I found is to start tomcat with an option similar to:
-Dspring.config.location=file:/usr/local/myapp/application.properties

But this works for just _one_ spring boot application: how may I have more than one if I use several spring boot apps on my container?

The usual way to externalize configs is to pass them to the webapp via JNDI, so, my ideal would be to define an environment of this kind:

automatically picked up by the webapp.

Is this possible to with the actual spring boot version or be implemented in the future?

Most helpful comment

In order to have a per web application spring.config.location you can also try this:

public class Application extends SpringBootServletInitializer {

 @Override
 protected SpringApplicationBuilder configure(SpringApplicationBuilder springApplicationBuilder) {
    return springApplicationBuilder
            .sources(Application.class)
            .properties(getProperties());
 }

 public static void main(String[] args) {

    SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(Application.class)
            .sources(Application.class)
            .properties(getProperties())
            .run(args);
 }

   static Properties getProperties() {
      Properties props = new Properties();
      props.put("spring.config.location", "classpath:myapp1/");
      return props;
    }
}

Check this for more details: http://stackoverflow.com/questions/31017064/how-to-externalize-spring-boot-application-properties-to-tomcat-lib-folder

All 11 comments

Could you provide an example of how you typically use JNDI to configure your application? Spring already provides a JndiPropertySource so I'm wondering if we can just auto-configure that.

I think you can use servlet context init params as well (should work out of the box).

I didn't know about JndiPropertySource but I'm unable to find even only one example to use it with spring boot, if you could point me to one I would be grateful.

Anyway my request was a bit different: it was about telling to spring boot where to find the application.properties file using a JNDI resource (a string with a value for spring.config.location) in this way I can use JNDI to deploy the war in production and I can use the usual application.properties file for integration testing.

That's the same use case (and the same as servlet init params): you use them to set the location of the properties file.

I'm sorry I don't understand how you propose to to that.

Also, it's important that it would be used only in a certain profile since it's used just for production

Well how about just trying it? Set up a JNDI variable called java:comp/env/spring.config.location and see if it works.

Just wow, that's great :+1:

Thanks

In order to have a per web application spring.config.location you can also try this:

public class Application extends SpringBootServletInitializer {

 @Override
 protected SpringApplicationBuilder configure(SpringApplicationBuilder springApplicationBuilder) {
    return springApplicationBuilder
            .sources(Application.class)
            .properties(getProperties());
 }

 public static void main(String[] args) {

    SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(Application.class)
            .sources(Application.class)
            .properties(getProperties())
            .run(args);
 }

   static Properties getProperties() {
      Properties props = new Properties();
      props.put("spring.config.location", "classpath:myapp1/");
      return props;
    }
}

Check this for more details: http://stackoverflow.com/questions/31017064/how-to-externalize-spring-boot-application-properties-to-tomcat-lib-folder

Hi @dsyer, sorry to post on a closed issue... I just wanted to know if it's possible to directly configure an individual application.properties property value using JNDI?

e.g. Use the following JNDI environment resource to configure my.custom.property: foo from application.yml

java:comp/env/my.custom.property = bar

Thanks!

@nickgrealy I think that's how it works (it's documented in the Spring Boot user guide), but I'm not sure I understand the foo/bar bit. Did you try it and it doesn't work?

Is there any working sample showing how to set application.properties values (not whole file) with external tomcat?

Was this page helpful?
0 / 5 - 0 ratings