Skip to content
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

AutowireCapableBeanFactory no longer autowiring since version 3.2.5 [SPR-11230] #15855

Closed
spring-projects-issues opened this issue Dec 15, 2013 · 3 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: invalid An issue that we don't feel is valid

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Dec 15, 2013

Roberto Ruiz opened SPR-11230 and commented

I am trying to create some beans using AutowireCapableBeanFactory to configure preexisting objects, and DefaultListableBeanFactory to register beans as singletons

I create 2 simple beans: bean1 and bean2. Bean2 has an autowired reference to bean1

Till version 3.2.4, bean1 is autowired correctlly into bean2, but it stopped working after version 3.2.5

I attach an small test. Beans are created in spring.factory.configuration.MyConfig.

To execute test, just write "mvn clean test" in command line. Use Maven 3 and Java 7.

You may swap spring version editing pom.xml and changing "spring.version" property. Test will be passed with version 3.2.4.RELEASE, but not with 3.2.5.RELEASE, 3.2.6.RELEASE or 4.0.0.RELEASE


Affects: 3.2.5, 3.2.6, 4.0 GA

Attachments:

Issue Links:

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

I'm afraid what you're doing there is invalid, and actually has always been invalid, just accidentally worked: You register an empty bean definition and then call configureBean with a reference to that bean definition, but for different given bean classes. This breaks caching in 3.2.5 which assumes that the bean class never changes for a given bean name. You were just lucky to never have run into such a caching scenario before...

What you should rather be doing: create individual bean definitions for each bean class, and refer to a specific one for each configureBean call; or call autowireBean/createBean instead of configureBean, not even passing in a bean name and not relying on the presence of a dummy bean definition. In fact, configureBean is meant to refer to a proper bean definition with actual metadata; if you don't have those, simply use autowire/createBean.

Hope that helps,

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Roberto Ruiz commented

Thanks a lot,

I was trying to register a singleton from an existing, not configured object, so I finally opted for the "no bean definition" option executing the following steps:

  1. Create the object
  2. Call autowireBean to autowire dependencies
  3. Call initializeBean to execute @PostConstruct and other bean processors
  4. Call registerSingleton

It works as expected, and I only need a BeanDefinition if object has any setter annotated with @Required.

I think the issue may be closed.

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Dec 22, 2013

Juergen Hoeller commented

This sounds like a fine approach.

As a recent change, due to #15871, your original scenario would probably work again - just going to replace the @Autowired cache entry every time the bean class differs. However, even with this in mind, I strongly recommend the new approach, or pointing to individual bean definitions per bean class if a bean definition happens to be required.

Juergen

@spring-projects-issues spring-projects-issues added status: invalid An issue that we don't feel is valid type: regression A bug that is also a regression in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: regression A bug that is also a regression label Jan 12, 2019
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) status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

2 participants