reader.loadBeanDefinitions(resource); });
private DefaultListableBeanFactory maybeRegistryToBeanFactory() { BeanDefinitionRegistry registry = this.reader.getRegistry(); if (registry instanceof DefaultListableBeanFactory) { return (DefaultListableBeanFactory) registry; } else if (registry instanceof GenericApplicationContext) { return ((GenericApplicationContext) registry).getDefaultListableBeanFactory(); } return null; } }
public synchronized void reload() { StopWatch sw = LoggerFactory.getInstance().createStopWatch("XMLBeanDefinitionResource.reload"); try { LoggerFactory.getInstance().echo( "JavaRebel-Spring: Reloading Spring bean definitions in '" + xmlFile + "'."); try { bdr.loadBeanDefinitions(xmlRes); if (bdr.getRegistry() instanceof DefaultListableBeanFactory) { // XXX Is this safe? See {@link DefaultListableBeanFactoryCBP} ((DefaultListableBeanFactory) bdr.getRegistry()).preInstantiateSingletons(); } } catch (Throwable e) { if (e instanceof Error) throw (Error) e; LoggerFactory.getInstance().error(e); } } finally { if (sw != null) sw.stop(); } }
/** * Attempts to load the class on the current thread context class loader or * the class loader which loaded us */ protected Class loadClass(String name) throws ClassNotFoundException { ClassLoader beanClassLoader = getBeanDefinitionReader().getBeanClassLoader(); if (beanClassLoader != null) { try { return beanClassLoader.loadClass(name); } catch (ClassNotFoundException e) { } } ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); if (contextClassLoader != null) { try { return contextClassLoader.loadClass(name); } catch (ClassNotFoundException e) { } } return getClass().getClassLoader().loadClass(name); }
/** * Parse the elements at the root level in the document: * "import", "alias", "bean". * @param root the DOM root element of the document * @return the number of bean definitions found */ protected int parseBeanDefinitions(Element root) throws BeanDefinitionStoreException { NodeList nl = root.getChildNodes(); int beanDefinitionCount = 0; for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element) { Element ele = (Element) node; if (IMPORT_ELEMENT.equals(node.getNodeName())) { importBeanDefinitionResource(ele); } else if (ALIAS_ELEMENT.equals(node.getNodeName())) { String name = ele.getAttribute(NAME_ATTRIBUTE); String alias = ele.getAttribute(ALIAS_ATTRIBUTE); this.beanDefinitionReader.getBeanFactory().registerAlias(name, alias); } else if (BEAN_ELEMENT.equals(node.getNodeName())) { beanDefinitionCount++; BeanDefinitionHolder bdHolder = parseBeanDefinitionElement(ele, false); BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, this.beanDefinitionReader.getBeanFactory()); } } } return beanDefinitionCount; }
/** * Loads the resource from the given URI */ protected InputStream loadResource(String uri) { if (System.getProperty("xbean.dir") != null) { File f = new File(System.getProperty("xbean.dir") + uri); try { return new FileInputStream(f); } catch (FileNotFoundException e) { // Ignore } } // lets try the thread context class loader first InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(uri); if (in == null) { ClassLoader cl = getBeanDefinitionReader().getBeanClassLoader(); if (cl != null) { in = cl.getResourceAsStream(uri); } if (in == null) { in = getClass().getClassLoader().getResourceAsStream(uri); if (in == null) { logger.debug("Could not find resource: " + uri); } } } return in; }
(AbstractBeanDefinition) beanDefinition, this.beanDefinitionReader.getBeanFactory(), isInnerBean); if (logger.isDebugEnabled()) { logger.debug("Neither XML 'id' nor 'name' specified - " +
/** * Load bean definitions into the supplied {@link GenericApplicationContext context} * from the locations or classes in the supplied {@code MergedContextConfiguration}. * * <p>The default implementation delegates to the {@link BeanDefinitionReader} * returned by {@link #createBeanDefinitionReader(GenericApplicationContext)} to * {@link BeanDefinitionReader#loadBeanDefinitions(String) load} the * bean definitions. * * <p>Subclasses must provide an appropriate implementation of * {@link #createBeanDefinitionReader(GenericApplicationContext)}. Alternatively subclasses * may provide a <em>no-op</em> implementation of {@code createBeanDefinitionReader()} * and override this method to provide a custom strategy for loading or * registering bean definitions. * * @param context the context into which the bean definitions should be loaded * @param mergedConfig the merged context configuration * @since 3.1 * @see #loadContext(MergedContextConfiguration) */ protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) { createBeanDefinitionReader(context).loadBeanDefinitions(mergedConfig.getLocations()); }
String typeClassName = ele.getAttribute(TYPE_ATTRIBUTE); try { Class typeClass = ClassUtils.forName(typeClassName, this.beanDefinitionReader.getBeanClassLoader()); return new TypedStringValue(value, typeClass);
String name = ele.getAttribute(NAME_ATTRIBUTE); String alias = ele.getAttribute(ALIAS_ATTRIBUTE); getBeanDefinitionReader().getBeanFactory().registerAlias(name, alias); BeanDefinitionHolder bdHolder = parseBeanDefinitionElement(ele, false); BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getBeanDefinitionReader() .getBeanFactory()); beanDefinitionCount++; BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getBeanDefinitionReader() .getBeanFactory()); beanDefinitionCount++; BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getBeanDefinitionReader() .getBeanFactory());
private XmlBeanDefinationScannerAgent(BeanDefinitionReader reader) { this.reader = reader; if (SpringPlugin.basePackagePrefixes != null && !basePackageInited) { ClassPathBeanDefinitionScannerAgent xmlBeanDefinitionScannerAgent = ClassPathBeanDefinitionScannerAgent.getInstance(new ClassPathBeanDefinitionScanner(reader.getRegistry())); for (String basePackage : SpringPlugin.basePackagePrefixes) { xmlBeanDefinitionScannerAgent.registerBasePackage(basePackage); } basePackageInited = true; } }
private int load(Resource source) { if (source.getFilename().endsWith(".groovy")) { if (this.groovyReader == null) { throw new BeanDefinitionStoreException( "Cannot load Groovy beans without Groovy on classpath"); } return this.groovyReader.loadBeanDefinitions(source); } return this.xmlReader.loadBeanDefinitions(source); }
className, parent, cargs, pvs, getBeanDefinitionReader().getBeanClassLoader());
@Before public void setUp() throws Exception { BeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory); reader.loadBeanDefinitions(new ClassPathResource("collectionMerging.xml", getClass())); }
reader.loadBeanDefinitions(resource); });
/** * reload bean from xml defination * @param url url of xml */ public void reloadBeanFromXml(URL url) { LOGGER.info("reloading xml file: " + url); // this will call registerBeanDefinition which in turn call resetBeanDefinition to destroy singleton // maybe should use watchResourceClassLoader.getResource? this.reader.loadBeanDefinitions(new FileSystemResource(url.getPath())); // spring won't rebuild dependency map if injectionMetadataCache is not cleared // which lead to singletons depend on beans in xml won't be destroy and recreate, may be a spring bug? ResetBeanPostProcessorCaches.reset(maybeRegistryToBeanFactory()); ProxyReplacer.clearAllProxies(); reloadFlag = false; }
@Test public void testInterfaceWithOneQualifiedFactoryAndOneQualifiedBean() { StaticApplicationContext context = new StaticApplicationContext(); BeanDefinitionReader reader = new XmlBeanDefinitionReader(context); reader.loadBeanDefinitions(CONFIG_LOCATION); }
prepareContext(context); customizeBeanFactory(context.getDefaultListableBeanFactory()); createBeanDefinitionReader(context).loadBeanDefinitions(locations); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); customizeContext(context);
@Test public void testQualifiedByAttributesFailsWithoutCustomQualifierRegistered() { StaticApplicationContext context = new StaticApplicationContext(); BeanDefinitionReader reader = new XmlBeanDefinitionReader(context); reader.loadBeanDefinitions(CONFIG_LOCATION); context.registerSingleton("testBean", QualifiedByAttributesTestBean.class); try { context.refresh(); fail("should have thrown a BeanCreationException"); } catch (BeanCreationException e) { assertTrue(e.getMessage().contains("found 6")); } }
@Test public void testNonQualifiedFieldFails() { StaticApplicationContext context = new StaticApplicationContext(); BeanDefinitionReader reader = new XmlBeanDefinitionReader(context); reader.loadBeanDefinitions(CONFIG_LOCATION); context.registerSingleton("testBean", NonQualifiedTestBean.class); try { context.refresh(); fail("Should have thrown a BeanCreationException"); } catch (BeanCreationException e) { assertTrue(e.getMessage().contains("found 6")); } }