如何将属性值注入使用注解配置的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
?
-
你可以在Spring 3中使用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;
-
Java如何将属性值注入使用注解配置的Spring Bean中?
2021-02-02 关注 0 浏览73 1答案
-
如何将属性值注入使用注释配置的 Spring Bean?
2022-05-26 关注 0 浏览14 1答案
-
如何将Class值设置为Spring bean属性?
2021-01-30 关注 0 浏览73 1答案
-
集成jsf,spring,hibernate。如何将Spring bean注入JSF管理的bean中?
2021-02-01 关注 0 浏览122 1答案
-
如何将Maven配置文件值注入属性文件
2021-01-30 关注 0 浏览89 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答案
-
Spring 中如何使用注解来配置 Bean?有哪些相关的 注解
2022-01-22 关注 0 浏览82 1答案
-
Spring:如何将HttpServletRequest注入到请求范围的bean中?
2021-02-02 关注 0 浏览102 1答案
-
如何从Main类将属性注入到Spring bean中
2021-01-29 关注 0 浏览89 1答案