@Test public void testClassNotFoundWithNoBeanClassLoader() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf); reader.setBeanClassLoader(null); reader.loadBeanDefinitions(CLASS_NOT_FOUND_CONTEXT); assertEquals("WhatALotOfRubbish", bf.getBeanDefinition("classNotFound").getBeanClassName()); }
ApplicationContext ctx = new ClassPathXmlApplicationContext(myCtxPath) { protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) { super.initBeanDefinitionReader(reader); reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE); reader.setBeanClassLoader(getClassLoader()); } }
ApplicationContext ctx = new ClassPathXmlApplicationContext(myCtxPath) { protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) { super.initBeanDefinitionReader(reader); reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE); reader.setBeanClassLoader(getClassLoader()); } }
/** * Load the application context using a single classloader * @param ac The spring application context * @param config a list of configurations represented as List of resources * @param loader the classloader to use */ public void loadComponent(ConfigurableApplicationContext ac, List<Resource> config, ClassLoader loader) { ClassLoader current = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(loader); try { // make a reader XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader( (BeanDefinitionRegistry) ac.getBeanFactory()); // In Spring 2, classes aren't loaded during bean parsing unless // this // classloader property is set. reader.setBeanClassLoader(loader); reader.loadBeanDefinitions(config.toArray(new Resource[0])); } catch (Throwable t) { log.warn("loadComponentPackage: exception loading: " + config + " : " + t, t); } finally { // restore the context loader Thread.currentThread().setContextClassLoader(current); } }
protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) { super.initBeanDefinitionReader(reader); reader.setBeanClassLoader(loader); setClassLoader(loader); }
reader.setBeanClassLoader(loader);
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); } }
reader.setBeanClassLoader(classLoader); applicationContext.setClassLoader(classLoader); Thread.currentThread().setContextClassLoader(classLoader);
@Override public Servlet getServletInstance(ServletContext context) { ClassLoader orig = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(module.getClassLoader()); try { final GenericWebApplicationContext mvcContext = new GenericWebApplicationContext(context); mvcContext.setClassLoader(module.getClassLoader()); mvcContext.setServletContext(context); XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(mvcContext); xmlReader.setBeanClassLoader(module.getClassLoader()); xmlReader.loadBeanDefinitions(new ClassPathResource(springMvcApplicationContextLocation, module.getClassLoader())); mvcContext.setParent(existingLilyRuntimeSpringContext); mvcContext.refresh(); return new JerseySpringServlet(mvcContext); } finally { Thread.currentThread().setContextClassLoader(orig); } }
classLoader = getClassLoader(applicationContext, reader, document); reader.setBeanClassLoader(classLoader); applicationContext.setClassLoader(classLoader); Thread.currentThread().setContextClassLoader(classLoader);
@Override public Servlet getServletInstance(ServletContext context) { ClassLoader orig = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(module.getClassLoader()); try { final GenericWebApplicationContext mvcContext = new GenericWebApplicationContext(context); mvcContext.setClassLoader(module.getClassLoader()); mvcContext.setServletContext(context); XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(mvcContext); xmlReader.setBeanClassLoader(module.getClassLoader()); xmlReader.loadBeanDefinitions(new ClassPathResource(springMvcApplicationContextLocation, module.getClassLoader())); mvcContext.setParent(existingLilyRuntimeSpringContext); mvcContext.refresh(); DispatcherServlet dispatcherServlet = new DispatcherServlet(mvcContext); dispatcherServlet.setDetectAllHandlerMappings(true); return dispatcherServlet; } finally { Thread.currentThread().setContextClassLoader(orig); } }
classLoader = getClassLoader(applicationContext, reader, document); reader.setBeanClassLoader(classLoader); applicationContext.setClassLoader(classLoader); Thread.currentThread().setContextClassLoader(classLoader);
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 (currentClassLoader != Thread.currentThread().getContextClassLoader()) { XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(ctx); reader.setBeanClassLoader(currentClassLoader); reader.loadBeanDefinitions(new ClassPathResource(config, currentClassLoader)); Collection<PluginConfig> plugins = configuration.getPlugins();
xmlReader.setBeanClassLoader(classLoader);
/** * Loads the bean definitions via an XmlBeanDefinitionReader. * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader * @see #initBeanDefinitionReader * @see #loadBeanDefinitions */ protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException { // Create a new XmlBeanDefinitionReader for the given BeanFactory. XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); // Configure the bean definition reader with this context's // resource loading environment. beanDefinitionReader.setResourceLoader(this); if (getClassLoader() != null) { beanDefinitionReader.setBeanClassLoader(getClassLoader()); } beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this)); // Allow a subclass to provide custom initialization of the reader, // then proceed with actually loading the bean definitions. initBeanDefinitionReader(beanDefinitionReader); loadBeanDefinitions(beanDefinitionReader); }
/** * Include BeanPostProcessor to deal with SCA Annotations in Spring Bean */ private GenericApplicationContext createApplicationContext(SCAParentApplicationContext scaParentContext, List<URL> resources) { GenericApplicationContext appCtx = new SCAGenericApplicationContext(scaParentContext, implementation.getClassLoader()); XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(appCtx); // REVIEW: [rfeng] How do we control the schema validation xmlReader.setValidating(false); for (URL resource : resources) { xmlReader.loadBeanDefinitions(new UrlResource(resource)); } xmlReader.setBeanClassLoader(implementation.getClassLoader()); includeAnnotationProcessors(appCtx.getBeanFactory()); return appCtx; }