@Resource processing properly works with scoped beans and prototypes again
Issue: SPR-9627
This commit is contained in:
parent
cec30a7a2d
commit
04af54ad4c
|
|
@ -35,7 +35,6 @@ import java.util.LinkedList;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
import javax.annotation.PreDestroy;
|
import javax.annotation.PreDestroy;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
@ -447,7 +446,9 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean
|
||||||
if (factory instanceof ConfigurableBeanFactory) {
|
if (factory instanceof ConfigurableBeanFactory) {
|
||||||
ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory) factory;
|
ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory) factory;
|
||||||
for (String autowiredBeanName : autowiredBeanNames) {
|
for (String autowiredBeanName : autowiredBeanNames) {
|
||||||
beanFactory.registerDependentBean(autowiredBeanName, requestingBeanName);
|
if (beanFactory.containsBean(autowiredBeanName)) {
|
||||||
|
beanFactory.registerDependentBean(autowiredBeanName, requestingBeanName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -550,20 +551,7 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Object getResourceToInject(Object target, String requestingBeanName) {
|
protected Object getResourceToInject(Object target, String requestingBeanName) {
|
||||||
Object value = null;
|
return getResource(this, requestingBeanName);
|
||||||
if (this.cached && this.shareable) {
|
|
||||||
value = this.cachedFieldValue;
|
|
||||||
}
|
|
||||||
synchronized (this) {
|
|
||||||
if (!this.cached) {
|
|
||||||
value = getResource(this, requestingBeanName);
|
|
||||||
if (value != null && this.shareable) {
|
|
||||||
this.cachedFieldValue = value;
|
|
||||||
this.cached = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2011 the original author or authors.
|
* Copyright 2002-2012 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -32,6 +32,7 @@ import org.springframework.beans.TestBean;
|
||||||
import org.springframework.beans.factory.BeanCreationException;
|
import org.springframework.beans.factory.BeanCreationException;
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||||
|
import org.springframework.beans.factory.ObjectFactory;
|
||||||
import org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor;
|
import org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor;
|
||||||
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
|
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
|
||||||
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
|
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
|
||||||
|
|
@ -164,6 +165,72 @@ public class CommonAnnotationBeanPostProcessorTests {
|
||||||
assertTrue(bean.destroy3Called);
|
assertTrue(bean.destroy3Called);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testResourceInjectionWithPrototypes() {
|
||||||
|
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||||
|
CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
|
||||||
|
bpp.setResourceFactory(bf);
|
||||||
|
bf.addBeanPostProcessor(bpp);
|
||||||
|
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ResourceInjectionBean.class, false));
|
||||||
|
bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class, false));
|
||||||
|
bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class, false));
|
||||||
|
|
||||||
|
ResourceInjectionBean bean = (ResourceInjectionBean) bf.getBean("annotatedBean");
|
||||||
|
assertTrue(bean.initCalled);
|
||||||
|
assertTrue(bean.init2Called);
|
||||||
|
assertTrue(bean.init3Called);
|
||||||
|
|
||||||
|
TestBean tb = bean.getTestBean();
|
||||||
|
TestBean tb2 = bean.getTestBean2();
|
||||||
|
assertNotNull(tb);
|
||||||
|
assertNotNull(tb2);
|
||||||
|
|
||||||
|
ResourceInjectionBean anotherBean = (ResourceInjectionBean) bf.getBean("annotatedBean");
|
||||||
|
assertNotSame(anotherBean, bean);
|
||||||
|
assertNotSame(anotherBean.getTestBean(), tb);
|
||||||
|
assertNotSame(anotherBean.getTestBean2(), tb2);
|
||||||
|
|
||||||
|
bf.destroyBean("annotatedBean", bean);
|
||||||
|
assertTrue(bean.destroyCalled);
|
||||||
|
assertTrue(bean.destroy2Called);
|
||||||
|
assertTrue(bean.destroy3Called);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testResourceInjectionWithResolvableDependencyType() {
|
||||||
|
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||||
|
CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
|
||||||
|
bpp.setBeanFactory(bf);
|
||||||
|
bf.addBeanPostProcessor(bpp);
|
||||||
|
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ExtendedResourceInjectionBean.class, false));
|
||||||
|
bf.registerBeanDefinition("testBean4", new RootBeanDefinition(TestBean.class, false));
|
||||||
|
|
||||||
|
bf.registerResolvableDependency(BeanFactory.class, bf);
|
||||||
|
bf.registerResolvableDependency(INestedTestBean.class, new ObjectFactory() {
|
||||||
|
public Object getObject() throws BeansException {
|
||||||
|
return new NestedTestBean();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.setProperty("tb", "testBean4");
|
||||||
|
ppc.setProperties(props);
|
||||||
|
ppc.postProcessBeanFactory(bf);
|
||||||
|
|
||||||
|
ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
|
||||||
|
INestedTestBean tb = bean.getTestBean6();
|
||||||
|
assertNotNull(tb);
|
||||||
|
|
||||||
|
ExtendedResourceInjectionBean anotherBean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
|
||||||
|
assertNotSame(anotherBean, bean);
|
||||||
|
assertNotSame(anotherBean.getTestBean6(), tb);
|
||||||
|
|
||||||
|
String[] depBeans = bf.getDependenciesForBean("annotatedBean");
|
||||||
|
assertEquals(1, depBeans.length);
|
||||||
|
assertEquals("testBean4", depBeans[0]);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testResourceInjectionWithTwoProcessors() {
|
public void testResourceInjectionWithTwoProcessors() {
|
||||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||||
|
|
@ -522,6 +589,14 @@ public class CommonAnnotationBeanPostProcessorTests {
|
||||||
return testBean4;
|
return testBean4;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public INestedTestBean getTestBean5() {
|
||||||
|
return testBean5;
|
||||||
|
}
|
||||||
|
|
||||||
|
public INestedTestBean getTestBean6() {
|
||||||
|
return testBean6;
|
||||||
|
}
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
protected void init2() {
|
protected void init2() {
|
||||||
if (this.testBean3 == null || this.testBean4 == null) {
|
if (this.testBean3 == null || this.testBean4 == null) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue