Java Code Examples for org.springframework.context.support.GenericApplicationContext#getBean()
The following examples show how to use
org.springframework.context.support.GenericApplicationContext#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: InjectAnnotationAutowireContextTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testAutowiredFieldResolvesQualifiedCandidateWithDefaultValueAndNoValueOnBeanDefinition() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs1 = new ConstructorArgumentValues(); cavs1.addGenericArgumentValue(JUERGEN); RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null); // qualifier added, but includes no value person1.addQualifier(new AutowireCandidateQualifier(TestQualifierWithDefaultValue.class)); ConstructorArgumentValues cavs2 = new ConstructorArgumentValues(); cavs2.addGenericArgumentValue(MARK); RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null); context.registerBeanDefinition(JUERGEN, person1); context.registerBeanDefinition(MARK, person2); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedFieldWithDefaultValueTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); context.refresh(); QualifiedFieldWithDefaultValueTestBean bean = (QualifiedFieldWithDefaultValueTestBean) context.getBean("autowired"); assertEquals(JUERGEN, bean.getPerson().getName()); }
Example 2
Source File: InjectAnnotationAutowireContextTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testAutowiredMethodParameterWithSingleQualifiedCandidate() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs = new ConstructorArgumentValues(); cavs.addGenericArgumentValue(JUERGEN); RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null); person.addQualifier(new AutowireCandidateQualifier(TestQualifier.class)); context.registerBeanDefinition(JUERGEN, person); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedMethodParameterTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); context.refresh(); QualifiedMethodParameterTestBean bean = (QualifiedMethodParameterTestBean) context.getBean("autowired"); assertEquals(JUERGEN, bean.getPerson().getName()); }
Example 3
Source File: QualifierAnnotationAutowireContextTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void autowiredMethodParameterWithStaticallyQualifiedCandidate() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs = new ConstructorArgumentValues(); cavs.addGenericArgumentValue(JUERGEN); RootBeanDefinition person = new RootBeanDefinition(QualifiedPerson.class, cavs, null); context.registerBeanDefinition(JUERGEN, ScopedProxyUtils.createScopedProxy(new BeanDefinitionHolder(person, JUERGEN), context, true).getBeanDefinition()); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedMethodParameterTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); context.refresh(); QualifiedMethodParameterTestBean bean = (QualifiedMethodParameterTestBean) context.getBean("autowired"); assertEquals(JUERGEN, bean.getPerson().getName()); }
Example 4
Source File: AnnotationProcessorPerformanceTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testPrototypeCreationWithOverriddenResourcePropertiesIsFastEnough() { GenericApplicationContext ctx = new GenericApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx); ctx.refresh(); RootBeanDefinition rbd = new RootBeanDefinition(ResourceAnnotatedTestBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); rbd.getPropertyValues().add("spouse", new RuntimeBeanReference("spouse")); ctx.registerBeanDefinition("test", rbd); ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class)); TestBean spouse = (TestBean) ctx.getBean("spouse"); StopWatch sw = new StopWatch(); sw.start("prototype"); for (int i = 0; i < 100000; i++) { TestBean tb = (TestBean) ctx.getBean("test"); assertSame(spouse, tb.getSpouse()); } sw.stop(); assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000); }
Example 5
Source File: QualifierAnnotationAutowireContextTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void autowiredFieldResolvesWithBaseQualifierAndDefaultValue() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs1 = new ConstructorArgumentValues(); cavs1.addGenericArgumentValue(JUERGEN); RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null); ConstructorArgumentValues cavs2 = new ConstructorArgumentValues(); cavs2.addGenericArgumentValue(MARK); RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null); person2.addQualifier(new AutowireCandidateQualifier(Qualifier.class)); context.registerBeanDefinition(JUERGEN, person1); context.registerBeanDefinition(MARK, person2); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedFieldWithBaseQualifierDefaultValueTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); context.refresh(); QualifiedFieldWithBaseQualifierDefaultValueTestBean bean = (QualifiedFieldWithBaseQualifierDefaultValueTestBean) context.getBean("autowired"); assertEquals(MARK, bean.getPerson().getName()); }
Example 6
Source File: QualifierAnnotationAutowireContextTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void autowiredFieldResolvesQualifiedCandidateWithDefaultValueAndNoValueOnBeanDefinition() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs1 = new ConstructorArgumentValues(); cavs1.addGenericArgumentValue(JUERGEN); RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null); // qualifier added, but includes no value person1.addQualifier(new AutowireCandidateQualifier(TestQualifierWithDefaultValue.class)); ConstructorArgumentValues cavs2 = new ConstructorArgumentValues(); cavs2.addGenericArgumentValue(MARK); RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null); context.registerBeanDefinition(JUERGEN, person1); context.registerBeanDefinition(MARK, person2); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedFieldWithDefaultValueTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); context.refresh(); QualifiedFieldWithDefaultValueTestBean bean = (QualifiedFieldWithDefaultValueTestBean) context.getBean("autowired"); assertEquals(JUERGEN, bean.getPerson().getName()); }
Example 7
Source File: SpringAtInjectTck.java From spring4-understanding with Apache License 2.0 | 6 votes |
public static Test suite() { GenericApplicationContext ac = new GenericApplicationContext(); AnnotatedBeanDefinitionReader bdr = new AnnotatedBeanDefinitionReader(ac); bdr.setScopeMetadataResolver(new Jsr330ScopeMetadataResolver()); bdr.registerBean(Convertible.class); bdr.registerBean(DriversSeat.class, Drivers.class); bdr.registerBean(Seat.class, Primary.class); bdr.registerBean(V8Engine.class); bdr.registerBean(SpareTire.class, "spare"); bdr.registerBean(Cupholder.class); bdr.registerBean(Tire.class, Primary.class); bdr.registerBean(FuelTank.class); ac.refresh(); Car car = ac.getBean(Car.class); return Tck.testsFor(car, false, true); }
Example 8
Source File: AsyncExecutionTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void asyncMethodsInInterface() throws Exception { originalThreadName = Thread.currentThread().getName(); GenericApplicationContext context = new GenericApplicationContext(); context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncMethodsInterfaceBean.class)); context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class)); context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class)); context.refresh(); AsyncMethodsInterface asyncTest = context.getBean("asyncTest", AsyncMethodsInterface.class); asyncTest.doNothing(5); asyncTest.doSomething(10); Future<String> future = asyncTest.returnSomething(20); assertEquals("20", future.get()); }
Example 9
Source File: AsyncExecutionTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void dynamicAsyncMethodsInInterfaceWithPostProcessor() throws Exception { originalThreadName = Thread.currentThread().getName(); GenericApplicationContext context = new GenericApplicationContext(); context.registerBeanDefinition("asyncTest", new RootBeanDefinition(DynamicAsyncMethodsInterfaceBean.class)); context.registerBeanDefinition("asyncProcessor", new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class)); context.refresh(); AsyncMethodsInterface asyncTest = context.getBean("asyncTest", AsyncMethodsInterface.class); asyncTest.doSomething(10); Future<String> future = asyncTest.returnSomething(20); assertEquals("20", future.get()); }
Example 10
Source File: ScopedProxyTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testScopedOverride() throws Exception { GenericApplicationContext ctx = new GenericApplicationContext(); new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(OVERRIDE_CONTEXT); SimpleMapScope scope = new SimpleMapScope(); ctx.getBeanFactory().registerScope("request", scope); ctx.refresh(); ITestBean bean = (ITestBean) ctx.getBean("testBean"); assertEquals("male", bean.getName()); assertEquals(99, bean.getAge()); assertTrue(scope.getMap().containsKey("scopedTarget.testBean")); assertEquals(TestBean.class, scope.getMap().get("scopedTarget.testBean").getClass()); }
Example 11
Source File: ComponentScanParserBeanDefinitionDefaultsTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testDefaultNonExistingInitAndDestroyMethodsDefined() { GenericApplicationContext context = new GenericApplicationContext(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context); reader.loadBeanDefinitions(LOCATION_PREFIX + "defaultNonExistingInitAndDestroyMethodsTests.xml"); context.refresh(); DefaultsTestBean bean = (DefaultsTestBean) context.getBean(TEST_BEAN_NAME); assertFalse("bean should not have been initialized", bean.isInitialized()); context.close(); assertFalse("bean should not have been destroyed", bean.isDestroyed()); }
Example 12
Source File: ApplicationContextExpressionTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void prototypeCreationReevaluatesExpressions() { GenericApplicationContext ac = new GenericApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(ac); GenericConversionService cs = new GenericConversionService(); cs.addConverter(String.class, String.class, String::trim); ac.getBeanFactory().registerSingleton(GenericApplicationContext.CONVERSION_SERVICE_BEAN_NAME, cs); RootBeanDefinition rbd = new RootBeanDefinition(PrototypeTestBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); rbd.getPropertyValues().add("country", "#{systemProperties.country}"); rbd.getPropertyValues().add("country2", new TypedStringValue("-#{systemProperties.country}-")); ac.registerBeanDefinition("test", rbd); ac.refresh(); try { System.getProperties().put("name", "juergen1"); System.getProperties().put("country", " UK1 "); PrototypeTestBean tb = (PrototypeTestBean) ac.getBean("test"); assertEquals("juergen1", tb.getName()); assertEquals("UK1", tb.getCountry()); assertEquals("-UK1-", tb.getCountry2()); System.getProperties().put("name", "juergen2"); System.getProperties().put("country", " UK2 "); tb = (PrototypeTestBean) ac.getBean("test"); assertEquals("juergen2", tb.getName()); assertEquals("UK2", tb.getCountry()); assertEquals("-UK2-", tb.getCountry2()); } finally { System.getProperties().remove("name"); System.getProperties().remove("country"); } }
Example 13
Source File: FormattingConversionServiceTests.java From spring-analysis-note with MIT License | 5 votes |
@Test @SuppressWarnings("resource") public void formatFieldForAnnotationWithPlaceholdersAndFactoryBean() throws Exception { GenericApplicationContext context = new GenericApplicationContext(); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.setProperty("dateStyle", "S-"); props.setProperty("datePattern", "M-d-yy"); ppc.setProperties(props); context.registerBeanDefinition("formattingService", new RootBeanDefinition(FormattingConversionServiceFactoryBean.class)); context.getBeanFactory().registerSingleton("ppc", ppc); context.refresh(); formattingService = context.getBean("formattingService", FormattingConversionService.class); doTestFormatFieldForAnnotation(ModelWithPlaceholders.class, false); }
Example 14
Source File: PersistenceInjectionTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testPublicPersistenceUnitSetter() { GenericApplicationContext gac = new GenericApplicationContext(); gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf); gac.registerBeanDefinition("annotationProcessor", new RootBeanDefinition(PersistenceAnnotationBeanPostProcessor.class)); gac.registerBeanDefinition(DefaultPublicPersistenceUnitSetter.class.getName(), new RootBeanDefinition(DefaultPublicPersistenceUnitSetter.class)); gac.refresh(); DefaultPublicPersistenceUnitSetter bean = (DefaultPublicPersistenceUnitSetter) gac.getBean( DefaultPublicPersistenceUnitSetter.class.getName()); assertSame(mockEmf, bean.emf); }
Example 15
Source File: FormattingConversionServiceTests.java From java-technology-stack with MIT License | 5 votes |
@Test @SuppressWarnings("resource") public void formatFieldForAnnotationWithPlaceholdersAndFactoryBean() throws Exception { GenericApplicationContext context = new GenericApplicationContext(); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.setProperty("dateStyle", "S-"); props.setProperty("datePattern", "M-d-yy"); ppc.setProperties(props); context.registerBeanDefinition("formattingService", new RootBeanDefinition(FormattingConversionServiceFactoryBean.class)); context.getBeanFactory().registerSingleton("ppc", ppc); context.refresh(); formattingService = context.getBean("formattingService", FormattingConversionService.class); doTestFormatFieldForAnnotation(ModelWithPlaceholders.class, false); }
Example 16
Source File: ScriptFactoryPostProcessorTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testRefreshedScriptReferencePropagatesToCollaborators() throws Exception { BeanDefinition processorBeanDefinition = createScriptFactoryPostProcessor(true); BeanDefinition scriptedBeanDefinition = createScriptedGroovyBean(); BeanDefinitionBuilder collaboratorBuilder = BeanDefinitionBuilder.rootBeanDefinition(DefaultMessengerService.class); collaboratorBuilder.addPropertyReference(MESSENGER_BEAN_NAME, MESSENGER_BEAN_NAME); GenericApplicationContext ctx = new GenericApplicationContext(); ctx.registerBeanDefinition(PROCESSOR_BEAN_NAME, processorBeanDefinition); ctx.registerBeanDefinition(MESSENGER_BEAN_NAME, scriptedBeanDefinition); final String collaboratorBeanName = "collaborator"; ctx.registerBeanDefinition(collaboratorBeanName, collaboratorBuilder.getBeanDefinition()); ctx.refresh(); Messenger messenger = (Messenger) ctx.getBean(MESSENGER_BEAN_NAME); assertEquals(MESSAGE_TEXT, messenger.getMessage()); // cool; now let's change the script and check the refresh behaviour... pauseToLetRefreshDelayKickIn(DEFAULT_SECONDS_TO_PAUSE); StaticScriptSource source = getScriptSource(ctx); source.setScript(CHANGED_SCRIPT); Messenger refreshedMessenger = (Messenger) ctx.getBean(MESSENGER_BEAN_NAME); // the updated script surrounds the message in quotes before returning... assertEquals(EXPECTED_CHANGED_MESSAGE_TEXT, refreshedMessenger.getMessage()); // ok, is this change reflected in the reference that the collaborator has? DefaultMessengerService collaborator = (DefaultMessengerService) ctx.getBean(collaboratorBeanName); assertEquals(EXPECTED_CHANGED_MESSAGE_TEXT, collaborator.getMessage()); }
Example 17
Source File: QualifierAnnotationAutowireContextTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void autowiredFieldWithSingleQualifiedCandidate() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs = new ConstructorArgumentValues(); cavs.addGenericArgumentValue(JUERGEN); RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null); person.addQualifier(new AutowireCandidateQualifier(TestQualifier.class)); context.registerBeanDefinition(JUERGEN, person); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedFieldTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); context.refresh(); QualifiedFieldTestBean bean = (QualifiedFieldTestBean) context.getBean("autowired"); assertEquals(JUERGEN, bean.getPerson().getName()); }
Example 18
Source File: PersistenceInjectionTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testPrivateVendorSpecificPersistenceContextField() { GenericApplicationContext gac = new GenericApplicationContext(); gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf); gac.registerBeanDefinition("annotationProcessor", new RootBeanDefinition(PersistenceAnnotationBeanPostProcessor.class)); gac.registerBeanDefinition(DefaultVendorSpecificPrivatePersistenceContextField.class.getName(), new RootBeanDefinition(DefaultVendorSpecificPrivatePersistenceContextField.class)); gac.refresh(); DefaultVendorSpecificPrivatePersistenceContextField bean = (DefaultVendorSpecificPrivatePersistenceContextField) gac.getBean(DefaultVendorSpecificPrivatePersistenceContextField.class.getName()); assertNotNull(bean.em); }
Example 19
Source File: ComponentScanParserBeanDefinitionDefaultsTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testAutowireConstructor() { GenericApplicationContext context = new GenericApplicationContext(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context); reader.loadBeanDefinitions(LOCATION_PREFIX + "defaultAutowireConstructorTests.xml"); context.refresh(); DefaultsTestBean bean = (DefaultsTestBean) context.getBean(TEST_BEAN_NAME); assertNotNull("constructor dependency should have been autowired", bean.getConstructorDependency()); assertEquals("cd", bean.getConstructorDependency().getName()); assertNull("property dependencies should not have been autowired", bean.getPropertyDependency1()); assertNull("property dependencies should not have been autowired", bean.getPropertyDependency2()); }
Example 20
Source File: ApplicationContextExpressionTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void prototypeCreationIsFastEnough() { Assume.group(TestGroup.PERFORMANCE); Assume.notLogging(factoryLog); GenericApplicationContext ac = new GenericApplicationContext(); RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); rbd.getConstructorArgumentValues().addGenericArgumentValue("#{systemProperties.name}"); rbd.getPropertyValues().add("country", "#{systemProperties.country}"); ac.registerBeanDefinition("test", rbd); ac.refresh(); StopWatch sw = new StopWatch(); sw.start("prototype"); System.getProperties().put("name", "juergen"); System.getProperties().put("country", "UK"); try { for (int i = 0; i < 100000; i++) { TestBean tb = (TestBean) ac.getBean("test"); assertEquals("juergen", tb.getName()); assertEquals("UK", tb.getCountry()); } sw.stop(); } finally { System.getProperties().remove("country"); System.getProperties().remove("name"); } assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 6000); }