-
Notifications
You must be signed in to change notification settings - Fork 40.9k
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
Support programmatic lazy-int exclusion #16615
Conversation
Thanks for the PR, @tkvangorder. We might explore the possibility of the callback interface extending |
@wilkinsona A predicate is a good idea and gives downstream projects a little more control. I made some changes to support a predicate, is this what you had in mind? |
for (String beanName : beanFactory.getBeanDefinitionNames()) { | ||
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName); | ||
if (eagerPredicateList.stream().anyMatch( | ||
(predicate) -> predicate.test(beanFactory.getType(beanName)))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this may lead to factory beans being instantiated so we need to avoid it. We probably need something that's somewhat similar to BeanTypeRegistry
. Making that class public doesn't feel like a good idea, but it's also complicated enough that copying it doesn't feel like a good idea either. In short, a change is needed here but I don't know what it should be. I wonder if Framework could provide an API for getting a bean's type that gives up and returns null rather than instantiating a factory bean that lacks a generic type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or we can revert back to using a customizer that passes in a list of classes that should be ignored? This basically inverts the problem and allows the use of beanFactory.isTypeMatch()
. I really like the idea of a Predicate (as you can build much smarter ignore logic) but I am wondering if there are any cases where a list of classes wouldn't suffice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, we can't safely use isTypeMatch()
either. If a bean is a FactoryBean
, Framework may instantiate it to determine the type of the object that it produces.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing that could be done (at least for collecting the Predicates) would be to use SpringFactoriesLoader
rather than scanning the list of bean definitions. Of course, once we have the list of predicates we are still back to the need of passing each bean definition's type to those predicates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We considered spring.factories
(it has some notable benefits over beans being created very early) but preferred the @Bean
-based approach that allows the predicate and the auto-configuration to which it relates to be defined together in the same class.
Flagged for team attention so we can discuss the best way to go from a bean definition to the type of the defined bean. |
I've raised spring-projects/spring-framework#23056 to see what (if any) of our |
Although the |
Since we now have a method to extract all beans of a given type from the factory (without initializing the factory beans): How would you feel about not using a predicate, but rather reverting back to a "customizer" that returns a list of classes that should be excluded from the lazy-loading processor? I can then just iterate over the classes from the customizers and use |
@tkvangorder I'm still hopeful that we can get a fix for spring-projects/spring-framework#23374 into Framework before GA |
@philwebb I have updated this PR to take advantage of the new |
@tkvangorder Wow that was quick!! It only landed in Framework a few hours ago 👍 |
@philwebb @wilkinsona I know you are both crazy busy, but I think this PR is ready to go. Do you want me to do anything else with it? |
@tkvangorder I'm afraid we haven't got to it yet but this one is really high on the list and we need to merge it before RC1. Rest assured, we've not forgotten :) |
Allow the `LazyInitializationBeanFactoryPostProcessor` to skip setting lazy-init based on a programmatic callback. This feature allows downstream projects to deal with edge-cases in which it is not easy to support lazy-loading (such as in DSLs that dynamically create additional beans). See gh-16615
Thanks very much @tkvangorder, this has now been merged to master. I've refined things a little in commit 3ffc5f2. I decided to rename Thanks again for all your work on this, and please let me know if you find any issues with my changes! |
I like the changes. Thanks! |
No description provided.