|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2011 the original author or authors. |
| 2 | + * Copyright 2002-2012 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
32 | 32 | import org.springframework.beans.factory.BeanCreationException;
|
33 | 33 | import org.springframework.beans.factory.BeanFactory;
|
34 | 34 | import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
| 35 | +import org.springframework.beans.factory.ObjectFactory; |
35 | 36 | import org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor;
|
36 | 37 | import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
|
37 | 38 | import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
|
@@ -164,6 +165,72 @@ public void testResourceInjection() {
|
164 | 165 | assertTrue(bean.destroy3Called);
|
165 | 166 | }
|
166 | 167 |
|
| 168 | + @Test |
| 169 | + public void testResourceInjectionWithPrototypes() { |
| 170 | + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); |
| 171 | + CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor(); |
| 172 | + bpp.setResourceFactory(bf); |
| 173 | + bf.addBeanPostProcessor(bpp); |
| 174 | + bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ResourceInjectionBean.class, false)); |
| 175 | + bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class, false)); |
| 176 | + bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class, false)); |
| 177 | + |
| 178 | + ResourceInjectionBean bean = (ResourceInjectionBean) bf.getBean("annotatedBean"); |
| 179 | + assertTrue(bean.initCalled); |
| 180 | + assertTrue(bean.init2Called); |
| 181 | + assertTrue(bean.init3Called); |
| 182 | + |
| 183 | + TestBean tb = bean.getTestBean(); |
| 184 | + TestBean tb2 = bean.getTestBean2(); |
| 185 | + assertNotNull(tb); |
| 186 | + assertNotNull(tb2); |
| 187 | + |
| 188 | + ResourceInjectionBean anotherBean = (ResourceInjectionBean) bf.getBean("annotatedBean"); |
| 189 | + assertNotSame(anotherBean, bean); |
| 190 | + assertNotSame(anotherBean.getTestBean(), tb); |
| 191 | + assertNotSame(anotherBean.getTestBean2(), tb2); |
| 192 | + |
| 193 | + bf.destroyBean("annotatedBean", bean); |
| 194 | + assertTrue(bean.destroyCalled); |
| 195 | + assertTrue(bean.destroy2Called); |
| 196 | + assertTrue(bean.destroy3Called); |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + public void testResourceInjectionWithResolvableDependencyType() { |
| 201 | + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); |
| 202 | + CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor(); |
| 203 | + bpp.setBeanFactory(bf); |
| 204 | + bf.addBeanPostProcessor(bpp); |
| 205 | + bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ExtendedResourceInjectionBean.class, false)); |
| 206 | + bf.registerBeanDefinition("testBean4", new RootBeanDefinition(TestBean.class, false)); |
| 207 | + |
| 208 | + bf.registerResolvableDependency(BeanFactory.class, bf); |
| 209 | + bf.registerResolvableDependency(INestedTestBean.class, new ObjectFactory() { |
| 210 | + public Object getObject() throws BeansException { |
| 211 | + return new NestedTestBean(); |
| 212 | + } |
| 213 | + }); |
| 214 | + |
| 215 | + PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); |
| 216 | + Properties props = new Properties(); |
| 217 | + props.setProperty("tb", "testBean4"); |
| 218 | + ppc.setProperties(props); |
| 219 | + ppc.postProcessBeanFactory(bf); |
| 220 | + |
| 221 | + ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean"); |
| 222 | + INestedTestBean tb = bean.getTestBean6(); |
| 223 | + assertNotNull(tb); |
| 224 | + |
| 225 | + ExtendedResourceInjectionBean anotherBean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean"); |
| 226 | + assertNotSame(anotherBean, bean); |
| 227 | + assertNotSame(anotherBean.getTestBean6(), tb); |
| 228 | + |
| 229 | + String[] depBeans = bf.getDependenciesForBean("annotatedBean"); |
| 230 | + assertEquals(1, depBeans.length); |
| 231 | + assertEquals("testBean4", depBeans[0]); |
| 232 | + } |
| 233 | + |
167 | 234 | @Test
|
168 | 235 | public void testResourceInjectionWithTwoProcessors() {
|
169 | 236 | DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
@@ -522,6 +589,14 @@ public ITestBean getTestBean4() {
|
522 | 589 | return testBean4;
|
523 | 590 | }
|
524 | 591 |
|
| 592 | + public INestedTestBean getTestBean5() { |
| 593 | + return testBean5; |
| 594 | + } |
| 595 | + |
| 596 | + public INestedTestBean getTestBean6() { |
| 597 | + return testBean6; |
| 598 | + } |
| 599 | + |
525 | 600 | @PostConstruct
|
526 | 601 | protected void init2() {
|
527 | 602 | if (this.testBean3 == null || this.testBean4 == null) {
|
|
0 commit comments