List of usage examples for org.springframework.beans.factory.support DefaultListableBeanFactory preInstantiateSingletons
@Override public void preInstantiateSingletons() throws BeansException
From source file:com.helpinput.spring.SourceScaner.java
private void scanAndCreateBeans() { if (scanning == true) return;//from www . j a v a 2s. co m scanning = true; try { initBeanInfosAndFileReaders(); boolean changed = false; for (Map<String, BeanInfo> map : scanedBeanInfos.values()) { for (BeanInfo subInfo : map.values()) { if (subInfo.needParse || !subInfo.scaned) { changed = true; break; } } if (changed) break; } if (!changed) return; int size = 0; for (Map<String, BeanInfo> beans : scanedBeanInfos.values()) { size += beans.size(); } Map<String, String> scanedNames = new HashMap<>(size); Set<String> scanedImputs = new HashSet<>(size); Map<String, Pattern> scanedRefeWrap = new HashMap<>(size); for (Map<String, BeanInfo> map : scanedBeanInfos.values()) { for (BeanInfo beanInfo : map.values()) { if (beanInfo.scaned) { scanedNames.put(beanInfo.scanName, beanInfo.importName); scanedImputs.add(beanInfo.importName); scanedRefeWrap.put(beanInfo.scanName, beanInfo.referencWrapPt); } } } DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory) ((AbstractApplicationContext) context) .getBeanFactory(); synchronized (dlbf) { for (Map<String, BeanInfo> beanInfosInPath : scanedBeanInfos.values()) { for (BeanInfo beanInfo : beanInfosInPath.values()) { File file = new File(beanInfo.fileName); if (beanInfo.needParse && file.exists()) { Class<?> clz = null; String scriptText; try { scriptText = readFile(beanInfo, scanedNames, scanedImputs, scanedRefeWrap, beanInfo.fileName); } catch (Exception e) { parserBeanInfoError(beanInfo); logger.info(beanInfo.fileName, e); continue; } //logger.info(scriptText); GroovyCodeSource groovyCodeSource = new GroovyCodeSource(scriptText, beanInfo.relativePath, "script/groovy"); groovyCodeSource.setCachable(false); try { clz = ClassLoaderHolder.gcl.parseClass(groovyCodeSource); if (clz == null) { //parser???? parserBeanInfoError(beanInfo); continue; } //System.out.println(scriptText); } catch (Exception e) { //?????? parserBeanInfoError(beanInfo); System.err.println(scriptText); e.printStackTrace(); continue; } scriptText = null; String beanName = null; BeanDefinitionBuilder builder = BeanRegister.createBuilder(clz, Interceptor.class); beanName = BeanRegister.registerBean(context, dlbf, builder, clz, null, null, beanInfo) .getBeanName(); changed = true; beanInfo.cu = null; if (hasLength(beanName)) { beanInfo.beanName = beanName; beanInfo.beanClass = clz; beanInfo.scaned = true; beanInfo.needParse = false; beanInfo.isUpdate = false; beanInfo.isNew = true; } } } } List<Class<?>> removedClasses = removeNotScanedBean(dlbf); changed = changed || hasLength(removedClasses); if (changed) { ClassLoaderHolder.gcl.compileCount++; boolean needClearClassLoader = (ClassLoaderHolder.gcl.compileCount % 100) == 0; Map<Class<?>, ScanedType> scanedClasses = new HashMap<>(size + removedClasses.size()); for (Class<?> deletedClass : removedClasses) scanedClasses.put(deletedClass, ScanedType.DELETED); Set<String> usedclassNames = needClearClassLoader ? new HashSet<String>(size) : null; Set<String> usedPaths = needClearClassLoader ? new HashSet<String>(size) : null; for (Map<String, BeanInfo> beans : scanedBeanInfos.values()) { for (BeanInfo beanInfo : beans.values()) { if (needClearClassLoader) { usedclassNames.add(beanInfo.importName); usedPaths.add(beanInfo.importName.replace('.', '/') + ".class"); } if (beanInfo.beanClass != null) scanedClasses.put(beanInfo.beanClass, beanInfo.isNew ? ScanedType.NEW : ScanedType.SAME); } } BeanRegister.refreshContext(this.context, scanedClasses); if (needClearClassLoader) ClassLoaderHolder.gcl.clearNodeUsedCache(usedclassNames, usedPaths); dlbf.preInstantiateSingletons(); } } } finally { scanning = false; } }
From source file:org.sakaiproject.metaobj.utils.mvc.impl.beans.AddableResourceBundleViewResolver.java
/** * Initialize the BeanFactory from the ResourceBundle, for the given locale. * Synchronized because of access by parallel threads. *//* w w w . j a v a2 s .co m*/ protected synchronized BeanFactory initFactory(Locale locale) throws MissingResourceException, BeansException { BeanFactory parsedBundle = isCache() ? (BeanFactory) this.cachedFactories.get(locale) : null; if (parsedBundle != null) { return parsedBundle; } DefaultListableBeanFactory factory = new DefaultListableBeanFactory(getApplicationContext()); PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(factory); reader.setDefaultParentBean(this.defaultParentView); for (Iterator i = baseNames.iterator(); i.hasNext();) { ResourceBundle bundle = ResourceBundle.getBundle((String) i.next(), locale, Thread.currentThread().getContextClassLoader()); reader.registerBeanDefinitions(bundle); } factory.registerCustomEditor(Resource.class, new ResourceEditor(getApplicationContext())); if (isCache()) { factory.preInstantiateSingletons(); this.cachedFactories.put(locale, factory); } return factory; }
From source file:org.springframework.beans.factory.access.JndiBeanFactoryLocator.java
/** * Actually create the BeanFactory, given an array of classpath resource strings * which should be combined. This is split out as a separate method so that subclasses * can override the actual type uses (to be an ApplicationContext, for example). * @param resources an array of Strings representing classpath resource names * @return the created BeanFactory, wrapped in a BeanFactoryReference *//*from ww w. j av a2 s. c o m*/ protected BeanFactoryReference createBeanFactory(String[] resources) throws BeansException { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory); for (int i = 0; i < resources.length; ++i) { reader.loadBeanDefinitions(new ClassPathResource(resources[i])); } factory.preInstantiateSingletons(); return new DefaultBeanFactoryReference(factory); }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testUnreferencedSingletonWasInstantiated() { KnowsIfInstantiated.clearInstantiationRecord(); DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("x1.(class)", KnowsIfInstantiated.class.getName()); assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated()); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); lbf.preInstantiateSingletons(); assertTrue("singleton was instantiated", KnowsIfInstantiated.wasInstantiated()); }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testLazyInitialization() { KnowsIfInstantiated.clearInstantiationRecord(); DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("x1.(class)", KnowsIfInstantiated.class.getName()); p.setProperty("x1.(lazy-init)", "true"); assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated()); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated()); lbf.preInstantiateSingletons(); assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated()); lbf.getBean("x1"); assertTrue("singleton was instantiated", KnowsIfInstantiated.wasInstantiated()); }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testFactoryBeanDidNotCreatePrototype() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("x1.(class)", DummyFactory.class.getName()); // Reset static state DummyFactory.reset();//from www. jav a2 s. c o m p.setProperty("x1.singleton", "false"); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); assertEquals(TestBean.class, lbf.getType("x1")); lbf.preInstantiateSingletons(); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); lbf.getBean("x1"); assertEquals(TestBean.class, lbf.getType("x1")); assertTrue(lbf.containsBean("x1")); assertTrue(lbf.containsBean("&x1")); assertTrue("prototype was instantiated", DummyFactory.wasPrototypeCreated()); }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testInitializedFactoryBeanFoundByNonEagerTypeMatching() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("x1.(class)", DummyFactory.class.getName()); // Reset static state DummyFactory.reset();/*from w w w .j a v a 2s . c om*/ p.setProperty("x1.singleton", "false"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); lbf.preInstantiateSingletons(); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); assertEquals(1, beanNames.length); assertEquals("x1", beanNames[0]); assertTrue(lbf.containsSingleton("x1")); assertTrue(lbf.containsBean("x1")); assertTrue(lbf.containsBean("&x1")); assertTrue(lbf.containsLocalBean("x1")); assertTrue(lbf.containsLocalBean("&x1")); assertFalse(lbf.isSingleton("x1")); assertTrue(lbf.isSingleton("&x1")); assertTrue(lbf.isPrototype("x1")); assertFalse(lbf.isPrototype("&x1")); assertTrue(lbf.isTypeMatch("x1", TestBean.class)); assertFalse(lbf.isTypeMatch("&x1", TestBean.class)); assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class)); assertTrue(lbf.isTypeMatch("x1", Object.class)); assertTrue(lbf.isTypeMatch("&x1", Object.class)); assertEquals(TestBean.class, lbf.getType("x1")); assertEquals(DummyFactory.class, lbf.getType("&x1")); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); lbf.registerAlias("x1", "x2"); assertTrue(lbf.containsBean("x2")); assertTrue(lbf.containsBean("&x2")); assertTrue(lbf.containsLocalBean("x2")); assertTrue(lbf.containsLocalBean("&x2")); assertFalse(lbf.isSingleton("x2")); assertTrue(lbf.isSingleton("&x2")); assertTrue(lbf.isPrototype("x2")); assertFalse(lbf.isPrototype("&x2")); assertTrue(lbf.isTypeMatch("x2", TestBean.class)); assertFalse(lbf.isTypeMatch("&x2", TestBean.class)); assertTrue(lbf.isTypeMatch("&x2", DummyFactory.class)); assertTrue(lbf.isTypeMatch("x2", Object.class)); assertTrue(lbf.isTypeMatch("&x2", Object.class)); assertEquals(TestBean.class, lbf.getType("x2")); assertEquals(DummyFactory.class, lbf.getType("&x2")); assertEquals(1, lbf.getAliases("x1").length); assertEquals("x2", lbf.getAliases("x1")[0]); assertEquals(1, lbf.getAliases("&x1").length); assertEquals("&x2", lbf.getAliases("&x1")[0]); assertEquals(1, lbf.getAliases("x2").length); assertEquals("x1", lbf.getAliases("x2")[0]); assertEquals(1, lbf.getAliases("&x2").length); assertEquals("&x1", lbf.getAliases("&x2")[0]); }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testBeanDefinitionRemoval() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); lbf.setAllowBeanDefinitionOverriding(false); lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class)); lbf.registerAlias("test", "test2"); lbf.preInstantiateSingletons(); lbf.removeBeanDefinition("test"); lbf.removeAlias("test2"); lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class)); lbf.registerAlias("test", "test2"); assertTrue(lbf.getBean("test") instanceof NestedTestBean); assertTrue(lbf.getBean("test2") instanceof NestedTestBean); }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testRegisterExistingSingletonWithNameOverriding() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("test.(class)", TestBean.class.getName()); p.setProperty("test.name", "Tony"); p.setProperty("test.age", "48"); p.setProperty("test.spouse(ref)", "singletonObject"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); lbf.registerBeanDefinition("singletonObject", new RootBeanDefinition(PropertiesFactoryBean.class)); Object singletonObject = new TestBean(); lbf.registerSingleton("singletonObject", singletonObject); lbf.preInstantiateSingletons(); assertTrue(lbf.isSingleton("singletonObject")); assertEquals(TestBean.class, lbf.getType("singletonObject")); TestBean test = (TestBean) lbf.getBean("test"); assertEquals(singletonObject, lbf.getBean("singletonObject")); assertEquals(singletonObject, test.getSpouse()); Map<?, ?> beansOfType = lbf.getBeansOfType(TestBean.class, false, true); assertEquals(2, beansOfType.size()); assertTrue(beansOfType.containsValue(test)); assertTrue(beansOfType.containsValue(singletonObject)); beansOfType = lbf.getBeansOfType(null, false, true); Iterator<String> beanNames = lbf.getBeanNamesIterator(); assertEquals("test", beanNames.next()); assertEquals("singletonObject", beanNames.next()); assertFalse(beanNames.hasNext());/*w w w.j av a 2s . c o m*/ assertEquals(2, beansOfType.size()); assertTrue(lbf.containsSingleton("test")); assertTrue(lbf.containsSingleton("singletonObject")); assertTrue(lbf.containsBeanDefinition("test")); assertTrue(lbf.containsBeanDefinition("singletonObject")); }