Skip to content

Doc: Autowiring in @Configuration classes with post-processor definitions [SPR-13285] #17875

@spring-projects-issues

Description

@spring-projects-issues

Gary Russell opened SPR-13285 and commented

While the new documentation now warns that factory-method injection is preferred to auto wiring, there are cases where auto wiring does not work at all with @Configuration classes.

Consider:

@Configuration
public class FooConfig {

	@Value("${foo}")
	public String foo;

	@Bean
	public String earlyFoo() {
		return this.foo;
	}

	@Bean
	public String foo(@Value("${foo}") String foo) {
		return foo;
	}

	@Bean
	public PropertySourcesPlaceholderConfigurer configurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}

}

public class Testing {

	@Test
	public void test() {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
		context.register(FooConfig.class);
		StandardEnvironment env = new StandardEnvironment();
		Properties props = new Properties();
		props.setProperty("foo", "bar");
		PropertiesPropertySource pps = new PropertiesPropertySource("sftpprop", props);
		env.getPropertySources().addLast(pps);
		context.setEnvironment(env);
		context.refresh();
		assertNull(context.getBean("earlyFoo"));
		assertEquals("bar", context.getBean("foo"));
		context.close();
	}

}

In this case, the value is not injected into the field because fooConfig is created before the BPP s...

2015-07-28 14:39:08,114 [main] DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'fooConfig'
...
2015-07-28 14:39:08,130 [main] DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'

I don't know if this can be fixed, but perhaps we should document the conditions under which it occurs.


Affects: 4.2 RC3

Issue Links:

Referenced from: commits 90493f4

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions