I see that the values in application context are not getting picked up when the Spring Boot project is war file and runs on an external Tomcat.
How do I resolve this issue?
Example - server.port=8096 but the port when loaded is still 8080
You may need to move the properties file outside of your project. The application.properties under src/main/resources will remain but referencing the external properties file. For e.g.
spring.config.location=./tomcatfolder/external-application.properties
Related
I actually want to know what exactly happens during WAR packaging of a spring boot application using Maven. My specific interest would be to know whether the embedded tomcat dependency will be included or not while packaging app as WAR.
Also, If we have some tomcat config properties defined in application.properties and deploy the WAR file to external Tomcat which config will be taken into account while running the application? Like the config properties defined in server.xml of Tomcat server or the properties defined in application.properties?
My specific interest would be to know whether the embedded tomcat dependency will be included or not while packaging app as WAR
This is covered in the Spring Boot reference documentation:
If you use the Spring Boot build tools, marking the embedded servlet container dependency as provided produces an executable war file with the provided dependencies packaged in a lib-provided directory. This means that, in addition to being deployable to a servlet container, you can also run your application by using java -jar on the command line.
When you deploy a war to Tomcat, the configuration in server.xml is used rather than the server- and Tomcat-related configuration in application.properties. Specifically, none of your application's server.* properties will have any effect.
I have a spring boot 2 application with 2 property files:
application.properties
application-dev.properties
When I run the application in IntellijIdea using the dev profile, Spring reads both the profile specific file and the default file.
Once I build an executable jar file, I created a new application-prod.properties outside of the jar, with production environment properties and execute the application with --spring.profiles.active=prod expecting it to read this file AND the default application.properties, but it is only reading the file outside the jar (application-prod.properties).
Should Spring Boot read both files or am I expecting something that it does not do by default?
If it should, what can I be missing?
As mentioned by Michael, Spring boot supports property hierarchies.
I tried this and it works as expected, you can find a reference project in here
A few things that I can think about:
Extract the contents of your jar file and confirm that the file named application.properties is bundled in there.
You may be overwriting the spring.config.location property and using filenames instead of directories
From Spring Boot documentation
If you have specified any files in spring.config.location, profile-specific variants of those files are not considered. Use directories in spring.config.location if you want to also use profile-specific properties.
If you override the default spring.config.location then the application may not be able to find the default application.properties file.
By default, application.properties will be used to run the Spring Boot application. While running the JAR file, you need to specify the spring active profile. only one file will be read
There is an option to disregard the spring boot jar package application.properties file and
search the properties values of the application.properties from a directory on the server.
On the server where we run the jar application with spring boot, I need the application to look up the properties values of the application.properties file in another directory on the server, disregarding the existing BOOT-INF\classes\application.properties directory in the jar.
I would like to make an option to disregard the application.properties in jar spring boot generation and fetch the values of an application.properties from a directory on the server.
This configuration would be for the application to work for each environment (production, approval and development) without the need for the developer to change the profiles in the application before the packaging.
Any examples of this setup?
Taken from the Spring documentation, you can pass inthe location of your externalised config using this:
java -jar myproject.jar --spring.config.location=/path/application.properties
This command will support directories, provided spring.config.name matches the name of your config file.
I have developed a web application using spring boot. I have three resource folders in src/main/resources staging,qa,production which consists of application properties and logging configuration for the specific environment. Along with these folders I have application.properties and logging configuration in resources folder which I use it for dev environment. I want to package war file according to the environment using spring-boot-maven plugin. I am new to maven any help would be appreciated?
Convetion is application-{profileName}.properties
Point 10 and 11:
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
e.g application-test.properties
it overrides the application.properties
Make profile in pom.xml
Introduction to profiles:
http://maven.apache.org/guides/introduction/introduction-to-profiles.html
Rather than using Maven to add different folders to your classpath for each environment, you can use Spring profiles.
First of all create different application.properties for each environment, such as:
application-staging.properties
application-qa.properties
application-production.properties
For the logging you can use the logging.config property. So, in application-staging.properties you could use:
logging.config=classpath:logback-staging.xml
In the other properties files you can use different logging.config properties.
Now just run your application with the spring.profiles.active property.
However, an easier solution would be to use externalized configuration. Rather than having to rebuild each time you want to change configuration for a specific profile, you can externalize it by putting an application.properties file next to your JAR/WAR in the correct environment, rather than on your classpath. Spring boot will pick this up automatically.
Now you can also externalize your logging config by placing a logback.xml (or log4j2.xml, ...) file next to your JAR/WAR and just configure your (externalized) application properties with:
logging.config=file:logback.xml
This allows you to edit your configuration and logging without having to change your JAR/WAR.
I have Spring configured to look up a conf/database.properties file to load some configuration.
This works well outside Tomcat, and in Junit tests, but in Tomcat, it never load. Below the images of this problem.
Configurations:
And:
The properties file in project folder:
The parameters to run Tomcat inside Eclipse:
The temp0 Tomcat Folder, where is all the files being generated Ok:
The Tomcat error log:
https://gist.github.com/4060538
I solved the problem. I think I was using spring in the wrong way.
I changed the follow:
In the library I'm importing/referencing, I removed the line where was importing database.properties file.
I created a spring.xml in my main web app, where in this file I imported the database.properties file and the other app-context.xml files I need to reference.
I think Spring spring don't load properties file outside of the jar. You need to load properties file locally in you main application, and so, references another spring-context.xml files needed.