如何将属性值注入到使用注释配置的Spring Bean中? [英] How can I inject a property value into a Spring Bean which was configured using annotations?
问题描述
@Repository(personDao )
public class PersonDaoImpl extends AbstractDaoImpl implements PersonDao {
//实现省略
}
在Spring XML文件中,有一个 PropertyPlaceholderConfigurer
< bean id =propertyConfigurer
class =org.springframework.beans.factory.config.PropertyPlaceholderConfigurer>
< property name =locationvalue =/ WEB-INF / app.properties/>
< / bean>
我想将app.properites中的一个属性注入上面显示的bean。我不能简单地做一些像
< bean class =com.example.PersonDaoImpl >
< property name =maxResultsvalue =$ {results.max}/>
< / bean>
由于PersonDaoImpl在Spring XML文件中不起作用(它通过注释从类路径中获取) 。我已经达到以下目的:
@Repository(personDao)
public class PersonDaoImpl extends AbstractDaoImpl实现PersonDao {
@Resource(name =propertyConfigurer)
protected void setProperties(PropertyPlaceholderConfigurer ppc){
//现在我如何访问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名称。
另一个例子,当您想从属性
对象。它还显示您可以将 @Value
应用于字段:
@ Value(#{myProperties ['github.oauth.clientId']})
private String githubOauthClientId;
这是一个博客我写了一些更多的信息。
I have a bunch of Spring beans which are picked up from the classpath via annotations, e.g.
@Repository("personDao")
public class PersonDaoImpl extends AbstractDaoImpl implements PersonDao {
// Implementation omitted
}
In the Spring XML file, there's a PropertyPlaceholderConfigurer defined:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/app.properties" />
</bean>
I want to inject one of the properties from app.properites into the bean shown above. I can't simply do something like
<bean class="com.example.PersonDaoImpl">
<property name="maxResults" value="${results.max}"/>
</bean>
Because PersonDaoImpl does not feature in the Spring XML file (it is picked up from the classpath via annotations). I've got as far as the following:
@Repository("personDao")
public class PersonDaoImpl extends AbstractDaoImpl implements PersonDao {
@Resource(name = "propertyConfigurer")
protected void setProperties(PropertyPlaceholderConfigurer ppc) {
// Now how do I access results.max?
}
}
But it's not clear to me how I access the property I'm interested in from ppc
?
You can do this in Spring 3 using EL support. Example:
@Value("#{systemProperties.databaseName}")
public void setDatabaseName(String dbName) { ... }
@Value("#{strategyBean.databaseKeyGenerator}")
public void setKeyGenerator(KeyGenerator kg) { ... }
systemProperties
is an implicit object and strategyBean
is a bean name.
One more example, which works when you want to grab a property from a Properties
object. It also shows that you can apply @Value
to fields:
@Value("#{myProperties['github.oauth.clientId']}")
private String githubOauthClientId;
Here is a blog post I wrote about this for a little more info.
这篇关于如何将属性值注入到使用注释配置的Spring Bean中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!