[英]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 你需要两件事
A file watcher to watch for file system changes - JDK7 provides a way to do this. 文件监视程序,用于监视文件系统更改-JDK7提供了一种执行此操作的方法。
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.