Skip to content

Conversation

philwebb
Copy link
Member

Initial attempt at fixing the issues raised from #23056. Since these changes are quite core, careful review is required.

philwebb added 6 commits July 23, 2019 11:49
Update `ListableBeanFactory` and `BeanFactoryUtils` to add the missing
`getBeanNamesForType` methods that accept a `ResolvableType` rather
than a `Class`.

This completes the work started in 778a019.

Closes spring-projectsgh-23335
Update the logic in `AbstractBeanFactory` so that caches from merged
bean definitions remain whenever possible.

Prior to this commit, all merged bean definitions would be completely
removed after bean post processing in case a processor changed the bean
type. It's fairly unlikely these days that the bean type will actually
change, so instead we now compare a subset of the old cached properties
against the newly created definition. Only if key properties have
changed do we now discard the older cached values.

Closes spring-projectsgh-23336
Add an additional cache to the `RootBeanDefinition` to save
recalculating the result of `isFactoryBean`.

Closes spring-projectsgh-23337
Update the `FactoryBean` type prediction logic (primarily in the
`DefaultListableBeanFactory`) so that generic type information is
considered when calling `getBeanNamesForType` on a non-frozen
configuration.

Calling `getBeanNamesForType` with `allowEagerInit` disabled will now
detect `FactoryBean` variants as long as generic type information is
available in either the class or the factory method return type.

Closes spring-projectsgh-23338
Update `getTypeForFactoryBean` detection so that a bean definition
attribute can be used to supply the result. This commit allows projects
such as Spring Data to provide the result that would be supplied by
`getObjectType` early so that we don't need to initialize the
`FactoryBean` unnecessarily.

Closes spring-projectsgh-23338
Update code that's often called so that zero length array results use
a single shared static constant, rather than a new instance for each
call.

Closes spring-projectsgh-23340
@philwebb philwebb requested a review from jhoeller July 23, 2019 11:26
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 23, 2019
@tkvangorder
Copy link

tkvangorder commented Jul 25, 2019

@philwebb Your changes look good. In context to spring-projects/spring-boot#16615, I have one (probably silly) question: Is there a way to ask the BeanFactory for a Bean Definition's type without forcing the initialization of that bean? Essentially a BeanFactory.getType(String name, boolean allowEagerInit). The code in that PR 16615 iterates over all the bean definitions gets the type for each definition and then consults the predicates: https://github.com/spring-projects/spring-boot/pull/16615/files#diff-66d2d216c30cb3f8db7e7e3f09a66249R61-R65

I am likely missing something obvious. I could change the Predicate to accept a BeanDefinition and use something like BeanFactory.isTypeMatch() but I am afraid that might also force the initialization of the bean. ugh...slippery slope.

@philwebb
Copy link
Member Author

@tkvangorder I'm not sure there is such an API yet. I've raised #23374 to look into it after this one is merged.

@jhoeller jhoeller self-assigned this Jul 30, 2019
@jhoeller jhoeller added this to the 5.2 RC1 milestone Jul 30, 2019
@jhoeller jhoeller added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jul 30, 2019
@jhoeller jhoeller merged commit cca32a5 into spring-projects:master Jul 31, 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) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants