Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I load ${catalina.home}/conf/application.properties in a Spring / Tomcat webapp?

Tags:

spring

tomcat

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?

like image 916
9 revs Avatar asked Jul 12 '13 08:07

9 revs


People also ask

How to externalize application properties in Tomcat webserver for spring?

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

How to configure Spring Boot framework with application properties?

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.

How do I create a unique property file for a tomcat?

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:

What are the configuration options for embedded Tomcat?

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.


2 Answers

For annotation based configuration you can use:

@PropertySource("file:${catalina.home}/conf/application.properties")
like image 56
user2805890 Avatar answered Nov 16 '22 00:11

user2805890


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"/>
like image 40
Ian Roberts Avatar answered Nov 15 '22 23:11

Ian Roberts