Enforce BeanRegistrationExcludeFilter beans are also AOT processors

Update `BeanDefinitionMethodGeneratorFactory` to enforce that any
`BeanRegistrationExcludeFilter` filter that is from a bean factory
also implements an AOT processor interface.

See gh-28866
This commit is contained in:
Phillip Webb
2022-07-28 13:48:52 +01:00
parent a4f3d1d6e8
commit 9413383d72
3 changed files with 42 additions and 5 deletions

View File

@@ -22,10 +22,12 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.aot.AotServices.Source;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.core.log.LogMessage;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
@@ -65,6 +67,14 @@ class BeanDefinitionMethodGeneratorFactory {
BeanDefinitionMethodGeneratorFactory(AotServices.Loader loader) {
this.aotProcessors = loader.load(BeanRegistrationAotProcessor.class);
this.excludeFilters = loader.load(BeanRegistrationExcludeFilter.class);
for (BeanRegistrationExcludeFilter excludeFilter : this.excludeFilters) {
if (this.excludeFilters.getSource(excludeFilter) == Source.BEAN_FACTORY) {
Assert.state(excludeFilter instanceof BeanRegistrationAotProcessor
|| excludeFilter instanceof BeanFactoryInitializationAotProcessor,
() -> "BeanRegistrationExcludeFilter bean of type %s must also implement an AOT processor interface"
.formatted(excludeFilter.getClass().getName()));
}
}
}
@@ -97,7 +107,7 @@ class BeanDefinitionMethodGeneratorFactory {
return true;
}
for (BeanRegistrationExcludeFilter excludeFilter : this.excludeFilters) {
if (excludeFilter.isExcluded(registeredBean)) {
if (excludeFilter.isExcludedFromAotProcessing(registeredBean)) {
logger.trace(LogMessage.format(
"Excluding registered bean '%s' from bean factory %s due to %s",
registeredBean.getBeanName(),

View File

@@ -35,6 +35,6 @@ public interface BeanRegistrationExcludeFilter {
* @param registeredBean the registered bean
* @return if the registered bean should be excluded
*/
boolean isExcluded(RegisteredBean registeredBean);
boolean isExcludedFromAotProcessing(RegisteredBean registeredBean);
}