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.cpf.SpringEnabledContentGenerator.java

private ConfigurableApplicationContext getSpringBeanFactory() {
    final PluginClassLoader loader = (PluginClassLoader) pm.getClassLoader(getPluginName());
    logger.warn(loader.getPluginDir());/*www .  j a  v  a  2s .  co m*/
    File f = new File(loader.getPluginDir(), "plugin.spring.xml"); //$NON-NLS-1$
    if (f.exists()) {
        logger.debug("Found plugin spring file @ " + f.getAbsolutePath()); //$NON-NLS-1$
        ConfigurableApplicationContext context = new FileSystemXmlApplicationContext(
                "file:" + f.getAbsolutePath()) { //$NON-NLS-1$
            @Override
            protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {

                beanDefinitionReader.setBeanClassLoader(loader);
            }

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

            /**
             * Critically important to override this and return the desired
             * CL
            *
             */
            @Override
            public ClassLoader getClassLoader() {
                return loader;
            }
        };
        return context;
    }
    throw new IllegalStateException("no plugin.spring.xml file found"); //$NON-NLS-1$
}

From source file:pt.webdetails.cdv.bean.factory.CoreBeanFactory.java

protected ConfigurableApplicationContext getSpringBeanFactory(String config) {
    try {//from  w  w  w  .  j  av  a  2 s .com
        final ClassLoader cl = this.getClass().getClassLoader();
        URL url = cl.getResource(config);
        if (url != null) {
            logger.debug("Found spring file @ " + url); //$NON-NLS-1$
            return new ClassPathXmlApplicationContext(config) {
                @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;
                }
            };
        }
    } catch (Exception e) {
        logger.fatal("Error loading " + CDV_SPRING_BEAN, e);
    }
    logger.fatal(
            "Spring definition file does not exist. There should be a <plugin_name>.spring.xml file on the classpath ");
    return null;
}

From source file:pt.webdetails.cdb.bean.factory.CoreBeanFactory.java

