Java Code Examples for org.springframework.beans.factory.support.DefaultListableBeanFactory#registerSingleton()
The following examples show how to use
org.springframework.beans.factory.support.DefaultListableBeanFactory#registerSingleton() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommonAnnotationBeanPostProcessorTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testResourceInjection() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor(); bpp.setResourceFactory(bf); bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ResourceInjectionBean.class)); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); TestBean tb2 = new TestBean(); bf.registerSingleton("testBean2", tb2); ResourceInjectionBean bean = (ResourceInjectionBean) bf.getBean("annotatedBean"); assertTrue(bean.initCalled); assertTrue(bean.init2Called); assertTrue(bean.init3Called); assertSame(tb, bean.getTestBean()); assertSame(tb2, bean.getTestBean2()); bf.destroySingletons(); assertTrue(bean.destroyCalled); assertTrue(bean.destroy2Called); assertTrue(bean.destroy3Called); }
Example 2
Source File: InjectAnnotationBeanPostProcessorTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testConstructorResourceInjectionWithMultipleCandidatesAsCollection() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorsCollectionResourceInjectionBean.class)); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); NestedTestBean ntb1 = new NestedTestBean(); bf.registerSingleton("nestedTestBean1", ntb1); NestedTestBean ntb2 = new NestedTestBean(); bf.registerSingleton("nestedTestBean2", ntb2); ConstructorsCollectionResourceInjectionBean bean = (ConstructorsCollectionResourceInjectionBean) bf.getBean("annotatedBean"); assertNull(bean.getTestBean3()); assertSame(tb, bean.getTestBean4()); assertEquals(2, bean.getNestedTestBeans().size()); assertSame(ntb1, bean.getNestedTestBeans().get(0)); assertSame(ntb2, bean.getNestedTestBeans().get(1)); bf.destroySingletons(); }
Example 3
Source File: DefaultListableBeanFactoryTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testCustomEditorWithBeanReference() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); lbf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() { @Override public void registerCustomEditors(PropertyEditorRegistry registry) { NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN); registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true)); } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("myFloat", new RuntimeBeanReference("myFloat")); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("testBean", bd); lbf.registerSingleton("myFloat", "1,1"); TestBean testBean = (TestBean) lbf.getBean("testBean"); assertTrue(testBean.getMyFloat().floatValue() == 1.1f); }
Example 4
Source File: CommonAnnotationBeanPostProcessorTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testResourceInjectionWithTwoProcessors() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor(); bpp.setResourceFactory(bf); bf.addBeanPostProcessor(bpp); CommonAnnotationBeanPostProcessor bpp2 = new CommonAnnotationBeanPostProcessor(); bpp2.setResourceFactory(bf); bf.addBeanPostProcessor(bpp2); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ResourceInjectionBean.class)); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); TestBean tb2 = new TestBean(); bf.registerSingleton("testBean2", tb2); ResourceInjectionBean bean = (ResourceInjectionBean) bf.getBean("annotatedBean"); assertTrue(bean.initCalled); assertTrue(bean.init2Called); assertSame(tb, bean.getTestBean()); assertSame(tb2, bean.getTestBean2()); bf.destroySingletons(); assertTrue(bean.destroyCalled); assertTrue(bean.destroy2Called); }
Example 5
Source File: MBeanExporterTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testIgnoreBeanName() throws MalformedObjectNameException { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); String firstBeanName = "spring:type=TestBean"; factory.registerSingleton(firstBeanName, new TestBean("test")); String secondBeanName = "spring:type=TestBean2"; factory.registerSingleton(secondBeanName, new TestBean("test2")); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(getServer()); exporter.setAssembler(new NamedBeanAutodetectCapableMBeanInfoAssemblerStub(firstBeanName, secondBeanName)); exporter.setBeanFactory(factory); exporter.setAutodetectMode(MBeanExporter.AUTODETECT_ALL); exporter.addExcludedBean(secondBeanName); start(exporter); assertIsRegistered("Bean not autodetected in (AUTODETECT_ALL) mode", ObjectNameManager.getInstance(firstBeanName)); assertIsNotRegistered("Bean should have been excluded", ObjectNameManager.getInstance(secondBeanName)); }
Example 6
Source File: AutowiredAnnotationBeanPostProcessorTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testGenericsBasedFieldInjectionWithSimpleMatch() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(RepositoryFieldInjectionBeanWithSimpleMatch.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); bf.registerSingleton("repo", new StringRepository()); RepositoryFieldInjectionBeanWithSimpleMatch bean = (RepositoryFieldInjectionBeanWithSimpleMatch) bf.getBean("annotatedBean"); Repository repo = bf.getBean("repo", Repository.class); assertSame(repo, bean.repository); assertSame(repo, bean.stringRepository); assertSame(1, bean.repositoryArray.length); assertSame(1, bean.stringRepositoryArray.length); assertSame(repo, bean.repositoryArray[0]); assertSame(repo, bean.stringRepositoryArray[0]); assertSame(1, bean.repositoryList.size()); assertSame(1, bean.stringRepositoryList.size()); assertSame(repo, bean.repositoryList.get(0)); assertSame(repo, bean.stringRepositoryList.get(0)); assertSame(1, bean.repositoryMap.size()); assertSame(1, bean.stringRepositoryMap.size()); assertSame(repo, bean.repositoryMap.get("repo")); assertSame(repo, bean.stringRepositoryMap.get("repo")); }
Example 7
Source File: DefaultListableBeanFactoryTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testArrayConstructorWithAutowiring() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerSingleton("integer1", new Integer(4)); bf.registerSingleton("integer2", new Integer(5)); RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); bf.registerBeanDefinition("arrayBean", rbd); ArrayBean ab = (ArrayBean) bf.getBean("arrayBean"); assertEquals(new Integer(4), ab.getIntegerArray()[0]); assertEquals(new Integer(5), ab.getIntegerArray()[1]); }
Example 8
Source File: AutowiredAnnotationBeanPostProcessorTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testConstructorInjectionWithMap() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(MapConstructorInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); TestBean tb1 = new TestBean(); TestBean tb2 = new TestBean(); bf.registerSingleton("testBean1", tb1); bf.registerSingleton("testBean2", tb1); MapConstructorInjectionBean bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean"); assertEquals(2, bean.getTestBeanMap().size()); assertTrue(bean.getTestBeanMap().keySet().contains("testBean1")); assertTrue(bean.getTestBeanMap().keySet().contains("testBean2")); assertTrue(bean.getTestBeanMap().values().contains(tb1)); assertTrue(bean.getTestBeanMap().values().contains(tb2)); bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean"); assertEquals(2, bean.getTestBeanMap().size()); assertTrue(bean.getTestBeanMap().keySet().contains("testBean1")); assertTrue(bean.getTestBeanMap().keySet().contains("testBean2")); assertTrue(bean.getTestBeanMap().values().contains(tb1)); assertTrue(bean.getTestBeanMap().values().contains(tb2)); }
Example 9
Source File: NotificationListenerTests.java From java-technology-stack with MIT License | 5 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testRegisterNotificationListenerWithBeanNameAndBeanNameInBeansMap() throws Exception { String beanName = "testBean"; ObjectName objectName = ObjectName.getInstance("spring:name=Test"); SelfNamingTestBean testBean = new SelfNamingTestBean(); testBean.setObjectName(objectName); DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); factory.registerSingleton(beanName, testBean); Map<String, Object> beans = new HashMap<>(); beans.put(beanName, beanName); Map listenerMappings = new HashMap(); CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener(); listenerMappings.put(beanName, listener); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(server); exporter.setBeans(beans); exporter.setNotificationListenerMappings(listenerMappings); exporter.setBeanFactory(factory); start(exporter); assertIsRegistered("Should have registered MBean", objectName); server.setAttribute(objectName, new Attribute("Age", new Integer(77))); assertEquals("Listener not notified", 1, listener.getCount("Age")); }
Example 10
Source File: SpringContextUtils.java From platform with Apache License 2.0 | 5 votes |
/** * 动态注册单例对象 * * @param beanName 对象名称 * @param singletonObject 对象实例 */ public static void registerSingleton(String beanName, Object singletonObject) { DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) SpringContextUtils.applicationContext.getAutowireCapableBeanFactory(); if (!beanFactory.containsSingleton(beanName)) { beanFactory.registerSingleton(beanName, singletonObject); } else { log.error("singleton bean with name [{}] allready exists.", beanName); } }
Example 11
Source File: DefaultListableBeanFactoryTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testRegisterExistingSingletonWithAlreadyBound() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Object singletonObject = new TestBean(); lbf.registerSingleton("singletonObject", singletonObject); try { lbf.registerSingleton("singletonObject", singletonObject); fail("Should have thrown IllegalStateException"); } catch (IllegalStateException ex) { // expected } }
Example 12
Source File: DefaultListableBeanFactoryTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@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()); assertEquals(2, beansOfType.size()); assertTrue(lbf.containsSingleton("test")); assertTrue(lbf.containsSingleton("singletonObject")); assertTrue(lbf.containsBeanDefinition("test")); assertTrue(lbf.containsBeanDefinition("singletonObject")); }
Example 13
Source File: BeihuMutilElasticsearchJestAutoConfiguration.java From beihu-boot with Apache License 2.0 | 5 votes |
private void registerBean(String name, Object obj) { // 获取BeanFactory DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) applicationContext .getAutowireCapableBeanFactory(); // 动态注册bean. defaultListableBeanFactory.registerSingleton(name, obj); }
Example 14
Source File: CommonAnnotationBeanPostProcessorTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testExtendedEjbInjection() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor(); bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); bf.registerResolvableDependency(BeanFactory.class, bf); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ExtendedEjbInjectionBean.class)); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); TestBean tb2 = new TestBean(); bf.registerSingleton("testBean2", tb2); TestBean tb3 = new TestBean(); bf.registerSingleton("testBean3", tb3); TestBean tb4 = new TestBean(); bf.registerSingleton("testBean4", tb4); NestedTestBean tb6 = new NestedTestBean(); bf.registerSingleton("xy", tb6); bf.registerAlias("xy", "testBean9"); ExtendedEjbInjectionBean bean = (ExtendedEjbInjectionBean) bf.getBean("annotatedBean"); assertTrue(bean.initCalled); assertTrue(bean.init2Called); assertSame(tb, bean.getTestBean()); assertSame(tb2, bean.getTestBean2()); assertSame(tb4, bean.getTestBean3()); assertSame(tb3, bean.getTestBean4()); assertSame(tb6, bean.testBean5); assertSame(tb6, bean.testBean6); assertSame(bf, bean.beanFactory); bf.destroySingletons(); assertTrue(bean.destroyCalled); assertTrue(bean.destroy2Called); }
Example 15
Source File: DefaultListableBeanFactoryTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test(timeout = 1000) public void testRegistrationOfManySingletonsIsFastEnough() { Assume.group(TestGroup.PERFORMANCE); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("b", new RootBeanDefinition(B.class)); // bf.getBean("b"); for (int i = 0; i < 100000; i++) { bf.registerSingleton("a" + i, new A()); } }
Example 16
Source File: DefaultListableBeanFactoryTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testArrayConstructorWithAutowiring() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerSingleton("integer1", new Integer(4)); bf.registerSingleton("integer2", new Integer(5)); RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); bf.registerBeanDefinition("arrayBean", rbd); ArrayBean ab = (ArrayBean) bf.getBean("arrayBean"); assertEquals(new Integer(4), ab.getIntegerArray()[0]); assertEquals(new Integer(5), ab.getIntegerArray()[1]); }
Example 17
Source File: ExtensionGrantIdentityProviderFactoryPostProcessor.java From graviteeio-access-management with Apache License 2.0 | 4 votes |
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException { DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) configurableListableBeanFactory; beanFactory.registerSingleton("authenticationProvider", authenticationProvider); }
Example 18
Source File: NotificationListenerTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testRegisterNotificationListenerWithTwoBeanNamesMappedToDifferentBeanInstances() throws Exception { String beanName1 = "testBean1"; String beanName2 = "testBean2"; ObjectName objectName1 = ObjectName.getInstance("spring:name=Test1"); ObjectName objectName2 = ObjectName.getInstance("spring:name=Test2"); SelfNamingTestBean testBean1 = new SelfNamingTestBean(); testBean1.setObjectName(objectName1); SelfNamingTestBean testBean2 = new SelfNamingTestBean(); testBean2.setObjectName(objectName2); DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); factory.registerSingleton(beanName1, testBean1); factory.registerSingleton(beanName2, testBean2); Map<String, Object> beans = new HashMap<String, Object>(); beans.put(beanName1, testBean1); beans.put(beanName2, testBean2); Map listenerMappings = new HashMap(); CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener(); listenerMappings.put(beanName1, listener); listenerMappings.put(beanName2, listener); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(server); exporter.setBeans(beans); exporter.setNotificationListenerMappings(listenerMappings); exporter.setBeanFactory(factory); start(exporter); assertIsRegistered("Should have registered MBean", objectName1); assertIsRegistered("Should have registered MBean", objectName2); server.setAttribute(ObjectNameManager.getInstance(objectName1), new Attribute("Age", new Integer(77))); assertEquals("Listener not notified for testBean1", 1, listener.getCount("Age")); server.setAttribute(ObjectNameManager.getInstance(objectName2), new Attribute("Age", new Integer(33))); assertEquals("Listener not notified for testBean2", 2, listener.getCount("Age")); }
Example 19
Source File: CommonAnnotationBeanPostProcessorTests.java From java-technology-stack with MIT License | 4 votes |
@Test public void testExtendedResourceInjectionWithOverriding() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor(); bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); bf.registerResolvableDependency(BeanFactory.class, bf); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.setProperty("tb", "testBean3"); ppc.setProperties(props); ppc.postProcessBeanFactory(bf); RootBeanDefinition annotatedBd = new RootBeanDefinition(ExtendedResourceInjectionBean.class); TestBean tb5 = new TestBean(); annotatedBd.getPropertyValues().add("testBean2", tb5); bf.registerBeanDefinition("annotatedBean", annotatedBd); bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(NamedResourceInjectionBean.class)); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); TestBean tb2 = new TestBean(); bf.registerSingleton("testBean2", tb2); TestBean tb3 = new TestBean(); bf.registerSingleton("testBean3", tb3); TestBean tb4 = new TestBean(); bf.registerSingleton("testBean4", tb4); NestedTestBean tb6 = new NestedTestBean(); bf.registerSingleton("xy", tb6); ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean"); assertTrue(bean.initCalled); assertTrue(bean.init2Called); assertSame(tb, bean.getTestBean()); assertSame(tb5, bean.getTestBean2()); assertSame(tb4, bean.getTestBean3()); assertSame(tb3, bean.getTestBean4()); assertSame(tb6, bean.testBean5); assertSame(tb6, bean.testBean6); assertSame(bf, bean.beanFactory); try { bf.getBean("annotatedBean2"); } catch (BeanCreationException ex) { assertTrue(ex.getRootCause() instanceof NoSuchBeanDefinitionException); NoSuchBeanDefinitionException innerEx = (NoSuchBeanDefinitionException) ex.getRootCause(); assertEquals("testBean9", innerEx.getBeanName()); } bf.destroySingletons(); assertTrue(bean.destroyCalled); assertTrue(bean.destroy2Called); }
Example 20
Source File: PropertiesBeanFactoryPostProcessor.java From graviteeio-access-management with Apache License 2.0 | 4 votes |
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException { DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) configurableListableBeanFactory; beanFactory.registerSingleton("graviteeProperties", properties); }