Example usage for org.springframework.beans.factory.config ConfigurableListableBeanFactory setBeanClassLoader

List of usage examples for org.springframework.beans.factory.config ConfigurableListableBeanFactory setBeanClassLoader

Introduction

In this page you can find the example usage for org.springframework.beans.factory.config ConfigurableListableBeanFactory setBeanClassLoader.

Prototype

void setBeanClassLoader(@Nullable ClassLoader beanClassLoader);

Source Link

Document

Set the class loader to use for loading bean classes.

Usage

From source file:pt.webdetails.cte.engine.CoreBeanFactory.java

protected ConfigurableApplicationContext getSpringBeanFactory(String pluginName) {
    try {/*from  w  w w .  j a  v  a2  s  . c  o  m*/
        final ClassLoader cl = this.getClass().getClassLoader();

        // if we can't find a <plugin_name>.spring.xml, we'll default to 'plugin.spring.xml'
        String springBeanName = cl.getResource(pluginName) != null ? pluginName : DEFAULT_SPRING_XML_NAME;

        URL url = cl.getResource(springBeanName);

        if (url != null) {
            logger.debug("Found spring file @ " + url); //$NON-NLS-1$
            ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(springBeanName) {
                @Override
                protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {

                    beanDefinitionReader.setBeanClassLoader(cl);
                }

                @Override
                protected void prepareBeanFactory(ConfigurableListableBeanFactory clBeanFactory) {
                    super.prepareBeanFactory(clBeanFactory);
                    clBeanFactory.setBeanClassLoader(cl);
                }

                /**
                 * Critically important to override this and return the desired CL
                 **/
                @Override
                public ClassLoader getClassLoader() {
                    return cl;
                }
            };
            return context;
        }
    } catch (Exception e) {
        logger.fatal("Error loading " + pluginName, e);
    }
    logger.fatal(
            "Spring definition file does not exist. There should be a <plugin_name>.spring.xml file on the classpath ");
    return null;

}