如何在运行时在spring中加载额外的bean配置文件
我有一个spring应用程序,将来我们将开发更多的类,对于这些类,我们还将使用额外的配置文件(而不是覆盖现有文件)来定义bean。那么如何动态加载它们呢?我知道ApplicationContextAware有一个接口,我可以让一个bean运行,检查是否有新的配置文件可用,如果有,我可以运行如何在运行时在spring中加载额外的bean配置文件,spring,Spring,我有一个spring应用程序,将来我们将开发更多的类,对于这些类,我们还将使用额外的配置文件(而不是覆盖现有文件)来定义bean。那么如何动态加载它们呢?我知道ApplicationContextAware有一个接口,我可以让一个bean运行,检查是否有新的配置文件可用,如果有,我可以运行 setApplicationContext(ApplicationContext applicationContext) 但是,如何使用ApplicationContext加载附加的配置文件呢 更新: 如果
setApplicationContext(ApplicationContext applicationContext)
但是,如何使用ApplicationContext加载附加的配置文件呢
更新:
如果应用程序是从XML加载的,那么我可以将ApplicationContext转换为ClassPathXmlApplicationContext,然后使用load方法,但是如果AnnotationConfigApplicationContext,它只有扫描方法来扫描包,那么如果我想从XML加载呢
更新:
下面是我想要使用的代码,它使用spring集成来监视折叠,在运行时我可以将jar文件放在类路径上,然后将xml配置放在该文件夹中,这将触发loadAdditionBeans函数运行,并传入xml文件对象,需要做的是将该文件中的上下文添加到当前上下文中,而不是创建子上下文
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.
*/
}
}
请查看上述代码,并告诉我它是否有助于解决您的问题。如果您正在使用类路径扫描,但仍希望从XML加载其他配置,您只需在
@Configuration
类上使用@ImportResource
注释并导入所需的XML资源:
@配置
@ImportResource({“classpath*:/rest\u config.xml})
公共类MyConfig{
...
}
通过这种方式,可以很容易地将旧的XML配置与更新的Java配置混合在一起,并且您不必(例如)一次性迁移整个配置
希望有帮助 你不能在Spring Listener上加载配置文件吗?@SK08,我是Spring新手,请你提供更多详细信息。你是在根上下文中加载bean,还是在调用reader.loadBeanDefinitions(r)时创建一些子上下文?如果创建子上下文,那么这对我来说是个问题,因为根上下文看不到这些bean。这就是创建一个新上下文。如果要在根上下文中加载它,则将其作为参数传递给XmlBeanDefinitionReader。那应该行。谢谢你的信息。如果一个bean已经存在,它将被重新加载还是忽略?只是想知道如果发生冲突会发生什么。是否真的需要在循环中创建一个新的
GenericApplicationContext
?这个上下文不能在循环之外创建吗?创建一个新上下文似乎会引起问题,至少在我的例子中是这样。下面是建议的解决方案的一个示例,您的方法仅在java应用程序启动时有效。在应用程序启动之后,我们需要一种新的方法来动态添加bean。
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);
}