Bohne
Bohne

Reputation: 4047

set location of PropertyPlaceholderConfigurer in Spring ApplicationContext relative to applicationContext.xml

I would like to know: Is it possible to set the location of a property-file for PropertyPlaceholderConfigurer in the spring applicationContext.xml relative to the xml-file itself?

So when applicationContext.xml and dataSource.properties files are in the same directory: will something like this work, or wouldn't Spring find the file, because i have to modify the location-property value?

<bean id="dataSourceProperties"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="location" value="dataSource.properties" />   
</bean>

Thank you in advance!

Upvotes: 2

Views: 4047

Answers (2)

Arpit Aggarwal
Arpit Aggarwal

Reputation: 29276

Put your dataSourceProperties.properties file in the resources folder, modify your dataSourceProperties bean as follows:

<bean id="dataSourceProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="locations">
    <list>
      <value>classpath*:dataSource.properties</value>
    </list>
  </property>
</bean>

And get the property out of it in your class definition as follows:

@WebService
@Component
public class MyService{

      @Value("${databaseDriver}")
      private String databaseDriver;

      ....

}

Moreover, get complete property file, as follows:

 @Component
 public class MyService{

     @Resource(name="dataSourceProperties")
     private Properties dataSourceProperties;

     ....

 }

dataSource.properties:

databaseDriver=oracle.jdbc.driver.OracleDriver

Upvotes: 2

Bohne
Bohne

Reputation: 4047

No this will not work.

The "value" for the location will not start with the location of applicationContext.xml as "root-directory". It will use the directory from where the spring application was started.

Maybe anyone other will have this question too :P

Upvotes: -1

Related Questions