Skip to content

Doc: @Autowired not working in BeanFactoryPostProcessor [SPR-4935] #9610

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 Jun 18, 2008 · 9 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: task A general task
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jun 18, 2008

Rafał Piotrowski opened SPR-4935 and commented

I create simple bean which implements BeanFactoryPostProcessor and has dependent bean injected with @Autowired annotation. Injection does not occur.

Workaround is to use standard XML dependency injection.


Affects: 2.5.4

Attachments:

Issue Links:

Referenced from: commits a557878

0 votes, 6 watchers

@spring-projects-issues
Copy link
Collaborator Author

Rafał Piotrowski commented

Example of bug. You have to run SampleServiceTest (as JUnit4 test).

@spring-projects-issues
Copy link
Collaborator Author

Rafał Piotrowski commented

AbstractApplicationContext.java:

[...]
362: // Invoke factory processors registered as beans in the context.
363: invokeBeanFactoryPostProcessors(beanFactory);
364:
365: // Register bean processors that intercept bean creation.
366: registerBeanPostProcessors(beanFactory);
[...]

As you can see BeanFactoryPostProcessors are created and invoked before any BeanPostProcessor is created. AutowiredAnnotationBeanPostProcessor (postprocessor responsible for @Autowired annotation) is BeanPostProcessor and it is not applied to any BeanFactoryPostProcessors.

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

This is actually intended: Annotation-based injection is available to application objects only, which in particular excludes post-processors. The technical reason for this is that annotation processing itself is implemented as post-processors. That said - it's also by definition, so we need to document this properly.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Rafał Piotrowski commented

The same thing applies to factoryBeans and to beans that factoryBeans use (that are inject to some factoryBean by XML) when beanFactoryPostprocessor invokes beanFactory.getBeansOfType(Class) or beanFactory.getBeansOfType(Class, boolean, boolean) with last argument set to true.

Attached zip contains updated sample application with JUnit4 test class (SampleTest).

@spring-projects-issues
Copy link
Collaborator Author

Paul Benedict commented

Juergen, that documentation should go straight into the annotation's javadoc. That limitation sounds too important to be left to the Reference guide alone.

@spring-projects-issues
Copy link
Collaborator Author

Dave Syer commented

Rafal: the factory bean effect you are seeing is a side effect of the implementation of your BeanFactoryPostProcessor - if you play with the bean instances from the BeanFactory in a factory post processor, you bypass the normal ApplicationContext bean creation features, like applying BeanPostProcessors. In other words: it's expected behaviour.

I'll change this issue to a documentation issue.

@spring-projects-issues
Copy link
Collaborator Author

Oliver Drotbohm commented

Attached patch to alter @Autowired javadoc to mention that it cannot be used inside Bean(Factory)PostProcessor implementations.

@spring-projects-issues
Copy link
Collaborator Author

Oliver Drotbohm commented

Forward to Jürgen to review and apply patch.

@spring-projects-issues
Copy link
Collaborator Author

Chris Beams commented

Thanks, Ollie for the patch. With minor modifications it has been applied, as well as a similar update to @Value and AutowiredAnnotationBeanPostProcessor Javadoc. A note on limitations for these annotations has also been added to the reference documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: task A general task
Projects
None yet
Development

No branches or pull requests

1 participant