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


当前回答

我认为向bean中注入属性最方便的方法是setter方法。

例子:

package org.some.beans;

public class MyBean {
    Long id;
    String name;

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

Bean xml定义:

<bean id="Bean1" class="org.some.beans.MyBean">
    <property name="id" value="1"/>
    <property name="name" value="MyBean"/>
</bean>

对于每个命名属性方法setProperty(value)将被调用。

如果需要基于一个实现的多个bean,这种方法尤其有用。

例如,如果我们在xml中再定义一个bean:

<bean id="Bean2" class="org.some.beans.MyBean">
    <property name="id" value="2"/>
    <property name="name" value="EnotherBean"/>
</bean>

然后像这样编码:

MyBean b1 = appContext.getBean("Bean1");
System.out.println("Bean id = " + b1.getId() + " name = " + b1.getName());
MyBean b2 = appContext.getBean("Bean2");
System.out.println("Bean id = " + b2.getId() + " name = " + b2.getName());

将打印

Bean id = 1 name = MyBean
Bean id = 2 name = AnotherBean

所以,在你的例子中,它应该是这样的:

@Repository("personDao")
public class PersonDaoImpl extends AbstractDaoImpl implements PersonDao {

    Long maxResults;

    public void setMaxResults(Long maxResults) {
        this.maxResults = maxResults;
    }

    // Now use maxResults value in your code, it will be injected on Bean creation
    public void someMethod(Long results) {
        if (results < maxResults) {
            ...
        }
    }
}

其他回答

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

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

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

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

如果你需要更多的灵活性的配置,尝试Settings4jPlaceholderConfigurer: http://settings4j.sourceforge.net/currentrelease/configSpringPlaceholder.html

在我们的应用程序中,我们使用:

配置PreProd-和Prod-System的首选项 “mvn jetty:run”的首选项和JNDI环境变量(JNDI覆盖首选项) UnitTests的系统属性(@BeforeClass注释)

首先检查key-value-Source的默认顺序如下所示: http://settings4j.sourceforge.net/currentrelease/configDefault.html 它可以在类路径中使用settings4j.xml(精确到log4j.xml)进行定制。

让我知道你的意见:settings4j-user@lists.sourceforge.net

谨致友好问候, 哈拉尔德

使用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 =根

我认为向bean中注入属性最方便的方法是setter方法。

例子:

package org.some.beans;

public class MyBean {
    Long id;
    String name;

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

Bean xml定义:

<bean id="Bean1" class="org.some.beans.MyBean">
    <property name="id" value="1"/>
    <property name="name" value="MyBean"/>
</bean>

对于每个命名属性方法setProperty(value)将被调用。

如果需要基于一个实现的多个bean,这种方法尤其有用。

例如,如果我们在xml中再定义一个bean:

<bean id="Bean2" class="org.some.beans.MyBean">
    <property name="id" value="2"/>
    <property name="name" value="EnotherBean"/>
</bean>

然后像这样编码:

MyBean b1 = appContext.getBean("Bean1");
System.out.println("Bean id = " + b1.getId() + " name = " + b1.getName());
MyBean b2 = appContext.getBean("Bean2");
System.out.println("Bean id = " + b2.getId() + " name = " + b2.getName());

将打印

Bean id = 1 name = MyBean
Bean id = 2 name = AnotherBean

所以,在你的例子中,它应该是这样的:

@Repository("personDao")
public class PersonDaoImpl extends AbstractDaoImpl implements PersonDao {

    Long maxResults;

    public void setMaxResults(Long maxResults) {
        this.maxResults = maxResults;
    }

    // Now use maxResults value in your code, it will be injected on Bean creation
    public void someMethod(Long results) {
        if (results < maxResults) {
            ...
        }
    }
}