如何将属性值注入使用注释配置的Spring Bean?

如何将属性值注入使用注释配置的Spring Bean?

我有一堆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


一只斗牛犬
浏览 845回答 3
3回答

catspeake

有一个新的注释@Value在春季3.0.0M3。@Value不仅支持#{...}表达式,还支持${...}占位符
打开App,查看更多内容
随时随地看视频慕课网APP