简体   繁体   English

我可以将其他Spring配置文件动态加载到现有的WebApplicationContext中吗?

[英]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: 如果您需要数据库迁移,可以先使用几种模式运行它们:

  • if you're using Hibernate/JPA make your sessionFactory/persistenceManager depend-on the migration beans; 如果你正在使用Hibernate / JPA,你的sessionFactory / persistenceManager 依赖于迁移bean;
  • if you're using plain JDBC create a wrapper DataSource and in its init-method invoke the migrations ( code sample ) 如果您使用普通JDBC创建包装器DataSource并在其init方法中调用迁移( 代码示例

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.xmlapplicationContext.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.

相关问题 如何将现有对象传递给Spring WebApplicationContext? - How to pass existing object to Spring WebApplicationContext? Spring Application Context无法加载配置文件 - Spring Application Context can't load the configuration files 我可以通过扫描spring配置文件中的bean来动态创建List吗? - Can I dynamically create a List by scanning the beans in a spring configuration file? 如何使用配置文件连接到Spring中的数据库? - How can I connect to a database in Spring using configuration files? 如何使用几个bean配置文件配置Spring BeanFactory? - How can I configure a Spring BeanFactory with several bean configuration files? 我可以在 Spring Boot 配置文件中定义系统属性吗? - Can I define System Properties within Spring Boot configuration files? 使用WebApplicationContext的最低配置 - minimal configuration for using WebApplicationContext 如何使用Maven用其他文件更新现有的战争文件? - How can I update a existing war file with additional files using Maven? 使用 Web Start / JNLP 动态加载额外的 jar 文件 - Dynamically load additional jar files using Web Start / JNLP Spring-如何使用PropertyPlaceholderConfigurer动态加载文件 - Spring - how to use PropertyPlaceholderConfigurer to dynamically load files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM