我有一堆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. 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访问我感兴趣的财产?


当前回答

另一种选择是添加如下所示的appProperties bean:

<bean id="propertyConfigurer"   
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="/WEB-INF/app.properties" />
</bean> 


<bean id="appProperties" 
          class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="singleton" value="true"/>

        <property name="properties">
                <props>
                        <prop key="results.max">${results.max}</prop>
                </props>
        </property>
</bean>

检索时,这个bean可以转换为java.util.Properties,其中包含一个名为results的属性。Max,它的值从app.properties中读取。同样,这个bean可以通过@Resource注释被注入到任何类中(作为java.util.Properties的一个实例)。

就我个人而言,我更喜欢这个解决方案(与我提出的其他解决方案相比),因为您可以精确地限制appProperties暴露哪些属性,并且不需要读取app.properties两次。

其他回答

如果您一直在使用Spring 2.5,您可以为每个属性定义一个bean,并使用限定符注入它们。是这样的:

  <bean id="someFile" class="java.io.File">
    <constructor-arg value="${someFile}"/>
  </bean>

and

@Service
public class Thing
      public Thing(@Qualifier("someFile") File someFile) {
...

它不是超级可读的,但它完成了工作。

春天道: 私人@ value (" $ {propertyName} ") 字符串propertyField;

是一种使用Spring的“PropertyPlaceholderConfigurer”类注入值的新方法。 另一种方法是打电话

java.util.Properties props = System.getProperties().getProperty("propertyName");

注意:对于@Value,你不能使用static propertyField,它只能是非静态的,否则它会返回null。为了解决这个问题,为静态字段创建了一个非静态setter,并在该setter之上应用@Value。

对我来说,这是@Lucky的答案,特别是那句台词

AutowiredFakaSource fakeDataSource = ctx.getBean(AutowiredFakaSource.class);

从调试队长页面

这解决了我的问题。我有一个从命令行运行的基于applicationcontext的应用程序,从SO上的许多注释判断,Spring将这些应用程序以不同的方式连接到基于mvc的应用程序。

将属性值自动装配到Spring bean中:

大多数人都知道,可以使用@Autowired告诉Spring在加载应用程序上下文时将一个对象注入到另一个对象中。一个鲜为人知的信息是,您还可以使用@Value注释将属性文件中的值注入到bean的属性中。 更多信息请看这篇文章…

Spring 3.0的新功能 ||自动装配bean值 ||在春季自动装配属性值

我需要有两个属性文件,一个用于生产,一个用于开发(将不会部署)。

要同时拥有一个可以自动连接的属性Bean和一个PropertyConfigurer,你可以这样写:

<bean id="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="singleton" value="true" />

    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:live.properties</value>
            <value>classpath:development.properties</value>
        </list>
    </property>
</bean>

并在propertyconfigururer中引用Properties Bean

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="properties" ref="appProperties" />
</bean>