How can I inject a property value into a Spring Bean which was configured using annotations?

I have a bunch of Spring beans which are picked up from the classpath via annotations, e.g.

@Repository("personDao")
public class PersonDaoImpl extends AbstractDaoImpl implements PersonDao {
    // Implementation omitted
}

In the Spring XML file, there’s a PropertyPlaceholderConfigurer defined:

<bean id="propertyConfigurer" 
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="/WEB-INF/app.properties" />
</bean> 

I want to inject one of the properties from app.properites into the bean shown above. I can’t simply do something like

<bean class="com.example.PersonDaoImpl">
    <property name="maxResults" value="${results.max}"/>
</bean>

Because PersonDaoImpl does not feature in the Spring XML file (it is picked up from the classpath via annotations). I’ve got as far as the following:

@Repository("personDao")
public class PersonDaoImpl extends AbstractDaoImpl implements PersonDao {

    @Resource(name = "propertyConfigurer")
    protected void setProperties(PropertyPlaceholderConfigurer ppc) {
    // Now how do I access results.max? 
    }
}

But it’s not clear to me how I access the property I’m interested in from ppc?

18 Answers
18

Leave a Comment