简体   繁体   English

如何在春季动态加载配置文件

[英]how to dynamically load configuraiton file in spring

In a running spring app, could it monitor a folder under which I could put more configuration file in which more beans are defined? 在正在运行的spring应用程序中,它是否可以监视一个文件夹,在该文件夹下我可以放置更多定义更多Bean的配置文件? I don't want to stop the app, but want the ability to add load more beans, so I am thinking of create a folder, and then put bean definitions into that folder, and let spring to pick it up and initialize the beans. 我不想停止该应用程序,但是希望能够添加更多的bean,因此我正在考虑创建一个文件夹,然后将bean定义放入该文件夹中,并让spring进行拾取并初始化bean。 I am wondering whether it is doable or not 我想知道这是否可行

You would need two things 你需要两件事

  1. A file watcher to watch for file system changes - JDK7 provides a way to do this. 文件监视程序,用于监视文件系统更改-JDK7提供了一种执行此操作的方法。

  2. Refresh the application context - You can use the refresh method of one of the AbstractRefreshableApplicationContext class: it won't refresh your previously instantiated beans, but next call on context will return refreshed beans. 刷新应用程序上下文-您可以使用AbstractRefreshableApplicationContext类之一的refresh方法:它不会刷新您先前实例化的bean,但是对上下文的下一次调用将返回刷新的bean。

you can create a properties file and load it dynamically 您可以创建属性文件并动态加载

refer below code which i have used in my project: 请参考以下我在项目中使用的代码:

private static Properties beanProps = new Properties();
beanFactoryFileProps.load(AppContext.class
                    .getResourceAsStream("configFile.properties"));
            Collection props = beanProps.values();
            String[] configFiles = null;
            if (!Utils.isEmpty(props)) {
                Object[] config = props.toArray();
                configFiles = new String[config.length];
                for (int i = 0; i < config.length; i++) {
                    configFiles[i] = (String) config[i];
                }
            } 
            else
            {
                configFiles=new String[] {"dispatcher-servlet.xml"};
            }

properties file as below 属性文件如下

file1=dispatcher-servlet.xml
file2=ApplicationContext.xml
file2=QuartzSchedulerContext.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在关注基于Spring xml的配置,因此如何在spring-security.xml文件中配置TokenBasedRememberMeServices? - I am following spring xml based configuraiton so How TokenBasedRememberMeServices configuration in spring-security.xml file? 如何在apache camel + spring中动态加载属性文件值 - How to load property file values dynamically in apache camel + spring 如何动态加载Spring控制器 - How to dynamically load Spring controllers 如何动态加载 Spring 安全性 - How to load Spring Security dynamically Spring-如何使用PropertyPlaceholderConfigurer动态加载文件 - Spring - how to use PropertyPlaceholderConfigurer to dynamically load files 如何使用spring动态加载java中的配置 - how to dynamically load the config in java with spring spring boot中动态加载测试属性文件和服务属性文件 - dynamically load test property file and service property file in spring boot Spring 云客户端未从 spring 云服务器获取配置 - Spring Cloud Client not fetching configuraiton from spring cloud server 如何在Spring MVC中基于Profile动态加载Application.properties文件? - How to load Application.properties file dynamically based on Profile in Spring MVC? 动态加载jar文件并使用xml创建spring bean - Load jar file dynamically and create spring beans using xml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM