diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/config/ConfigurableBeanFactory.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/config/ConfigurableBeanFactory.java index 43f8158d977..0447597e967 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/config/ConfigurableBeanFactory.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/config/ConfigurableBeanFactory.java @@ -187,6 +187,13 @@ public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, Single */ void addEmbeddedValueResolver(StringValueResolver valueResolver); + /** + * Resolve the given embedded value, e.g. an annotation attribute. + * @param value the value to resolve + * @return the resolved value (may be the original value as-is) + */ + String resolveEmbeddedValue(String value); + /** * Add a new BeanPostProcessor that will get applied to beans created * by this factory. To be invoked during factory configuration. diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java index 14acfa3823b..77205adce01 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java @@ -660,12 +660,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp this.embeddedValueResolvers.add(valueResolver); } - /** - * Resolve the given embedded value, e.g. an annotation attribute. - * @param value the value to resolve - * @return the resolved value (may be the original value as-is) - */ - protected String resolveEmbeddedValue(String value) { + public String resolveEmbeddedValue(String value) { String result = value; for (StringValueResolver resolver : this.embeddedValueResolvers) { result = resolver.resolveStringValue(result); diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java index 0590d0265f3..b66889df3a2 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,7 +34,6 @@ import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; - import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; @@ -422,8 +421,8 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean protected Object autowireResource(BeanFactory factory, LookupElement element, String requestingBeanName) throws BeansException { - Object resource = null; - Set autowiredBeanNames = null; + Object resource; + Set autowiredBeanNames; String name = element.name; if (this.fallbackToDefaultTypeMatch && element.isDefaultName && @@ -521,6 +520,9 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean resourceName = Introspector.decapitalize(resourceName.substring(3)); } } + else if (beanFactory instanceof ConfigurableBeanFactory){ + resourceName = ((ConfigurableBeanFactory) beanFactory).resolveEmbeddedValue(resourceName); + } if (resourceType != null && !Object.class.equals(resourceType)) { checkResourceType(resourceType); } @@ -588,7 +590,7 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean @Override protected Object getResourceToInject(Object target, String requestingBeanName) { - Service service = null; + Service service; try { service = (Service) getResource(this, requestingBeanName); } diff --git a/org.springframework.context/src/test/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessorTests.java b/org.springframework.context/src/test/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessorTests.java index 6b93c5ff36e..304f9ab85bd 100644 --- a/org.springframework.context/src/test/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessorTests.java +++ b/org.springframework.context/src/test/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessorTests.java @@ -16,6 +16,8 @@ package org.springframework.context.annotation; +import java.util.Properties; + import static org.junit.Assert.*; import javax.annotation.PostConstruct; @@ -31,6 +33,7 @@ import org.springframework.beans.TestBean; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; @@ -171,10 +174,16 @@ public class CommonAnnotationBeanPostProcessorTests { public void testExtendedResourceInjection() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor(); - bpp.setResourceFactory(bf); + bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); bf.registerResolvableDependency(BeanFactory.class, bf); + PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); + Properties props = new Properties(); + props.setProperty("tb", "testBean3"); + ppc.setProperties(props); + ppc.postProcessBeanFactory(bf); + bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ExtendedResourceInjectionBean.class)); bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(NamedResourceInjectionBean.class)); TestBean tb = new TestBean(); @@ -212,10 +221,16 @@ public class CommonAnnotationBeanPostProcessorTests { public void testExtendedResourceInjectionWithOverriding() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor(); - bpp.setResourceFactory(bf); + bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); bf.registerResolvableDependency(BeanFactory.class, bf); + PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); + Properties props = new Properties(); + props.setProperty("tb", "testBean3"); + ppc.setProperties(props); + ppc.postProcessBeanFactory(bf); + RootBeanDefinition annotatedBd = new RootBeanDefinition(ExtendedResourceInjectionBean.class); TestBean tb5 = new TestBean(); annotatedBd.getPropertyValues().addPropertyValue("testBean2", tb5); @@ -387,7 +402,7 @@ public class CommonAnnotationBeanPostProcessorTests { super.setTestBean2(testBean2); } - @Resource(name="testBean3", type=ITestBean.class) + @Resource(name="${tb}", type=ITestBean.class) private void setTestBean4(ITestBean testBean4) { this.testBean4 = testBean4; }