Browse Source

fixed @Value injection to correctly cache temporary null results for non-singleton beans (SPR-7614)

pull/1234/head
Juergen Hoeller 14 years ago
parent
commit
1933b648c3
  1. 4
      org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java
  2. 52
      org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java

4
org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

@ -474,7 +474,7 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean @@ -474,7 +474,7 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
value = beanFactory.resolveDependency(descriptor, beanName, autowiredBeanNames, typeConverter);
synchronized (this) {
if (!this.cached) {
if (value != null) {
if (value != null || this.required) {
this.cachedFieldValue = descriptor;
registerDependentBeans(beanName, autowiredBeanNames);
if (autowiredBeanNames.size() == 1) {
@ -545,7 +545,7 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean @@ -545,7 +545,7 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
descriptors[i] = new DependencyDescriptor(methodParam, this.required);
arguments[i] = beanFactory.resolveDependency(
descriptors[i], beanName, autowiredBeanNames, typeConverter);
if (arguments[i] == null) {
if (arguments[i] == null && !this.required) {
arguments = null;
break;
}

52
org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 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.
@ -18,8 +18,10 @@ package org.springframework.context.annotation.configuration; @@ -18,8 +18,10 @@ package org.springframework.context.annotation.configuration;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import test.beans.Colour;
import test.beans.TestBean;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@ -27,18 +29,17 @@ import org.springframework.beans.factory.support.RootBeanDefinition; @@ -27,18 +29,17 @@ import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.ClassPathResource;
import test.beans.Colour;
import test.beans.TestBean;
/**
* System tests covering use of {@link Autowired} and {@link Value} within
* {@link Configuration} classes.
*
* @author Chris Beams
* @author Juergen Hoeller
*/
public class AutowiredConfigurationTests {
@ -98,25 +99,56 @@ public class AutowiredConfigurationTests { @@ -98,25 +99,56 @@ public class AutowiredConfigurationTests {
@Test
public void testValueInjection() {
System.setProperty("myProp", "foo");
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext(
"ValueInjectionTests.xml", AutowiredConfigurationTests.class);
System.clearProperty("myProp");
TestBean testBean = factory.getBean("testBean", TestBean.class);
assertNull(testBean.getName());
testBean = factory.getBean("testBean2", TestBean.class);
assertNull(testBean.getName());
System.setProperty("myProp", "foo");
testBean = factory.getBean("testBean", TestBean.class);
assertThat(testBean.getName(), equalTo("foo"));
testBean = factory.getBean("testBean2", TestBean.class);
assertThat(testBean.getName(), equalTo("foo"));
System.clearProperty("myProp");
testBean = factory.getBean("testBean", TestBean.class);
assertNull(testBean.getName());
testBean = factory.getBean("testBean2", TestBean.class);
assertNull(testBean.getName());
}
@Configuration
static class ValueConfig {
@Value("#{systemProperties.myProp}")
private String name = "default";
@Value("#{systemProperties[myProp]}")
private String name;
@Bean
private String name2;
@Value("#{systemProperties[myProp]}")
public void setName2(String name) {
this.name2 = name;
}
@Bean @Scope("prototype")
public TestBean testBean() {
return new TestBean(name);
}
@Bean @Scope("prototype")
public TestBean testBean2() {
return new TestBean(name2);
}
}

Loading…
Cancel
Save