Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to locate the properties file in the spring context configuration file

I am using spring web mvc project, and I put all the spring related files under WEB-INF\spring, including a ormlite.xml and a jdbc.properties.

Now I want to locate the jdbc.properties file in the ormlite.xml,Like this:

<context:property-placeholder location="/WEB-INF/spring/jdbc.properties"/>

But when I run the application ,it will told me that :

Could not load properties

It can not find the properties file.

What is the problem?

like image 729
hguser Avatar asked Dec 25 '22 22:12

hguser


1 Answers

From Spring forum:

The problem is that /WEB-INF isn't accessible as it isn't in the root of the path, you must use the same path as you use in your test case (include the src/main/webapp part but that will break your application from running).

I suggest you move the jdbc.properties to the src/main/resources directory and simply use classpath: prefix to load the properties.

Code:

<context:property-placeholder location="classpath:jdbc.properties"/>

The code above assumes they are on the root of the classpath (which is where they are when they are in src/main/resources).

I hope this can help someone else.

like image 80
hguser Avatar answered May 16 '23 07:05

hguser