java 来自数据库的Spring配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3913064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring configuration from database
提问by DD.
We have an application which is deployed 120 times with slightly different configurations for each. We would like the configuration to be stored in the database for auditing and management purposes.
我们有一个应用程序部署了 120 次,每个应用程序的配置略有不同。我们希望将配置存储在数据库中以进行审计和管理。
How can you instantiate Spring beans directly from the database without using XML?
如何在不使用 XML 的情况下直接从数据库实例化 Spring bean?
Thanks
谢谢
回答by Bozho
You can't have zero XML config (unless you use JavaConfig, which doesn't make things different in your case) . You can externalize some of it to the database, and use a custom PropertyPlaceholderConfigurer
. See this articleon how to achieve this.
您不能有零 XML 配置(除非您使用 JavaConfig,这不会使您的情况有所不同)。您可以将其中的一些外部化到数据库中,并使用自定义PropertyPlaceholderConfigurer
. 有关如何实现这一目标,请参阅本文。
回答by mk_
In case of Web Application:
如果是 Web 应用程序:
Write custom ServletContextListener implementation that populates Properties instance with the database values on application startup and passes it to the Spring's PropertyPlaceholderConfigurer. See this post for complete working example: http://blog.javaforge.net/post/31720600427/configuring-spring-based-web-application-from-database
编写自定义 ServletContextListener 实现,在应用程序启动时使用数据库值填充 Properties 实例,并将其传递给 Spring 的 PropertyPlaceholderConfigurer。有关完整的工作示例,请参阅此帖子:http: //blog.javaforge.net/post/31720600427/configuring-spring-based-web-application-from-database
回答by skaffman
@Bozho's suggestion is almost certainly the most practical solution, especially if the differences between the deployments is minimal and can be expressed via simple scalar properties.
@Bozho 的建议几乎可以肯定是最实用的解决方案,特别是如果部署之间的差异很小并且可以通过简单的标量属性表示。
The alternative is to write your own BeanFactory
implementation. This is a non-trivial exercise, and you want to be sure that it's what you need. A good starting point would be to look at the source for XmlBeanFactory
, and then write your own (DatabaseBeanFactory
, perhaps) which does something similar, but fetching the bean definitions from the database, rather than from local XML files.
另一种方法是编写自己的BeanFactory
实现。这是一项重要的练习,您要确保它是您所需要的。一个好的起点是查看 的源代码XmlBeanFactory
,然后编写您自己的(DatabaseBeanFactory
也许),它执行类似的操作,但从数据库中获取 bean 定义,而不是从本地 XML 文件中获取。
It's going to be quite a lot of extra work, though.
不过,这将是相当多的额外工作。
回答by axtavt
There are some options which are simplier than skaffman's suggestion:
有一些选项比 skaffman 的建议更简单:
If your configuration is stored in the database in XML form, you can implement a custom resource fetching strategy by overriding
AbstractApplicationContext.getResource()
, so that you can load XML configs from the database. See herefor some sample code. Using this approach you can also generate XML config on the fly.If your configuration is stored in the "disassembled" form, you can build
BeanDefinition
s and add them to theBeanDefinitionRegistry
during initialization of the context using one of the following approaches:- Implement a namespace extension
- Implement a
BeanFactoryPostProcessor
(you will need to downcastConfigurableListableBeanFactory
toBeanDefinitionRegistry
, which work for most of application contexts types) - Since Spring 3.0.1 the previous approach was rectified by introduction of
BeanDefinitionRegistryPostProcessor
如果您的配置以 XML 形式存储在数据库中,您可以通过覆盖实现自定义资源获取策略
AbstractApplicationContext.getResource()
,以便您可以从数据库加载 XML 配置。有关一些示例代码,请参见此处。使用这种方法,您还可以即时生成 XML 配置。如果您的配置以“反汇编”形式存储,则可以使用以下方法之一构建
BeanDefinition
s 并将它们添加到BeanDefinitionRegistry
上下文初始化期间:- 实现命名空间扩展
- 实现 a
BeanFactoryPostProcessor
(您需要向下转型ConfigurableListableBeanFactory
到BeanDefinitionRegistry
,这适用于大多数应用程序上下文类型) - 从 Spring 3.0.1 开始,之前的方法通过引入
BeanDefinitionRegistryPostProcessor