1

I need to load the application.properties file from outside the spring boot war which going to be deployed in tomcat.

I tried various solution missing something

  1. Tried setting environmental variable as below in windows

    name : SPRING_CONFIG_NAME value:D:/test/application.properties

i tried multiple values for above value like file:/// in prefix and only file: as perfix .Nothing worked

  1. Tried having context parameter is tomcat like mentioned in below SO answer https://stackoverflow.com/a/44697239/2751962

  2. Tried loading like this in main file which extends SpringBootServletIntializer

    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class) .properties(getProperties()); }

     public static void main(String[] args) throws Exception {
                    SpringApplication.run(Application.class, args);
    
                     SpringApplicationBuilder 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", "file:///D:/test/application.properties​");
                    return props;
                }
    

I not sure what i missed , Kindly help.

anavaras lamurep
  • 1,303
  • 2
  • 17
  • 33

2 Answers2

5

External Configuration in Spring Boot

When using Spring Boot, there are documented naming conventions and directory structure. A Spring Boot app searches for properties to load from a prioritized list, so there are to suggestions for you to consider:

  1. Use command-line flag spring.config.location to target specific file or directory from which to load properties sources. You can use this to specify directories to search or individual files to load. Be cautious loading individual files though, if you intend to use profile-based properties. (add flag in command like this: java -jar MyJar.jar --spring.config.location=D:\test\)
  2. By default, Spring Boot will look for a ./config/ directory where the WAR is and the directory of the WAR itself, so you may place "application.properties" in either position and it will be loaded.

Pivotal provides a super great reference for Spring Boot. Section 24 covers properties more extensively than I can in a post.

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html (*links to most recent release's reference)

Note: I am not a Windows user, so be careful pasting in that filepath above. Edit Me.


Extending Configuration to Deployable Packages

Normally Spring Boot packages into an executable WAR or JAR that has an embedded servlet container engine that is used for the runtime. In your case, however, you are packaging a conventional WAR and deploying that to an external instance of Tomcat, so the configuration parameters must be propagated through Tomcat, using the JAVA_OPTS variable.

For a Apache Tomcat, the convention is to place your properties in ${catalina_base}/conf where catalina.base points to the location of the Tomcat instance. I created a working demo just now following these steps:

  • First, follow section 88.1 of the reference to setup a base WAR app
  • mvn package
  • Place application.properties in conf directory within Tomcat
  • set JAVA_OPTS=-Dspring.config.location=${catalina.base}/conf/
  • "%CATALINA_HOME%"\bin\startup
  • Deploy

It's not the cleanest deployment pipeline, but if you must use an external Tomcat instance, then this will work. However, to run multiple apps with separate property files on the same Tomcat instance would complicate things. In that case, using Spring Framework (not Boot) would be easier to configure.

Kevin Caravaggio
  • 720
  • 5
  • 19
  • Yes , i have tried the priority list of property file mentioned before itself.While deploying to tomcat , war will be placed in webapp and during deployment it will unzip the war file place it in same directory level and I placed the config folder with application.properties file in the same webapp folder level. Is it correct ? – anavaras lamurep Sep 07 '18 at 04:25
  • The problem is that you are thinking too much about Spring Boot. Spring Boot is designed for executable packages with embedded servlet engines, but you are packaging into a traditional deployable WAR and running on a external servlet container engine. Therefore, loading the environment (properties, etc.) will require working through Tomcat configuration, not Spring Boot. I have a working example and I will edit my answer to explain normal WAR property loading. – Kevin Caravaggio Sep 07 '18 at 20:18
  • Thanks Kevin . I think too much in spring , thats the problem and tried different solution ended in confusion & error. Below Tarun's solution works fine even for absolute path. – anavaras lamurep Sep 09 '18 at 07:02
1

You can try setting properties via XML and or Java configuration and @PropertySource.

@Configuration
@PropertySource("classpath:foo.properties")
public class PropertiesWithJavaConfig {
    //...
}

source :- https://www.baeldung.com/properties-with-spring

Tarun
  • 986
  • 6
  • 19
  • Can we use absolute path with file://D:/test/application.properties? in propertysource annotation.I tried this also , but i am not sure , whether i'm missing something with windows path or anything . It fails and says datasource not loading so appilcation start failed. – anavaras lamurep Sep 07 '18 at 06:30
  • I m not sure that you can use absolute path for property file, This link [Externalized Configuration -Spring boot](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html) i cannot find way to use absolute path , it always uses default classpath with custom path – Tarun Sep 07 '18 at 06:42
  • 1
    Hi Tarun , it works for absolute path also , thank you .we need to have file: prefix with the path. It works for deployable war as well as standalone executable jar. – anavaras lamurep Sep 09 '18 at 07:04