How to Externalize Application.Properties in Tomcat Webserver for Spring

access externalize application.properties in Tomcat for Spring boot application?

To access application.properties file from tomcat directory. then we need to follow below steps

Need to add plugin in pom.xml. which means it'll ignore the workspace application.properties file after deployment

<!-- Added the below plugin to not include the application.properties inside the war -->
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<packagingExcludes>
**/application.properties/
</packagingExcludes>
</configuration>
</plugin>

Need to copy the application.properties file to tomcat directory lib location. then we need to change the ServletInitializer.java file.

"classpath:mortgage-api/" means we need to create a folder with name mortgage-api in tomcat lib folder and will copy application.properties file to this location. check the code comment.

import java.util.Properties;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MortgageLoanApiApplication.class).properties(loadproperties());
}

/**
* Added this method to load properties from the classpath while intializing the server app
* location of the properties file should be inside tomcat directory:
* lib/mortgage-api/application.properties
* @return
*/
private Properties loadproperties() {
Properties props = new Properties();
props.put("spring.config.location", "classpath:mortgage-api/");
return props;
}

}

then mvn clean, then build war file mvn install

How to externalize Spring Boot application.properties to tomcat/lib folder

A solution could be to load application-{profile}.properties as @PropertySource annotations as this question suggests, but then the logging system wont work, as you can see in the documentation.

The logging system is initialized early in the application lifecycle
and as such logging properties will not be found in property files
loaded via @PropertySource annotations.

This means that your logging properties in application-{profiles}.properties like:

logging.config=classpath:myapp1/logback.xml
logging.path = /path/to/logs
logging.file = myapp1.log

will be ignored and the logging system wont work.

To solve this I have used the SpringApplicationBuilder.properties() method to load properties at the beginning, when the application is configured. There I set the 'spring.config.location' used by Spring Boot to load all the application-{profiles}.properties:

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;
}
}

Then I have moved the properties files from src/main/resources to src/main/resources/myapp1

.
├src
| └main
| └resources
| └myapp1
| └application.properties
| └application-development.properties
| └logback.xml
└─pom.xml

In the pom.xml I have to set the scope of embedded tomcat libraries as "provided".
Also, to exclude all properties files in src/main/resources/myapp1 from the final war and generate a configuration free, deployable war:

    <plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<packagingExcludes>
**/myapp1/
</packagingExcludes>
</configuration>
</plugin>

Then in Tomcat I have

├apache-tomcat-7.0.59
└lib
├─myapp1
| └application.properties
| └logback.xml
└─myapp2
└application.properties
└logback.xml

Now I can generate the configuration free war and drop it into the apache-tomcat-7.0.59/webapps folder. Properties files will be resolved using the classpath, independently for each webapp:

   apache-tomcat-7.0.59/lib/myapp1
apache-tomcat-7.0.59/lib/myapp2
apache-tomcat-7.0.59/lib/myapp3

separate application.properties from .war for tomcat container

You can run your application with spring.config.location property set to the path to your properties file using the file: protocol:

# will look for /etc/myapp/application.properties
-Dspring.config.location=file:/etc/myapp/

# will look for /etc/myapp/custom.properties
-Dspring.config.location=file:/etc/myapp/custom.properties

Default config locations that are always searched:

  1. file:./config/
  2. file:./
  3. classpath:/config/
  4. classpath:/

There is a lot more info in the Externalized Configuration section of the documentation for Spring Boot.

Spring Boot 2.0 externalize property file in Tomcat using WAR packaging

it is too late to configure in the SpringBootServletInitializer, you must set the property before spring application run

@SpringBootApplication
public class MyApplication {

public static void main(String[] args) {
System.setProperty("spring.config.name", "db");
SpringApplication.run(MyApplication.class, args);
}
}

Can't deploy Spring-Boot WAR file on External Tomcat 7

Deploy to Tomcat 8 or later

Your error says the interface HttpSessionIdListener cannot be found.

Looking at the Javadoc for that interface tells us it was added in Servlet 3.1 specification.

Now look at the Which version? page on the Tomcat site. There we see that Tomcat 7 supports Servlet 3.0. For Servlet 3.1, you need Tomcat 8 or later.

Be aware that Tomcat 8.0.x has been superseded by Tomcat 8.5.x.



Related Topics



Leave a reply



Submit