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


当前回答

就我个人而言,我喜欢Spring 3.0文档中的这种新方式:

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

没有getter或setter !

通过配置加载属性:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="classpath:propertyFile.properties" name="propertiesBean"/>

更让我高兴的是,我甚至可以在IntelliJ中控制单击EL表达式,它将我带到属性定义!

还有完全非xml的版本:

@PropertySource("classpath:propertyFile.properties")
public class AppConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

其他回答

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

谨致友好问候, 哈拉尔德

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

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

一个可能的解决方案是声明第二个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属性注入依赖项。

您可以在Spring 3中使用EL支持来实现这一点。例子:

@Value("#{systemProperties.databaseName}")
public void setDatabaseName(String dbName) { ... }

@Value("#{strategyBean.databaseKeyGenerator}")
public void setKeyGenerator(KeyGenerator kg) { ... }

systemProperties是一个隐式对象,strategyBean是一个bean名称。

再举一个例子,当您想从Properties对象中获取属性时,它可以工作。它还显示了你可以对字段应用@Value:

@Value("#{myProperties['github.oauth.clientId']}")
private String githubOauthClientId;

这是我写的一篇关于这方面的博客文章,以获得更多信息。

就我个人而言,我喜欢Spring 3.0文档中的这种新方式:

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

没有getter或setter !

通过配置加载属性:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="classpath:propertyFile.properties" name="propertiesBean"/>

更让我高兴的是,我甚至可以在IntelliJ中控制单击EL表达式,它将我带到属性定义!

还有完全非xml的版本:

@PropertySource("classpath:propertyFile.properties")
public class AppConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }