@Test public void testSameScopeOnDifferentBeans() throws Exception { Object beanAInScope = ctx.getBean("scopedClass"); Object beanBInScope = ctx.getBean("scopedInterface"); assertNotSame(beanAInScope, beanBInScope); customScope.createNewScope = true; Object newBeanAInScope = ctx.getBean("scopedClass"); Object newBeanBInScope = ctx.getBean("scopedInterface"); assertNotSame(newBeanAInScope, newBeanBInScope); assertNotSame(newBeanAInScope, beanAInScope); assertNotSame(newBeanBInScope, beanBInScope); }
@Test public void withSingletonSupplier() { GenericApplicationContext ac = new GenericApplicationContext(); ac.registerBeanDefinition("testBean", new RootBeanDefinition(String.class, ac::toString)); ac.refresh(); assertSame(ac.getBean("testBean"), ac.getBean("testBean")); assertSame(ac.getBean("testBean"), ac.getBean(String.class)); assertSame(ac.getBean("testBean"), ac.getBean(CharSequence.class)); assertEquals(ac.toString(), ac.getBean("testBean")); }
@Test public void individualBeans() { GenericApplicationContext context = new GenericApplicationContext(); context.registerBean(BeanA.class); context.registerBean(BeanB.class); context.registerBean(BeanC.class); context.refresh(); assertSame(context.getBean(BeanB.class), context.getBean(BeanA.class).b); assertSame(context.getBean(BeanC.class), context.getBean(BeanA.class).c); assertSame(context, context.getBean(BeanB.class).applicationContext); }
@Test public void individualNamedBeans() { GenericApplicationContext context = new GenericApplicationContext(); context.registerBean("a", BeanA.class); context.registerBean("b", BeanB.class); context.registerBean("c", BeanC.class); context.refresh(); assertSame(context.getBean("b"), context.getBean("a", BeanA.class).b); assertSame(context.getBean("c"), context.getBean("a", BeanA.class).c); assertSame(context, context.getBean("b", BeanB.class).applicationContext); }
@Test public void testRawScopes() throws Exception { String beanName = "scopedProxyInterface"; // get hidden bean Object bean = ctx.getBean("scopedTarget." + beanName); assertFalse(bean instanceof ScopedObject); }
private void customBeanNameIsRespected(Class<?> testClass, Supplier<TestBean> testBeanSupplier, String beanName) { GenericApplicationContext ac = new GenericApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(ac); ac.registerBeanDefinition("config", new RootBeanDefinition(testClass)); ac.refresh(); assertSame(testBeanSupplier.get(), ac.getBean(beanName)); // method name should not be registered exception.expect(NoSuchBeanDefinitionException.class); ac.getBean("methodName"); }
@Test // SPR-2728 public void testCustomElementNestedWithinUtilSet() throws Exception { Set<?> things = (Set<?>) this.beanFactory.getBean("set.of.things"); assertNotNull(things); assertEquals(2, things.size()); }
@Test // SPR-2728 public void testCustomElementNestedWithinUtilMap() throws Exception { Map<?, ?> things = (Map<?, ?>) this.beanFactory.getBean("map.of.things"); assertNotNull(things); assertEquals(2, things.size()); }
@Test void genericApplicationContextInjectedIntoMethod( GenericApplicationContext applicationContext) { assertNotNull(applicationContext, "GenericApplicationContext should have been injected by Spring"); assertEquals(this.dilbert, applicationContext.getBean("dilbert", Person.class)); }
@Test public void asyncClassWithInterfaceAndPostProcessor() throws Exception { originalThreadName = Thread.currentThread().getName(); GenericApplicationContext context = new GenericApplicationContext(); context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncClassBeanWithInterface.class)); context.registerBeanDefinition("asyncProcessor", new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class)); context.refresh(); RegularInterface asyncTest = context.getBean("asyncTest", RegularInterface.class); asyncTest.doSomething(10); Future<String> future = asyncTest.returnSomething(20); assertEquals("20", future.get()); }
@Test public void asyncInterfaceWithPostProcessor() throws Exception { originalThreadName = Thread.currentThread().getName(); GenericApplicationContext context = new GenericApplicationContext(); context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncInterfaceBean.class)); context.registerBeanDefinition("asyncProcessor", new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class)); context.refresh(); AsyncInterface asyncTest = context.getBean("asyncTest", AsyncInterface.class); asyncTest.doSomething(10); Future<String> future = asyncTest.returnSomething(20); assertEquals("20", future.get()); }
@Test public void testAutowiredSingleConstructorSupported() { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(factory).loadBeanDefinitions( new ClassPathResource("annotation-config.xml", AutowiredConstructorConfig.class)); GenericApplicationContext ctx = new GenericApplicationContext(factory); ctx.registerBeanDefinition("config1", new RootBeanDefinition(AutowiredConstructorConfig.class)); ctx.registerBeanDefinition("config2", new RootBeanDefinition(ColorConfig.class)); ctx.refresh(); assertSame(ctx.getBean(AutowiredConstructorConfig.class).colour, ctx.getBean(Colour.class)); }
@Test public void testObjectFactoryConstructorWithTypeVariable() { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(factory).loadBeanDefinitions( new ClassPathResource("annotation-config.xml", ObjectFactoryConstructorConfig.class)); GenericApplicationContext ctx = new GenericApplicationContext(factory); ctx.registerBeanDefinition("config1", new RootBeanDefinition(ObjectFactoryConstructorConfig.class)); ctx.registerBeanDefinition("config2", new RootBeanDefinition(ColorConfig.class)); ctx.refresh(); assertSame(ctx.getBean(ObjectFactoryConstructorConfig.class).colour, ctx.getBean(Colour.class)); }
@Autowired void register(GenericApplicationContext ctx) { ctx.registerBean("spouse", TestBean.class, () -> new TestBean("functional")); Supplier<TestBean> testBeanSupplier = () -> new TestBean(ctx.getBean("spouse", TestBean.class)); ctx.registerBean(TestBean.class, testBeanSupplier, bd -> bd.setPrimary(true)); }
@Test // SPR-11830 public void configWithBeanWithProviderImplementation() { GenericApplicationContext ac = new GenericApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(ac); ac.registerBeanDefinition("config", new RootBeanDefinition(ConfigWithBeanWithProviderImplementation.class)); ac.refresh(); assertSame(ac.getBean("customName"), ConfigWithBeanWithProviderImplementation.testBean); }
@Test public void beanPostProcessorPublishesEvents() { GenericApplicationContext context = new GenericApplicationContext(); context.registerBeanDefinition("listener", new RootBeanDefinition(BeanThatListens.class)); context.registerBeanDefinition("messageSource", new RootBeanDefinition(StaticMessageSource.class)); context.registerBeanDefinition("postProcessor", new RootBeanDefinition(EventPublishingBeanPostProcessor.class)); context.refresh(); context.publishEvent(new MyEvent(this)); BeanThatListens listener = context.getBean(BeanThatListens.class); assertEquals(4, listener.getEventCount()); context.close(); }
@Test public void testProxyingDecorator() throws Exception { ITestBean bean = (ITestBean) this.beanFactory.getBean("debuggingTestBean"); assertTestBean(bean); assertTrue(AopUtils.isAopProxy(bean)); Advisor[] advisors = ((Advised) bean).getAdvisors(); assertEquals("Incorrect number of advisors", 1, advisors.length); assertEquals("Incorrect advice class", DebugInterceptor.class, advisors[0].getAdvice().getClass()); }
@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()); }
@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()); }
@Test public void testAutowireByName() { GenericApplicationContext context = new GenericApplicationContext(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context); reader.loadBeanDefinitions(LOCATION_PREFIX + "defaultAutowireByNameTests.xml"); context.refresh(); DefaultsTestBean bean = (DefaultsTestBean) context.getBean(TEST_BEAN_NAME); assertNull("constructor dependency should not have been autowired", bean.getConstructorDependency()); assertNull("propertyDependency1 should not have been autowired", bean.getPropertyDependency1()); assertNotNull("propertyDependency2 should have been autowired", bean.getPropertyDependency2()); assertEquals("pd2", bean.getPropertyDependency2().getName()); }