protected ConfigurableApplicationContext getSpringBeanFactory(String config) {
    try {/*from   www  . j  a  v a  2 s  .  c om*/
        final ClassLoader cl = this.getClass().getClassLoader();
        URL url = cl.getResource(config);
        if (url != null) {
            logger.debug("Found spring file @ " + url); //$NON-NLS-1$
            ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(config) {
                @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 " + CDB_SPRING_BEAN, e);
    }
    logger.fatal(
            "Spring definition file does not exist. There should be a <plugin_name>.spring.xml file on the classpath ");
    return null;

}

From source file:pt.webdetails.cdc.bean.factory.CoreBeanFactory.java

protected ConfigurableApplicationContext getSpringBeanFactory(String config) {
    try {/*from   w  w  w .  j  a v  a2s .  c  o  m*/
        final ClassLoader cl = this.getClass().getClassLoader();
        URL url = cl.getResource(config);
        if (url != null) {
            logger.debug("Found spring file @ " + url); //$NON-NLS-1$
            ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(config) {
                @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 " + CDC_SPRING_BEAN, e);
    }
    logger.fatal(
            "Spring definition file does not exist. There should be a <plugin_name>.spring.xml file on the classpath ");
    return null;

}

From source file:pt.webdetails.cpf.bean.AbstractBeanFactory.java

protected ConfigurableApplicationContext getSpringBeanFactory(String config) {
    getLogger().debug("bean factory init");

    try {/*from w ww  . ja va 2  s.  com*/
        // important: use the plugin's classloader
        final ClassLoader cl = getClass().getClassLoader();

        URL url = cl.getResource(config);
        if (url != null) {
            getLogger().debug("Found spring file @ " + url);

            ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(config) {

                @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 classloader
                 **/
                @Override
                public ClassLoader getClassLoader() {
                    return cl;
                }
            };
            getLogger().debug("bean factory context");
            return context;
        }
    } catch (Exception e) {
        getLogger().fatal("Error loading " + config, e);
    }
    getLogger().fatal("Spring definition file does not exist. " + "There should be a " + config
            + " file on the classpath ");
    return null;

}

From source file:org.pentaho.plugin.j2ee.ServletAdapterContentGenerator.java

private ConfigurableApplicationContext getSpringBeanFactory() {
    final PluginClassLoader loader = (PluginClassLoader) pm.getClassLoader(PLUGIN_ID);
    File f = new File(loader.getPluginDir(), "plugin.spring.xml"); //$NON-NLS-1$
    if (f.exists()) {
        logger.debug("Found plugin spring file @ " + f.getAbsolutePath()); //$NON-NLS-1$
        ConfigurableApplicationContext context = new FileSystemXmlApplicationContext(
                "file:" + f.getAbsolutePath()) { //$NON-NLS-1$
            @Override/*from   ww w .  jav  a  2  s.c  o m*/
            protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {

                beanDefinitionReader.setBeanClassLoader(loader);
            }

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

            /** Critically important to override this and return the desired CL **/
            @Override
            public ClassLoader getClassLoader() {
                return loader;
            }
        };
        return context;
    }
    throw new IllegalStateException("no plugin.spring.xml file found"); //$NON-NLS-1$
}

From source file:org.apache.struts2.spring.ClassReloadingXMLWebApplicationContext.java

protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    super.prepareBeanFactory(beanFactory);

    //overwrite the class loader in the bean factory
    if (classLoader != null)
        beanFactory.setBeanClassLoader(classLoader);
}

From source file:org.pentaho.platform.plugin.services.pluginmgr.DefaultPluginManager.java

/**
 * The native bean factory is the bean factory that has had all of its bean definitions loaded natively. In other
 * words, the plugin manager will not add any further bean definitions (i.e. from a plugin.xml file) into this
 * factory. This factory represents the one responsible for holding bean definitions for plugin.spring.xml or, if in a
 * unit test environment, the unit test pre-loaded bean factory.
 *
 * @return a bean factory will preconfigured bean definitions or <code>null</code> if no bean definition source is
 * available/*from  www.j  a  v a  2s.c om*/
 */
protected BeanFactory getNativeBeanFactory(final IPlatformPlugin plugin, final ClassLoader loader) {
    BeanFactory nativeFactory = null;
    if (plugin.getBeanFactory() != null) {
        // then we are probably in a unit test so just use the preconfigured one
        BeanFactory testFactory = plugin.getBeanFactory();
        if (testFactory instanceof ConfigurableBeanFactory) {
            ((ConfigurableBeanFactory) testFactory).setBeanClassLoader(loader);
        } else {
            logger.warn(Messages.getInstance().getString("PluginManager.WARN_WRONG_BEAN_FACTORY_TYPE")); //$NON-NLS-1$
        }
        nativeFactory = testFactory;
    } else {
        File f = new File(((PluginClassLoader) loader).getPluginDir(), "plugin.spring.xml"); //$NON-NLS-1$
        if (f.exists()) {
            logger.debug("Found plugin spring file @ " + f.getAbsolutePath()); //$NON-NLS-1$

            FileSystemResource fsr = new FileSystemResource(f);
            GenericApplicationContext appCtx = new GenericApplicationContext() {

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

                @Override
                public ClassLoader getClassLoader() {
                    return loader;
                }

            };

            XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(appCtx);
            xmlReader.setBeanClassLoader(loader);
            xmlReader.loadBeanDefinitions(fsr);

            nativeFactory = appCtx;
        }
    }
    return nativeFactory;
}

From source file:org.springframework.context.support.AbstractApplicationContext.java

/**
 * Configure the factory's standard context characteristics,
 * such as the context's ClassLoader and post-processors.
 * @param beanFactory the BeanFactory to configure
 *//*from   w w w .j a v  a2 s.c o  m*/
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    // Tell the internal bean factory to use the context's class loader etc.
    beanFactory.setBeanClassLoader(getClassLoader());
    beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
    beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

    // Configure the bean factory with context callbacks.
    beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
    beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
    beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
    beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
    beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
    beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
    beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

    // BeanFactory interface not registered as resolvable type in a plain factory.
    // MessageSource registered (and found for autowiring) as a bean.
    beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
    beanFactory.registerResolvableDependency(ResourceLoader.class, this);
    beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
    beanFactory.registerResolvableDependency(ApplicationContext.class, this);

    // Register early post-processor for detecting inner beans as ApplicationListeners.
    beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

    // Detect a LoadTimeWeaver and prepare for weaving, if found.
    if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
        beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
        // Set a temporary ClassLoader for type matching.
        beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
    }

    // Register default environment beans.
    if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
        beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
    }
    if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
        beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
    }
    if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
        beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
    }
}

From source file:pt.webdetails.cda.CoreBeanFactory.java

protected ConfigurableApplicationContext getSpringBeanFactory(String config) {
    logger.debug("bean factory ini");
    try {/*from w  w  w . j  ava  2 s .co m*/
        final ClassLoader cl = this.getClass().getClassLoader();
        URL url = cl.getResource(config);
        if (url != null) {
            logger.debug("Found spring file @ " + url); //$NON-NLS-1$
            ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(config) {
                @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;
                }
            };
            logger.debug("bean factory context");
            return context;
        }
    } catch (Exception e) {
        logger.fatal("Error loading cda.spring.xml", e);
    }
    logger.fatal(
            "Spring definition file does not exist. There should be a cda.spring.xml file on the classpath ");
    return null;

}