Adding a property file in the shared folder in tom

2020-07-18 09:04发布

问题:

I have a Java Spring MVC web application. I was using an application.properties file to set a few properties value. Now I am trying to get the properties independent of the war file. I have created a new folder shared/classes under tomcat as shown here.

In the catalina.properties file I have added shared.loader=${catalina.base}/shared/classes.

In my root-context.xml file, I added

<context:property-placeholder location="file:${catalina.base}/shared/classes/application.properties"/>

But I am getting the following error:

ERROR o.s.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: E:\projects\smartwcm\workspace-20163101-eclipse-wp\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\shared\classes\application.properties (The system cannot find the path specified)

How can I fix this issue?

回答1:

This is probably one of the limitations of the Servers folder created by eclipe. Where it actually places the contents of this folder is in

.metadata/.plugins/org.eclipse.wst.server.core/tmp0/conf

I've tried this locally and eclipse doesn't seem to publish any folder I create. And eclipse is very specific in naming the folder Tomcat v8.0 Server at localhost-config. This may also imply that property and configuration files are the only accepted system files. I can make everything work very well just by putting an application.properties file at the same level as for example catalina.properties. Then I publish with eclipse and then when I look at the temporary folder, the application file is there. Anyways there doesn't seem to be a specific reason to have your file in shared/classes. After all, as far as I can tell, they are properties to be shared in the server, so I would say their rightfull place is in the config folder.



回答2:

Check out this answear. Maybe it will help

Where to place and how to read configuration resource files in servlet based application?



回答3:

It looks like ${catalina.base} is being interpreted as:

E:\projects\smartwcm\workspace-20163101-eclipse-wp.metadata.plugins\org.eclipse.wst.server.core\tmp0

Have you set the $CATALINA_BASE variable on the machine you're running?



回答4:

I think you need to point to ${catalina.home}. Catalina.base is where tomcat is installed, catalina.home is where tomcat is run from. (This is due to tomcat multihost environment; you can have one instalation of tomcat and multiple hosts).

When encountering problems like this, (if you're on Windows) you should use program FileMon from Sysinternals suite. You should setup a fiter: "PATH contains YOURFILE.NAME" and see search paths where the file is looked. Then you can easily figure out where java process is trying to look for a file and put it in corresponding folder.

However, it's best to setup a normal directory somewhere outside of server home/base, and reference it via absolute OS file path.