How to find properties file in spring context configuration file

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

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

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

But when I launched the application, he told me that:

Could not load properties

Cannot find the properties file.

What is the problem?

+4
source share
4 answers

From the Spring forum:

The problem is that / WEB -INF is not available, since it is not in the root of the path, you should use the same path that you use in your test case (including the src / main / webapp part, but this will break your application from running )

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

the code:

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

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

Hope this helps someone else.

+9
source

I had the same problem - property files are out of class path.

My decision:

First define the bean properties:

 <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location"> <value>/WEB-INF/your.properties</value> </property> </bean> 

Then specify it in the placeholder property:

 <context:property-placeholder properties-ref="configProperties" /> 

Works well for me!

+1
source

Instead:

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

Using:

 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="/WEB-INF/spring/jdbc.properties"/> 

And your properties will be available in the Spring file and don't forget to add the namespace: xmlns:p="http://www.springframework.org/schema/p"

0
source

I think you are missing a prefix to instruct Spring how to try to load properties. I think your definition should be:

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

Note the addition of the file prefix :.

0
source

Source: https://habr.com/ru/post/1498561/


All Articles