[英]Can I dynamically load additional Spring configuration files into an existing WebApplicationContext?
Upon starting my webapp within Tomcat 6.0.18, I bootstrap Spring with only what is necessary to initialize the system -- namely, for now, database migrations. 在Tomcat 6.0.18中启动我的webapp后,我只使用初始化系统所需的内容来引导Spring - 即,目前,数据库迁移。 I do not want any part of the system to load until the migrations have successfully completed.
在迁移成功完成之前,我不希望加载系统的任何部分。 This prevents the other beans from having to wait on the migrations to complete before operating, or even instantiating.
这可以防止其他bean在操作或甚至实例化之前必须等待迁移完成。
I have a startup-appcontext.xml configured with a dbMigrationDAO, a startupManager which is a ThreadPoolExecutor , and lastly, a FullSystemLauch bean. 我有一个配置了dbMigrationDAO的startup-appcontext.xml,一个是ThreadPoolExecutor的startupManager,最后是一个FullSystemLauch bean。 I pass a list of configuration locations to the FullSystemLaunch bean via setter injection.
我通过setter注入将配置位置列表传递给FullSystemLaunch bean。 The FullSystemLaunch bean implements ServletContextAware , gets a reference to the current WebApplicationContext and thus I can have a ConfigurableListableBeanFactory .
FullSystemLaunch bean实现ServletContextAware ,获取对当前WebApplicationContext的引用,因此我可以拥有ConfigurableListableBeanFactory 。 Unfortunately, this bean factory isConfigurationFrozen() returns true, so by calling beanFactory.setConfigLocations(configLocations) has no effect.
不幸的是,这个bean工厂isConfigurationFrozen()返回true,所以通过调用beanFactory.setConfigLocations(configLocations)没有任何效果。
Can I accomplish this or is Spring preventing me from doing so because it's a bit out of the ordinary? 我可以做到这一点,还是Spring阻止我这样做,因为它有点与众不同? It seems reasonable if understood, but also a bit dangerous.
如果理解这似乎是合理的,但也有点危险。 And yes, I'm willing to blow away the current context b/c the currently loaded Singletons are not needed once initialization is complete.
是的,我愿意吹掉当前的上下文b / c初始化完成后不需要当前加载的单例。
Thank you for the help. 感谢您的帮助。
My opinion would be to allow Spring to initialise your beans is it sees fit - in the order of their declared dependencies. 我的意见是允许Spring初始化你认为合适的bean - 按照它们声明的依赖关系的顺序。
If you need database migrations there are a couple of patterns to have them run first: 如果您需要数据库迁移,可以先使用几种模式运行它们:
The advantage is clear: simplicity. 优点很明显:简单。
You could use the existing context as parent context for the other contexts, although I doubt that you could replace the existing WebApplicationContext. 您可以将现有上下文用作其他上下文的父上下文,但我怀疑您是否可以替换现有的WebApplicationContext。
If you use EAR - WAR packaging, you get this out-of-the-box (sort of) by loading an application context from the EAR and then adding one in the WAR. 如果您使用EAR-WAR打包,则可以通过从EAR加载应用程序上下文然后在WAR中添加一个应用程序上下文来获得开箱即用(类型)。
Not sure whether this is applicable in your situation. 不确定这是否适用于您的情况。
懒惰初始化可以替代你想要实现的目标吗?
可能的XmlBeanDefinitionReader可以帮到你吗?
you can upcat the WebApplicatonContext to ConfigurableWebApplicationContext then use the setConfigurations method. 您可以将WebApplicatonContext上传到ConfigurableWebApplicationContext,然后使用setConfigurations方法。
dont forget refresh; 不要忘记刷新;
There was the same task and I created two contexts: startUpContext.xml
and applicationContext.xml
. 有相同的任务,我创建了两个上下文:
startUpContext.xml
和applicationContext.xml
。 In startUpContext.xml
there is a bean, which triggers loading of appliationContext.xml
. 在
startUpContext.xml
有一个bean,这将触发加载appliationContext.xml
。 (application context location is configured in startUpContext.xml
as a property of a trigger). (应用程序上下文位置在
startUpContext.xml
配置为触发器的属性)。 And finally the trigger replaces locations of the current context and refreshes it: 最后,触发器替换当前上下文的位置并刷新它:
applicationContext.setConfigLocations(locations);
applicationContext.refresh();
(startUpContext.xml is loaded with a standard spring context loader listener) (startUpContext.xml加载了标准的spring上下文加载器监听器)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.