如何在运行时在春季加载其他bean配置文件 [英] how to load additional bean configuration file in spring at run time

查看:120
本文介绍了如何在运行时在春季加载其他bean配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个spring应用程序,将来我们将开发更多的类,对于这些类,我们还将使用其他配置文件(而不是覆盖现有的文件)来定义Bean.那么如何动态加载它们呢?我知道有一个ApplicationContextAware接口,我可以运行一个Bean来检查是否有新的配置文件,如果有,我可以运行

I have a spring app, and in the future we will develop more classes, and for these class we will also use additional configuration files (not overwrite the existing ones) to define the beans. Then how to dynamically load them? I know there is an interface of ApplicationContextAware, I could have a bean running checking whether new configuration files are available, if they come, I could run the

setApplicationContext(ApplicationContext applicationContext)

但是然后如何使用ApplicationContext加载其他配置文件?

But then how to use ApplicationContext to load the additional configuration file?

更新: 如果应用程序是从XML加载的,那么我可以将ApplicationContext转换为ClassPathXmlApplicationContext,然后使用load方法,但是如果AnnotationConfigApplicationContext仅具有扫描程序包的扫描方法,但是如果我想从xml加载怎么办?

update: If the app is loaded from XML then I could convert ApplicationContext to ClassPathXmlApplicationContext and then use the load method,but what if AnnotationConfigApplicationContext, it only has scan method to scan package, but what if I want to load from xml?

更新: 以下是我要使用的代码,它使用spring集成来监视折叠,在运行时我可以将jar文件放在类路径上,然后将xml配置放在该文件夹中,这将触发loadAdditionBeans函数运行,并将传递xml File对象,需要做的是将该文件中的上下文添加到当前上下文中,但不创建子上下文.

update: The following is the code I want to use, it used spring integration to monitor a fold, at run time I could put jar file on the class path, and then put the xml configuration in that folder, this will trigger the loadAdditionBeans function to run, and the xml File object will be passed in, what need to do is to add the context in that File to the current context but not create a child context.

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;

import java.io.File;

@MessageEndpoint
public class FolderWatcher implements ApplicationContextAware {
    //private ApplicationContext ctx;
    private AnnotationConfigApplicationContext ctx;  // it's a spring boot,so the ctx is AnnotationConfigApplicationContext
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.ctx=(AnnotationConfigApplicationContext)applicationContext;
    }
    @ServiceActivator
    public void loadAdditionBeans(File file){
        /*
        file is an xml configuration file, how to load the beans defined it into the currect context,
        I don't what to have another hierarchy, since that will make the beans defined in the file not
        available in parent.
         */
    }

}

推荐答案

PathMatchingResourcePatternResolver pmrl = new PathMatchingResourcePatternResolver(context.getClassLoader());
  Resource[] resources = pmrl.getResources(
    "classpath*:com/mycompany/**/applicationContext.xml"
  );

for (Resource r : resources) {
   GenericApplicationContext createdContext = new GenericApplicationContext(context);
   XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(createdContext);
   int i = reader.loadBeanDefinitions(r);
}

看看上面的代码,让我知道它是否有助于解决您的问题.

Have a look at the above code and let me know if it helps to resolve your problem.

这篇关于如何在运行时在春季加载其他bean配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