Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing a file via classpath in spring context

Tags:

java

spring

ide

I'm trying to set org.springframework.beans.factory.config.PropertyPlaceholderConfigurer spring bean, I have jdbc.properties in src/main/config .. when I put this file in src/main/resources and put classpath: in value my app deploys successfuly.

This works when jdbc.properties is located in src/main/resources

 <property name="location" value="classpath:jdbc.properties" />

However I'm required to put any configuration inside src/main/config , how do I point springs towards this location in the right way?

like image 445
Gandalf StormCrow Avatar asked Mar 26 '10 16:03

Gandalf StormCrow


Video Answer


3 Answers

This is a classpath issue, not a Spring issue. Add src/main/config to your classpath and it will work. In Eclipse, this means adding it to the project Build Path->Source.

like image 142
stevedbrown Avatar answered Oct 13 '22 20:10

stevedbrown


The right answer to this is given by @matt b ,

"What I am referring to is the fact that when you package the application, the config files are not packaged in a folder named src/main/config in the packaged file (jar/war/etc.). Therefore your answer only works when you run the application within the source code, or when src/main/config is in the classpath (which it is not by default). The correct prefix is to use classpath: or another location."

In this question :

Trying to setup externalizing properties in spring

like image 28
ant Avatar answered Oct 13 '22 21:10

ant


If it is a maven project, you can add

<resources>            
        <resource>
            <directory>src/main/config</directory>
            <filtering>false</filtering>
        </resource>
<resources>

to you <build> in your pom.xml

like image 32
Karen Avatar answered Oct 13 '22 20:10

Karen