Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Jul 28, 2015 · 1 comment
Assignees
Labels
type: documentation A documentation task
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jul 28, 2015

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

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

From my perspective, the proper solution here is to declare BeanFactoryPostProcessor (and also BeanPostProcessor) definitions as static @Bean methods, not triggering the instantiation of the containing configuration class to begin with. I guess we need to add a stronger note on that. Maybe we should even log a warning (or info at least) when a post-processor bean is being created through a non-static factory method...

Juergen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

2 participants