Java Code Examples for org.springframework.beans.factory.support.DefaultListableBeanFactory#getBean()
The following examples show how to use
org.springframework.beans.factory.support.DefaultListableBeanFactory#getBean() .
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: XmlBeanFactoryTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testInnerBeanInheritsScopeFromConcreteChildDefinition() { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); reader.loadBeanDefinitions(OVERRIDES_CONTEXT); TestBean jenny1 = (TestBean) xbf.getBean("jennyChild"); assertEquals(1, jenny1.getFriends().size()); Object friend1 = jenny1.getFriends().iterator().next(); assertTrue(friend1 instanceof TestBean); TestBean jenny2 = (TestBean) xbf.getBean("jennyChild"); assertEquals(1, jenny2.getFriends().size()); Object friend2 = jenny2.getFriends().iterator().next(); assertTrue(friend2 instanceof TestBean); assertNotSame(jenny1, jenny2); assertNotSame(friend1, friend2); }
Example 2
Source File: ProxyFactoryBeanTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testSerializableSingletonProxy() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS)); Person p = (Person) bf.getBean("serializableSingleton"); assertSame("Should be a Singleton", p, bf.getBean("serializableSingleton")); Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(p); assertEquals(p, p2); assertNotSame(p, p2); assertEquals("serializableSingleton", p2.getName()); // Add unserializable advice Advice nop = new NopInterceptor(); ((Advised) p).addAdvice(nop); // Check it still works assertEquals(p2.getName(), p2.getName()); assertFalse("Not serializable because an interceptor isn't serializable", SerializationTestUtils.isSerializable(p)); // Remove offending interceptor... assertTrue(((Advised) p).removeAdvice(nop)); assertTrue("Serializable again because offending interceptor was removed", SerializationTestUtils.isSerializable(p)); }
Example 3
Source File: MBeanExporterTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * SPR-3302 */ @Test public void testBeanNameCanBeUsedInNotificationListenersMap() throws Exception { String beanName = "charlesDexterWard"; BeanDefinitionBuilder testBean = BeanDefinitionBuilder.rootBeanDefinition(JmxTestBean.class); DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); factory.registerBeanDefinition(beanName, testBean.getBeanDefinition()); factory.preInstantiateSingletons(); Object testBeanInstance = factory.getBean(beanName); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(getServer()); Map<String, Object> beansToExport = new HashMap<String, Object>(); beansToExport.put("test:what=ever", testBeanInstance); exporter.setBeans(beansToExport); exporter.setBeanFactory(factory); StubNotificationListener listener = new StubNotificationListener(); exporter.setNotificationListenerMappings(Collections.singletonMap(beanName, listener)); start(exporter); }
Example 4
Source File: AutowiredAnnotationBeanPostProcessorTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testConstructorResourceInjectionWithMultipleCandidatesAsOrderedCollection() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); 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); FixedOrder2NestedTestBean ntb1 = new FixedOrder2NestedTestBean(); bf.registerSingleton("nestedTestBean1", ntb1); FixedOrder1NestedTestBean ntb2 = new FixedOrder1NestedTestBean(); bf.registerSingleton("nestedTestBean2", ntb2); ConstructorsCollectionResourceInjectionBean bean = (ConstructorsCollectionResourceInjectionBean) bf.getBean("annotatedBean"); assertNull(bean.getTestBean3()); assertSame(tb, bean.getTestBean4()); assertEquals(2, bean.getNestedTestBeans().size()); assertSame(ntb2, bean.getNestedTestBeans().get(0)); assertSame(ntb1, bean.getNestedTestBeans().get(1)); bf.destroySingletons(); }
Example 5
Source File: RequiredAnnotationBeanPostProcessorTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testWithStaticFactoryMethodAndRequiredPropertiesSpecified() { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); BeanDefinition beanDef = BeanDefinitionBuilder .genericBeanDefinition(RequiredTestBean.class) .setFactoryMethod("create") .addPropertyValue("age", "24") .addPropertyValue("favouriteColour", "Blue") .addPropertyValue("jobTitle", "Grand Poobah") .getBeanDefinition(); factory.registerBeanDefinition("testBean", beanDef); factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); factory.preInstantiateSingletons(); RequiredTestBean bean = (RequiredTestBean) factory.getBean("testBean"); assertEquals(24, bean.getAge()); assertEquals("Blue", bean.getFavouriteColour()); }
Example 6
Source File: AutowireWithExclusionTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void byTypeAutowireWithPrimaryInParentAndChild() throws Exception { CountingFactory.reset(); DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml"); parent.getBeanDefinition("props1").setPrimary(true); parent.preInstantiateSingletons(); DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent); RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class); robDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE); robDef.getPropertyValues().add("spouse", new RuntimeBeanReference("sally")); child.registerBeanDefinition("rob2", robDef); RootBeanDefinition propsDef = new RootBeanDefinition(PropertiesFactoryBean.class); propsDef.getPropertyValues().add("properties", "name=props3"); propsDef.setPrimary(true); child.registerBeanDefinition("props3", propsDef); TestBean rob = (TestBean) child.getBean("rob2"); assertEquals("props3", rob.getSomeProperties().getProperty("name")); assertEquals(1, CountingFactory.getFactoryBeanInstanceCount()); }
Example 7
Source File: XmlBeanFactoryTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testCircularReferences() { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE); reader.loadBeanDefinitions(REFTYPES_CONTEXT); TestBean jenny = (TestBean) xbf.getBean("jenny"); TestBean david = (TestBean) xbf.getBean("david"); TestBean ego = (TestBean) xbf.getBean("ego"); TestBean complexInnerEgo = (TestBean) xbf.getBean("complexInnerEgo"); TestBean complexEgo = (TestBean) xbf.getBean("complexEgo"); assertTrue("Correct circular reference", jenny.getSpouse() == david); assertTrue("Correct circular reference", david.getSpouse() == jenny); assertTrue("Correct circular reference", ego.getSpouse() == ego); assertTrue("Correct circular reference", complexInnerEgo.getSpouse().getSpouse() == complexInnerEgo); assertTrue("Correct circular reference", complexEgo.getSpouse().getSpouse() == complexEgo); }
Example 8
Source File: LocalSessionFactoryBeanTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testLocalSessionFactoryBeanWithTypeDefinitions() throws Exception { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new ClassPathResource("typeDefinitions.xml", getClass())); TypeTestLocalSessionFactoryBean sf = (TypeTestLocalSessionFactoryBean) xbf.getBean("&sessionFactory"); // Requires re-compilation when switching to Hibernate 3.5/3.6 // since Mappings changed from a class to an interface TypeDef type1 = sf.mappings.getTypeDef("type1"); TypeDef type2 = sf.mappings.getTypeDef("type2"); assertEquals("mypackage.MyTypeClass", type1.getTypeClass()); assertEquals(2, type1.getParameters().size()); assertEquals("value1", type1.getParameters().getProperty("param1")); assertEquals("othervalue", type1.getParameters().getProperty("otherParam")); assertEquals("mypackage.MyOtherTypeClass", type2.getTypeClass()); assertEquals(1, type2.getParameters().size()); assertEquals("myvalue", type2.getParameters().getProperty("myParam")); }
Example 9
Source File: ProxyFactoryBeanTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testTargetSourceNotAtEndOfInterceptorNamesIsRejected() { try { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(NOTLAST_TARGETSOURCE_CONTEXT, CLASS)); bf.getBean("targetSourceNotLast"); fail("TargetSource or non-advised object must be last in interceptorNames"); } catch (BeanCreationException ex) { // Root cause of the problem must be an AOP exception AopConfigException aex = (AopConfigException) ex.getCause(); assertTrue(aex.getMessage().contains("interceptorNames")); } }
Example 10
Source File: XmlBeanFactoryTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testInheritanceWithClass() throws Exception { DefaultListableBeanFactory parent = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT); DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent); new XmlBeanDefinitionReader(child).loadBeanDefinitions(CHILD_CONTEXT); assertEquals(DerivedTestBean.class, child.getType("inheritsWithClass")); DerivedTestBean inherits = (DerivedTestBean) child.getBean("inheritsWithClass"); // Name property value is overridden assertTrue(inherits.getName().equals("override")); // Age property is inherited from bean in parent factory assertTrue(inherits.getAge() == 1); assertTrue(inherits.wasInitialized()); }
Example 11
Source File: DefaultListableBeanFactoryTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testGetBeanByTypeInstanceDefinedInParent() { DefaultListableBeanFactory parent = new DefaultListableBeanFactory(); RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition(99); parent.registerBeanDefinition("bd1", bd1); DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(parent); ConstructorDependency bean = lbf.getBean(ConstructorDependency.class); assertThat(bean.beanName, equalTo("bd1")); assertThat(bean.spouseAge, equalTo(99)); bean = lbf.getBean(ConstructorDependency.class, 42); assertThat(bean.beanName, equalTo("bd1")); assertThat(bean.spouseAge, equalTo(42)); ObjectProvider<ConstructorDependency> provider = lbf.getBeanProvider(ConstructorDependency.class); bean = provider.getObject(); assertThat(bean.beanName, equalTo("bd1")); assertThat(bean.spouseAge, equalTo(99)); bean = provider.getObject(42); assertThat(bean.beanName, equalTo("bd1")); assertThat(bean.spouseAge, equalTo(42)); bean = provider.getIfAvailable(); assertThat(bean.beanName, equalTo("bd1")); assertThat(bean.spouseAge, equalTo(99)); bean = provider.getIfUnique(); assertThat(bean.beanName, equalTo("bd1")); assertThat(bean.spouseAge, equalTo(99)); }
Example 12
Source File: FactoryMethodTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testFactoryMethodsWithInvalidDestroyMethod() { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass())); try { xbf.getBean("defaultTestBeanWithInvalidDestroyMethod"); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { // expected } }
Example 13
Source File: PrototypeTargetTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testPrototypeProxyWithPrototypeTarget() { TestBeanImpl.constructionCount = 0; DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT); for (int i = 0; i < 10; i++) { TestBean tb = (TestBean) bf.getBean("testBeanPrototype"); tb.doSomething(); } TestInterceptor interceptor = (TestInterceptor) bf.getBean("testInterceptor"); assertEquals(10, TestBeanImpl.constructionCount); assertEquals(10, interceptor.invocationCount); }
Example 14
Source File: XmlBeanFactoryTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testConstructorArgWithDoubleSimpleTypeMatch() { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT); SingleSimpleTypeConstructorBean bean = (SingleSimpleTypeConstructorBean) xbf.getBean("beanWithBooleanAndString"); assertTrue(bean.isSecondBoolean()); assertEquals("A String", bean.getTestString()); SingleSimpleTypeConstructorBean bean2 = (SingleSimpleTypeConstructorBean) xbf.getBean("beanWithBooleanAndString2"); assertTrue(bean2.isSecondBoolean()); assertEquals("A String", bean2.getTestString()); }
Example 15
Source File: AutowireWithExclusionTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void byTypeAutowireWithInclusion() throws Exception { CountingFactory.reset(); DefaultListableBeanFactory beanFactory = getBeanFactory("autowire-with-inclusion.xml"); beanFactory.preInstantiateSingletons(); TestBean rob = (TestBean) beanFactory.getBean("rob"); assertEquals("props1", rob.getSomeProperties().getProperty("name")); assertEquals(1, CountingFactory.getFactoryBeanInstanceCount()); }
Example 16
Source File: DefaultListableBeanFactoryTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testBeanDefinitionWithAbstractClass() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); lbf.registerBeanDefinition("test", new RootBeanDefinition(AbstractBeanFactory.class)); try { lbf.getBean("test"); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { assertEquals("test", ex.getBeanName()); assertTrue(ex.getMessage().toLowerCase().contains("abstract")); } }
Example 17
Source File: InjectAnnotationBeanPostProcessorTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testOptionalFieldInjectionWithBeanNotAvailable() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalFieldInjectionBean.class)); OptionalFieldInjectionBean bean = (OptionalFieldInjectionBean) bf.getBean("annotatedBean"); assertFalse(bean.getTestBean().isPresent()); bf.destroySingletons(); }
Example 18
Source File: XmlBeanFactoryTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testAutowireModeNotInherited() { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); reader.loadBeanDefinitions(OVERRIDES_CONTEXT); TestBean david = (TestBean) xbf.getBean("magicDavid"); // the parent bean is autowiring assertNotNull(david.getSpouse()); TestBean derivedDavid = (TestBean) xbf.getBean("magicDavidDerived"); // this fails while it inherits from the child bean assertNull("autowiring not propagated along child relationships", derivedDavid.getSpouse()); }
Example 19
Source File: XmlBeanFactoryTests.java From java-technology-stack with MIT License | 5 votes |
/** * Note that prototype/singleton distinction is <b>not</b> inherited. * It's possible for a subclass singleton not to return independent * instances even if derived from a prototype */ @Test public void testSingletonInheritsFromParentFactoryPrototype() throws Exception { DefaultListableBeanFactory parent = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT); DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent); new XmlBeanDefinitionReader(child).loadBeanDefinitions(CHILD_CONTEXT); TestBean inherits = (TestBean) child.getBean("singletonInheritsFromParentFactoryPrototype"); // Name property value is overridden assertTrue(inherits.getName().equals("prototype-override")); // Age property is inherited from bean in parent factory assertTrue(inherits.getAge() == 2); TestBean inherits2 = (TestBean) child.getBean("singletonInheritsFromParentFactoryPrototype"); assertTrue(inherits2 == inherits); }
Example 20
Source File: XmlBeanFactoryTests.java From spring-analysis-note with MIT License | 4 votes |
@Test public void testConstructorArgResolution() { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT); TestBean kerry1 = (TestBean) xbf.getBean("kerry1"); TestBean kerry2 = (TestBean) xbf.getBean("kerry2"); ConstructorDependenciesBean rod9 = (ConstructorDependenciesBean) xbf.getBean("rod9"); assertEquals(99, rod9.getAge()); ConstructorDependenciesBean rod9a = (ConstructorDependenciesBean) xbf.getBean("rod9", 98); assertEquals(98, rod9a.getAge()); ConstructorDependenciesBean rod9b = (ConstructorDependenciesBean) xbf.getBean("rod9", "myName"); assertEquals("myName", rod9b.getName()); ConstructorDependenciesBean rod9c = (ConstructorDependenciesBean) xbf.getBean("rod9", 97); assertEquals(97, rod9c.getAge()); ConstructorDependenciesBean rod10 = (ConstructorDependenciesBean) xbf.getBean("rod10"); assertEquals(null, rod10.getName()); ConstructorDependenciesBean rod11 = (ConstructorDependenciesBean) xbf.getBean("rod11"); assertEquals(kerry2, rod11.getSpouse1()); ConstructorDependenciesBean rod12 = (ConstructorDependenciesBean) xbf.getBean("rod12"); assertEquals(kerry1, rod12.getSpouse1()); assertNull(rod12.getSpouse2()); ConstructorDependenciesBean rod13 = (ConstructorDependenciesBean) xbf.getBean("rod13"); assertEquals(kerry1, rod13.getSpouse1()); assertEquals(kerry2, rod13.getSpouse2()); ConstructorDependenciesBean rod14 = (ConstructorDependenciesBean) xbf.getBean("rod14"); assertEquals(kerry1, rod14.getSpouse1()); assertEquals(kerry2, rod14.getSpouse2()); ConstructorDependenciesBean rod15 = (ConstructorDependenciesBean) xbf.getBean("rod15"); assertEquals(kerry2, rod15.getSpouse1()); assertEquals(kerry1, rod15.getSpouse2()); ConstructorDependenciesBean rod16 = (ConstructorDependenciesBean) xbf.getBean("rod16"); assertEquals(kerry2, rod16.getSpouse1()); assertEquals(kerry1, rod16.getSpouse2()); assertEquals(29, rod16.getAge()); ConstructorDependenciesBean rod17 = (ConstructorDependenciesBean) xbf.getBean("rod17"); assertEquals(kerry1, rod17.getSpouse1()); assertEquals(kerry2, rod17.getSpouse2()); assertEquals(29, rod17.getAge()); }