How do I load ${catalina.home}/conf/application.properties
in a Spring / Tomcat webapp?
Looking around on StackOverflow and Google I see many discussions which claim it's possible. However, it's just not working for me. In line with the advice from my research my Spring applicationContext.xml
file contains the following line:
<context:property-placeholder location="${catalina.home}/conf/application.properties"/>
But I get this in the logs:
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/Users/username/Servers/apache-tomcat-6.0.36/conf/application.properties]
From the log entry I can see that ${catalina.home}
is expanding correctly. When I expand it by hand in the applicationContext.xml
file it returns the same error. The following returns the contents of the application.properties file as expected:
cat /Users/username/Servers/apache-tomcat-6.0.36/conf/application.properties
So the path is clearly correct. Is this a webapp security or Tomcat server configuration issue?
How to externalize application.properties in Tomcat webserver for Spring? SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment: - A /config subdirectory of the current directory. - The current directory - A classpath /config package - The classpath root
In brief, you can use the application.properties file to: configure Spring Boot framework, define your application custom configuration properties. Creating application.properties in default location Spring Boot loads the application.properties file automatically from the project classpath.
In the case of Tomcat you can do this by adding the following line to your <TOMCAT_HOME>/bin/setenv.sh file (create the file if missing): Place the properties file in that folder. In case you have multiple apps you can set the name of the properties file of each app to be unique. For a Spring Boot App I have done it like this:
Common Embedded Tomcat Configurations 2.1. Server Address and Port The most common configuration we may wish to change is the port number: If we don't provide the server.port parameter it's set to 8080 by default.
For annotation based configuration you can use:
@PropertySource("file:${catalina.home}/conf/application.properties")
The location
of a context:property-placeholder
is a Resource, which means that if you provide just a file path (as opposed to a full URL with a protocol) then the path will be resolved against the base directory of the webapp - it is trying to load /Users/username/Servers/apache-tomcat-6.0.36/webapps/<appname>/Users/username/Servers/apache-tomcat-6.0.36/conf/application.properties
, which does not exist. If you prefix it with file:
it'll work as you require:
<context:property-placeholder location="file:${catalina.home}/conf/application.properties"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With