Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
797 views
in Technique[技术] by (71.8m points)

how to load additional bean configuration file in spring at run time

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)

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

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?

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.
         */
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...