简体   繁体   English

来自数据库和属性的Spring配置属性

[英]Spring config properties from database and properties

I asked a similar question, but based on the responses, I did a bad job describing what I am after. 我问了一个类似的问题,但是根据回答,我在描述我的追求方面做得很差。 I have a spring 4 webapp that loads properties from a properties file. 我有一个Spring 4 Webapp,可以从属性文件加载属性。 We consume those properties both via the "${proper.name"} expressions in spring, as well as by injecting a properties object into some of our classes. 我们既可以在春季通过“ $ {proper.name”}表达式来使用这些属性,也可以通过将属性对象注入到某些类中来使用这些属性。

We want to move most of the properties to a database table and make them reloadable. 我们希望将大多数属性移动到数据库表中,并使其可重装。 However, a few need to stay in local properties, potentially overriding the database setting. 但是,有一些需要保留在本地属性中,从而有可能覆盖数据库设置。 These should also be loaded dynamically after the app is running. 在应用程序运行后,还应该动态加载这些文件。

I know that once a particular bean is injected, it won't get reloaded, that doesn't concern me, it's up to that module to handle that. 我知道一旦注入了特定的bean,就不会重新加载它,这与我无关,由该模块来处理。 But I am having trouble getting the behavior I want. 但是我很难得到我想要的行为。 In particular, I have implemented an AbstractConfiguration from apache commons configuration to get the dual source and overriding I am after. 特别是,我已经从apache commons配置中实现了AbstractConfiguration,以获取双重源并覆盖我所追求的。 But while it works for injecting the properties object, expressions loaded with "${prop.name}" don't work at all. 但是,尽管它可以注入属性对象,但使用“ $ {prop.name}”加载的表达式根本不起作用。

How can I get them to work? 我如何让他们工作? Did I override the wrong thing? 我是否覆盖了错误的内容? Is it just some config detail? 这只是一些配置细节吗?

<bean id="sysProperties" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="databaseConfigurator" />
    <property name="targetMethod" value="getProperties"/>
</bean> 

<bean id="databaseConfigurator" class="my.util.config.MyDatabaseConfigurator">
    <property name="datasource" ref="dataSource" />
    <property name="propertyFile" value="/WEB-INF/my.properties" />
    <property name="applicationName" value="ThisApp" />
</bean>

<bean id="dbConfigFactory" class="org.apache.commons.configuration.ConfigurationConverter" factory-method="getProperties">
    <constructor-arg ref="databaseConfigurator" />
</bean> 

I haven't tested this, but I think it might work. 我尚未对此进行测试,但我认为它可能会起作用。

<bean id="sysProperties" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="databaseConfigurator" />
    <property name="targetMethod" value="getProperties"/>
</bean> 

<bean id="databaseConfigurator" class="my.util.config.MyDatabaseConfigurator">
    <property name="datasource" ref="dataSource" />
    <property name="propertyFile" value="/WEB-INF/my.properties" />
    <property name="applicationName" value="ThisApp" />
</bean>



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

<bean name="CommonsConfigurationFactoryBean" class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
       <constructor-arg ref="databaseConfigurator"/>
</bean>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM