diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/TypeExcludeFilter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/TypeExcludeFilter.java index 4e538be646..ce0964e1d6 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/TypeExcludeFilter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/TypeExcludeFilter.java @@ -51,6 +51,8 @@ public class TypeExcludeFilter implements TypeFilter, BeanFactoryAware { private BeanFactory beanFactory; + private Collection delegates; + @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactory = beanFactory; @@ -60,9 +62,7 @@ public class TypeExcludeFilter implements TypeFilter, BeanFactoryAware { public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { if (this.beanFactory instanceof ListableBeanFactory && getClass() == TypeExcludeFilter.class) { - Collection delegates = ((ListableBeanFactory) this.beanFactory) - .getBeansOfType(TypeExcludeFilter.class).values(); - for (TypeExcludeFilter delegate : delegates) { + for (TypeExcludeFilter delegate : getDelegates()) { if (delegate.match(metadataReader, metadataReaderFactory)) { return true; } @@ -71,6 +71,15 @@ public class TypeExcludeFilter implements TypeFilter, BeanFactoryAware { return false; } + private Collection getDelegates() { + Collection delegates = this.delegates; + if (delegates == null) { + delegates = ((ListableBeanFactory) this.beanFactory).getBeansOfType(TypeExcludeFilter.class).values(); + this.delegates = delegates; + } + return delegates; + } + @Override public boolean equals(Object obj) { throw new IllegalStateException("TypeExcludeFilter " + getClass() + " has not implemented equals");