[英]loading Spring profiles from database
One may dynamically load Spring profiles by implementing ApplicationContextInitializer and adding Spring profiles to the environment. 通过实现ApplicationContextInitializer并将Spring配置文件添加到环境中,可以动态加载Spring配置文件。
The problem is that in one of projects I am helping in they are using properties stored in database. 问题是在我正在帮助的一个项目中,他们正在使用存储在数据库中的属性。 How to load property representing additional active Spring profiles from the database and than injecting it to the environment.
如何从数据库中加载表示其他活动Spring概要文件的属性,然后将其注入环境。 In ApplicationContextInitializer I cannot use Spring beans because the application context is not yet fully initialized.
在ApplicationContextInitializer中,由于应用程序上下文尚未完全初始化,因此我无法使用Spring Bean。 Is low level access to the database my only option?
低级访问数据库是我唯一的选择吗?
Spring PropertyPlaceholderConfigurer
needs to be initialized with a location property that is the properties file you want. Spring
PropertyPlaceholderConfigurer
需要使用一个位置属性初始化,该属性是所需的属性文件。 But, this class can be also initialized with a java.util.Properties
object. 但是,也可以使用
java.util.Properties
对象初始化该类。
From deprecated project Spring-Modules, you can find here this class that implements InitializingBean
and FactoryBean
Spring clases, such way it behaves like a normal java.util.Properties
object which can be passed on to PropertyPlaceholderConfigurer.setProperties()
method. 从弃用项目Spring的模块,你可以在这里找到这个类实现
InitializingBean
和FactoryBean
春天clases,它的行为像一个正常的这样的方式java.util.Properties
对象,它可以被传递到PropertyPlaceholderConfigurer.setProperties()
方法。
This way you can take advantage of org.apache.commons.configuration.DatabaseConfiguration
which acts like a Properties object but reading properties from a database. 这样,您就可以利用
org.apache.commons.configuration.DatabaseConfiguration
,该属性的行为类似于Properties对象,但从数据库中读取属性。 For example, think of this bean configuration: 例如,考虑以下bean配置:
<bean
name="MyDatabaseConfiguration"
class="org.apache.commons.configuration.DatabaseConfiguration">
<constructor-arg type="javax.sql.DataSource" ref="someDataSource"/>
<constructor-arg index="1" value="SCHEMA.PROPERTIES_TABLE"/>
<constructor-arg index="2" value="KEY"/>
<constructor-arg index="3" value="VALUE"/>
</bean>
Here, arg 1 is the table that contains the properties, arg 2 the key column ans arg 2 the value column. 在这里,arg 1是包含属性的表,arg 2是键列,而arg 2是值列。
So, you can create your custom class, very similar to CommonsConfigurationFactoryBean
and use in this way: 因此,您可以创建与
CommonsConfigurationFactoryBean
非常相似的自定义类,并以这种方式使用:
<bean
name="PropertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="MyCustomClass"/>
</bean>
Where MyCustomClass
is the class you will use to wrap MyDatabaseConfiguration
. 其中
MyCustomClass
是用于包装MyDatabaseConfiguration
。
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.