【发布时间】:2010-09-23 23:30:12
【问题描述】:
我有一堆 Spring bean,它们是通过注释从类路径中提取的,例如
@Repository("personDao")
public class PersonDaoImpl extends AbstractDaoImpl implements PersonDao {
// Implementation omitted
}
在 Spring XML 文件中,定义了 PropertyPlaceholderConfigurer:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/app.properties" />
</bean>
我想将 app.properites 中的属性之一注入到上面显示的 bean 中。我不能简单地做类似的事情
<bean class="com.example.PersonDaoImpl">
<property name="maxResults" value="${results.max}"/>
</bean>
因为 PersonDaoImpl 在 Spring XML 文件中没有特性(它是通过注释从类路径中提取的)。我有以下几点:
@Repository("personDao")
public class PersonDaoImpl extends AbstractDaoImpl implements PersonDao {
@Resource(name = "propertyConfigurer")
protected void setProperties(PropertyPlaceholderConfigurer ppc) {
// Now how do I access results.max?
}
}
但我不清楚如何从ppc 访问我感兴趣的属性?
【问题讨论】:
-
我问过基本相同的问题,尽管情况略有不同:stackoverflow.com/questions/310271/…。到目前为止,还没有人能够回答。
-
请注意,从 Spring 3.1 开始,
PropertyPlaceholderConfigurer不再是推荐的类。更喜欢PropertySourcesPlaceholderConfigurer。在任何情况下,您都可以使用较短的 XML 定义<context:property-placeholder />。
标签: java spring dependency-injection