如何将属性值注入使用注释配置的 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
?
-
您可以使用 EL 支持在 Spring 3 中执行此操作。例子:
@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;
这是我写的一篇博文,以获取更多信息。
-
如何将属性值注入使用注解配置的Spring Bean中?
2021-02-02 关注 0 浏览290 1答案
-
Java如何将属性值注入使用注解配置的Spring Bean中?
2021-02-02 关注 0 浏览73 1答案
-
如何将Class值设置为Spring bean属性?
2021-01-30 关注 0 浏览73 1答案
-
如何使用注释将值注入到bean构造函数
2021-02-02 关注 0 浏览71 1答案
-
如何将Maven配置文件值注入属性文件
2021-01-30 关注 0 浏览89 1答案
-
集成jsf,spring,hibernate。如何将Spring bean注入JSF管理的bean中?
2021-02-01 关注 0 浏览122 1答案
-
如何在Spring Boot中将配置属性注入到Spring Retry注释中?
2021-02-02 关注 0 浏览89 1答案
-
spring:如何将String bean注入构造函数?
2021-02-02 关注 0 浏览104 1答案
-
如何将Spring bean注入到jsp 2.0 SimpleTag中?
2021-01-30 关注 0 浏览113 1答案
-
如何将Spring bean注入到jsp 2.0 SimpleTag中?
2021-02-02 关注 0 浏览78 1答案