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


当前回答

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

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

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

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

其他回答

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

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

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

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

<上下文:property-placeholder…/>是等价于PropertyPlaceholderConfigurer的XML。

例子: 中

<context:property-placeholder location="classpath:test.properties"/>  

组件类

 private @Value("${propertyName}") String propertyField;

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

要同时拥有一个可以自动连接的属性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>

使用Spring的“PropertyPlaceholderConfigurer”类

一个简单的示例,显示作为bean的属性动态读取的属性文件

<bean id="placeholderConfig"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>/WEB-INF/classes/config_properties/dev/database.properties</value>
        </list>
    </property> 
</bean>

<bean id="devDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${dev.app.jdbc.driver}"/>
    <property name="jdbcUrl" value="${dev.app.jdbc.url}"/>
    <property name="user" value="${dev.app.jdbc.username}"/>
    <property name="password" value="${dev.app.jdbc.password}"/>
    <property name="acquireIncrement" value="3"/>
    <property name="minPoolSize" value="5"/>
    <property name="maxPoolSize" value="10"/>
    <property name="maxStatementsPerConnection" value="11000"/>
    <property name="numHelperThreads" value="8"/>
    <property name="idleConnectionTestPeriod" value="300"/>
    <property name="preferredTestQuery" value="SELECT 0"/>
</bean> 

属性文件

dev.app.jdbc.driver = com.mysql.jdbc.Driver

dev.app.jdbc.url = jdbc: mysql: / / localhost: 3306 / addvertisement

dev.app.jdbc.username =根

dev.app.jdbc.password =根

在Spring 3.0.0M3中有一个新的注释@Value。@Value不仅支持#{…}表达式但是${…以及占位符