Skip to content

Commit 1971870

Browse files
committed
@resource processing properly works with scoped beans and prototypes again
Issue: SPR-9627
1 parent 8599dfd commit 1971870

File tree

2 files changed

+80
-17
lines changed

2 files changed

+80
-17
lines changed

org.springframework.context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import java.util.Map;
3636
import java.util.Set;
3737
import java.util.concurrent.ConcurrentHashMap;
38-
3938
import javax.annotation.PostConstruct;
4039
import javax.annotation.PreDestroy;
4140
import javax.annotation.Resource;
@@ -447,7 +446,9 @@ protected Object autowireResource(BeanFactory factory, LookupElement element, St
447446
if (factory instanceof ConfigurableBeanFactory) {
448447
ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory) factory;
449448
for (String autowiredBeanName : autowiredBeanNames) {
450-
beanFactory.registerDependentBean(autowiredBeanName, requestingBeanName);
449+
if (beanFactory.containsBean(autowiredBeanName)) {
450+
beanFactory.registerDependentBean(autowiredBeanName, requestingBeanName);
451+
}
451452
}
452453
}
453454

@@ -550,20 +551,7 @@ else if (beanFactory instanceof ConfigurableBeanFactory){
550551

551552
@Override
552553
protected Object getResourceToInject(Object target, String requestingBeanName) {
553-
Object value = null;
554-
if (this.cached && this.shareable) {
555-
value = this.cachedFieldValue;
556-
}
557-
synchronized (this) {
558-
if (!this.cached) {
559-
value = getResource(this, requestingBeanName);
560-
if (value != null && this.shareable) {
561-
this.cachedFieldValue = value;
562-
this.cached = true;
563-
}
564-
}
565-
}
566-
return value;
554+
return getResource(this, requestingBeanName);
567555
}
568556
}
569557

org.springframework.context/src/test/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessorTests.java

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
import org.springframework.beans.factory.BeanCreationException;
3333
import org.springframework.beans.factory.BeanFactory;
3434
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
35+
import org.springframework.beans.factory.ObjectFactory;
3536
import org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor;
3637
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
3738
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
@@ -164,6 +165,72 @@ public void testResourceInjection() {
164165
assertTrue(bean.destroy3Called);
165166
}
166167

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+
167234
@Test
168235
public void testResourceInjectionWithTwoProcessors() {
169236
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
@@ -522,6 +589,14 @@ public ITestBean getTestBean4() {
522589
return testBean4;
523590
}
524591

592+
public INestedTestBean getTestBean5() {
593+
return testBean5;
594+
}
595+
596+
public INestedTestBean getTestBean6() {
597+
return testBean6;
598+
}
599+
525600
@PostConstruct
526601
protected void init2() {
527602
if (this.testBean3 == null || this.testBean4 == null) {

0 commit comments

Comments
 (0)