Example usage for org.springframework.beans.factory.xml XmlBeanDefinitionReader setBeanClassLoader

List of usage examples for org.springframework.beans.factory.xml XmlBeanDefinitionReader setBeanClassLoader

Introduction

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

Prototype

public void setBeanClassLoader(@Nullable ClassLoader beanClassLoader) 

Source Link

Document

Set the ClassLoader to use for bean classes.

Usage

From source file:com.mitre.cockpits.cmscockpit.CmsCockpitConfigurationTest.java

@BeforeClass
public static void testsSetup() {
    Registry.setCurrentTenantByID("junit");
    final GenericApplicationContext context = new GenericApplicationContext();
    context.setResourceLoader(new DefaultResourceLoader(Registry.class.getClassLoader()));
    context.setClassLoader(Registry.class.getClassLoader());
    context.getBeanFactory().setBeanClassLoader(Registry.class.getClassLoader());
    context.setParent(Registry.getGlobalApplicationContext());
    final XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(context);
    xmlReader.setBeanClassLoader(Registry.class.getClassLoader());
    xmlReader.setDocumentReaderClass(ScopeTenantIgnoreDocReader.class);
    xmlReader.loadBeanDefinitions(getSpringConfigurationLocations());
    context.refresh();//  w w  w. java 2s  . com
    applicationContext = context;
}

From source file:org.jwebsocket.spring.JWebSocketBeanFactory.java

/**
 * Load beans from a configuration file into a specific bean factory
 *
 * @param aNamespace/*from  w w  w.  j a  v  a 2 s. co  m*/
 * @param aPath
 * @param aClassLoader
 */
public static void load(String aNamespace, String aPath, ClassLoader aClassLoader) {
    String lPath = Tools.expandEnvVarsAndProps(aPath);

    XmlBeanDefinitionReader lXmlReader;
    if (null != aNamespace) {
        lXmlReader = new XmlBeanDefinitionReader(getInstance(aNamespace));
    } else {
        lXmlReader = new XmlBeanDefinitionReader(getInstance());
    }

    lXmlReader.setBeanClassLoader(aClassLoader);

    // if no JWEBSOCKET_HOME environment variable set 
    // then use the classpath resource, otherwise the file system resource
    // System.out.println("getJWebSocketHome: '" + JWebSocketConfig.getJWebSocketHome() + "'...");
    if (JWebSocketConfig.getJWebSocketHome().isEmpty()) {
        // System.out.println("Loading resource from classpath: " + aPath + "...");
        lXmlReader.loadBeanDefinitions(new ClassPathResource(lPath));
    } else {
        // System.out.println("Loading resource from filesystem: " + aPath + "...");
        lXmlReader.loadBeanDefinitions(new FileSystemResource(lPath));
    }
}

From source file:pt.webdetails.cpf.SpringEnabledContentGenerator.java

private ConfigurableApplicationContext getSpringBeanFactory() {
    final PluginClassLoader loader = (PluginClassLoader) pm.getClassLoader(getPluginName());
    logger.warn(loader.getPluginDir());//from  ww  w  . j a  v  a 2s  .  c o  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 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$
            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:org.walkmod.conf.providers.SpringConfigurationProvider.java

@Override
public void loadBeanFactory() throws ConfigurationException {

    GenericApplicationContext ctx = new GenericApplicationContext();
    ClassLoader currentClassLoader = configuration.getClassLoader();
    if (currentClassLoader != Thread.currentThread().getContextClassLoader()) {
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(ctx);
        reader.setBeanClassLoader(currentClassLoader);
        reader.loadBeanDefinitions(new ClassPathResource(config, currentClassLoader));
        Collection<PluginConfig> plugins = configuration.getPlugins();
        if (plugins != null) {
            for (PluginConfig plugin : plugins) {
                String descriptorName = plugin.getArtifactId();
                if (!descriptorName.startsWith("walkmod-")) {
                    descriptorName = "walkmod-" + descriptorName;
                }//from   w ww.j  ava  2  s. c o m
                if (!descriptorName.endsWith("-plugin")) {
                    descriptorName = descriptorName + "-plugin";
                }

                reader.loadBeanDefinitions(new ClassPathResource("META-INF/walkmod/" + descriptorName + ".xml",
                        configuration.getClassLoader()));

                URL url = currentClassLoader.getResource("META-INF/walkmod2/" + descriptorName + ".xml");
                if (url != null) {
                    reader.loadBeanDefinitions(new ClassPathResource(
                            "META-INF/walkmod2/" + descriptorName + ".xml", configuration.getClassLoader()));
                }
            }
        }
        configuration.setBeanDefinitionRegistry(reader.getRegistry());
        ctx.refresh();
    }

    configuration.setBeanFactory(ctx);
}

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

protected ConfigurableApplicationContext getSpringBeanFactory(String config) {
    try {//  w ww  . ja v  a2  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$
            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 {/* ww w  .  j a v  a 2s .  com*/
        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 w  w.ja  v  a2s  . c om
        // 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   w  w w  .java 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.sakaiproject.util.SakaiApplicationContext.java

protected void loadBeanDefinitions(ConfigurableListableBeanFactory beanFactory) throws IOException {
    // Create a new XmlBeanDefinitionReader for the given BeanFactory.
    XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(
            (BeanDefinitionRegistry) beanFactory);
    beanDefinitionReader.setBeanClassLoader(Thread.currentThread().getContextClassLoader());
    beanDefinitionReader.setResourceLoader(this);
    beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

    if (configLocations != null) {
        beanDefinitionReader.loadBeanDefinitions(configLocations);
    }/*from   w w w . j  a v a 2s.  c o  m*/
}