Donatello
Donatello

Reputation: 353

xml Spring config using values from properties files

For DI in xml via setters using values from properties files I can use:

<beans>
..
   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="props.properties"/>
    </bean>
    <bean id="first" class="..">
        <property name="name" value="${values.name}"/>
    </bean>

</beans>

But PropertyPlaceholderConfigurer class is deprecated.

I have tried to use

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

instead of It but It didn't work

prop.properties in situated in resources, error message is no declaration can be found for element 'context:property-placeholder'

Upvotes: 0

Views: 1110

Answers (1)

Shailesh Chandra
Shailesh Chandra

Reputation: 2340

As per Javadocs of PropertyPlaceholderConfigurer

as of 5.2; use org.springframework.context.support.PropertySourcesPlaceholderConfigurer instead which is more flexible through taking advantage of the Environment and PropertySource mechanism

you can load property like below, you have to include namespace & schemalocation in your beans tags. copy the beans tag from below

 <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
            xmlns:util="http://www.springframework.org/schema/util"
            xsi:schemaLocation="
              http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
              http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">


 <context:property-placeholder location="classpath:foo.properties,classpath:bar.properties"/>

    </bean>

</beans>

Upvotes: 2

Related Questions