我有一堆SpringBean,它们是通过注释从类路径中获取的,例如.

@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> 

我想从应用程序中注入一个属性.如上图所示,在豆子中加入适当的淀粉.我不能简单地做这样的事情

<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开始进入我感兴趣的房产?

推荐答案

您可以在Spring3中使用EL支持来做到这一点.示例:

@Value("#{systemProperties.databaseName}")
public void setDatabaseName(String dbName) { ... }

@Value("#{strategyBean.databaseKeyGenerator}")
public void setKeyGenerator(KeyGenerator kg) { ... }

systemProperties是隐式对象,strategyBean是bean名称.

再举一个例子,当您想要从一个Properties对象中抓取一个属性时,它会起作用.它还显示您可以将@Value应用于字段:

@Value("#{myProperties['github.oauth.clientId']}")
private String githubOauthClientId;

这是我写的关于这一点的blog post美元,以获得更多信息.

Java相关问答推荐

PostgreSQL货币兑换率查询

当耗时的代码完成时,Circular ProgressIndicator显示得太晚

如何使用解析器组合子解析Java数组类型签名?

CAMEL 4中的SAXParseException

通过合并Akka Streams中的多个慢源保持订购

如何才能使我的程序不会要求两次输入?

Javadoc在方法摘要中省略方法

如何在JavaFX循环中完美地制作一个AudioClip/MediaPlayer?

当b是一个字节并且在Java中值为-1时,为什么b>;>;>;1总是等于-1?

在Spring Boot JPA for MySQL中为我的所有类创建Bean时出错?

具有多个模式的DateTimeForMatter的LocalDate.parse失败

Java.time.OffsetDateTime的SQL Server数据库列类型是什么?

不能在 map 上移除折线

在Java中使用StorageReference将数据从Firebase存储添加到数组列表

Cucumber java-maven-示例表-未定义一步

让标签占用JavaFX中HBox的所有可用空间

try 添加;按流派搜索;在Web应用程序上,但没有;I don’我不知道;It’这个代码错了

如何在更改分辨率时将鼠标坐标计算为世界坐标

ANTLR 接受特殊字符,例如 .标识符或表达式中的(点)和 ,(逗号)

如何在 WebSphere Application Server 内的托管线程上运行 BatchEE 作业(job)?