【问题标题】:How to add beans loaded via FileSystemXmlApplicationContext to an existing context loaded via ClassPathXmlApplicationContext?如何将通过 FileSystemXmlApplicationContext 加载的 bean 添加到通过 ClassPathXmlApplicationContext 加载的现有上下文中?
【发布时间】:2020-04-16 16:10:33
【问题描述】:

我有一个旧应用程序,它的所有 spring 批处理 bean 都在类路径中;这些 bean 使用 ClassPathXmlApplicationContext 加载到应用程序上下文中。现在,我需要将这些 spring batch bean 放在我的 war/classpath 之外。

如果你能在这里帮助我吗?我无法更改现有代码,如果我使用 FileSystemXmlApplicationContext 加载这些新定义,如何将它们与现有上下文合并?

谢谢!

【问题讨论】:

    标签: spring spring-mvc web-applications applicationcontext


    【解决方案1】:

    您可以尝试在新应用程序中使用@ImportResource 从 XML 资源加载 bean 定义:

    @Configuration
    @ImportResource({
            "classpath:your-old-applicationContext.xml",
            "classpath:another-old-applicationContext.xml"
    })
    public class NewApplicationConfig {
    }
    

    【讨论】:

    • 感谢您的回复;但遗留应用程序有多个 context.xml 文件;列出它们将是另一个挑战。有什么方法可以将我的新上下文合并到现有上下文?
    • 如果“现有”是指“完全实例化的 ApplicationContext”,那么这是不可能的(afaik)。原因是 ApplicationContext 必须重新实例化其 bean 生命周期以适应新添加的 bean。
    • 是的,现有的我的意思是完全实例化的 ApplicationContext。
    猜你喜欢
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多