Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

spring

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

}
like image 554
Daniel Wu Avatar asked Dec 20 '14 13:12

Daniel Wu


People also ask

Can we have 2 configuration files in Spring?

Yes, in large projects, having multiple Spring configurations increase maintainability and modularity. You can also upload one XML file that will contain all configs.

How can we load all the bean configuration files?

You may load multiple Spring bean configuration files in the code : ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Common. xml", "Spring-Connection. xml","Spring-ModuleA.

What is the correct way to load bean definitions from another file?

Just import the xml defining the bean with <import resource="otherXml. xml"> and you will be able to use the bean definition.


1 Answers

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.

like image 106
Andy Dufresne Avatar answered Nov 21 '22 04:11

Andy Dufresne