Introduce ApplicationContextAotGenerator
This commit introduces a way to process a GenericApplicationContext ahead of time. Components that can contribute in that phase are invoked, and their contributions are recorded in the GeneratedTypeContext. This commit also expands BeanFactoryContribution so that it can exclude bean definitions that are no longer required. Closes gh-28150
This commit is contained in:
@@ -20,8 +20,11 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.BiPredicate;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.core.io.support.SpringFactoriesLoader;
|
||||
@@ -69,6 +72,16 @@ public class BeanDefinitionsContribution implements BeanFactoryContribution {
|
||||
writeBeanDefinitions(initialization);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BiPredicate<String, BeanDefinition> getBeanDefinitionExcludeFilter() {
|
||||
List<BiPredicate<String, BeanDefinition>> predicates = new ArrayList<>();
|
||||
for (String beanName : this.beanFactory.getBeanDefinitionNames()) {
|
||||
handleMergedBeanDefinition(beanName, beanDefinition -> predicates.add(
|
||||
getBeanRegistrationContribution(beanName, beanDefinition).getBeanDefinitionExcludeFilter()));
|
||||
}
|
||||
return predicates.stream().filter(Objects::nonNull).reduce((n, d) -> false, BiPredicate::or);
|
||||
}
|
||||
|
||||
private void writeBeanDefinitions(BeanFactoryInitialization initialization) {
|
||||
for (String beanName : this.beanFactory.getBeanDefinitionNames()) {
|
||||
handleMergedBeanDefinition(beanName, beanDefinition -> {
|
||||
|
||||
@@ -16,6 +16,10 @@
|
||||
|
||||
package org.springframework.beans.factory.generator;
|
||||
|
||||
import java.util.function.BiPredicate;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
|
||||
/**
|
||||
* Contribute optimizations ahead of time to initialize a bean factory.
|
||||
*
|
||||
@@ -31,4 +35,14 @@ public interface BeanFactoryContribution {
|
||||
*/
|
||||
void applyTo(BeanFactoryInitialization initialization);
|
||||
|
||||
/**
|
||||
* Return a predicate that determines if a particular bean definition
|
||||
* should be excluded from processing. Can be used to exclude infrastructure
|
||||
* that has been optimized using generated code.
|
||||
* @return the predicate to use
|
||||
*/
|
||||
default BiPredicate<String, BeanDefinition> getBeanDefinitionExcludeFilter() {
|
||||
return (beanName, beanDefinition) -> false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user