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


当前回答

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

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

从调试队长页面

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

其他回答

一个可能的解决方案是声明第二个bean,它从相同的属性文件中读取:

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

<util:properties id="appProperties" location="classpath:/WEB-INF/app.properties"/>

名为‘appProperties’的bean类型是java.util.Properties,可以使用上面所示的@Resource属性注入依赖项。

如果你需要更多的灵活性的配置,尝试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

谨致友好问候, 哈拉尔德

另一种选择是添加如下所示的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两次。

正如前面提到的@Value做的工作,它是相当灵活的,因为你可以在里面有弹簧EL。

这里有一些例子,可能会有帮助:

//Build and array from comma separated parameters 
//Like currency.codes.list=10,11,12,13
@Value("#{'${currency.codes.list}'.split(',')}") 
private List<String> currencyTypes;

另一个是从列表中获得一组

//If you have a list of some objects like (List<BranchVO>) 
//and the BranchVO has areaCode,cityCode,...
//You can easily make a set or areaCodes as below
@Value("#{BranchList.![areaCode]}") 
private Set<String> areas;

还可以为基本类型设置值。

@Value("${amount.limit}")
private int amountLimit;

你可以调用静态方法:

@Value("#{T(foo.bar).isSecurityEnabled()}")
private boolean securityEnabled;

你可以有逻辑

@Value("#{T(foo.bar).isSecurityEnabled() ? '${security.logo.path}' : '${default.logo.path}'}")
private String logoPath;

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