001/* 002 * Copyright 2012-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.context; 018 019import java.io.IOException; 020import java.util.Collection; 021 022import org.springframework.beans.BeansException; 023import org.springframework.beans.factory.BeanFactory; 024import org.springframework.beans.factory.BeanFactoryAware; 025import org.springframework.beans.factory.ListableBeanFactory; 026import org.springframework.core.type.classreading.MetadataReader; 027import org.springframework.core.type.classreading.MetadataReaderFactory; 028import org.springframework.core.type.filter.TypeFilter; 029 030/** 031 * Provides exclusion {@link TypeFilter TypeFilters} that are loaded from the 032 * {@link BeanFactory} and automatically applied to {@code SpringBootApplication} 033 * scanning. Can also be used directly with {@code @ComponentScan} as follows: 034 * <pre class="code"> 035 * @ComponentScan(excludeFilters = @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class)) 036 * </pre> 037 * <p> 038 * Implementations should provide a subclass registered with {@link BeanFactory} and 039 * override the {@link #match(MetadataReader, MetadataReaderFactory)} method. They should 040 * also implement a valid {@link #hashCode() hashCode} and {@link #equals(Object) equals} 041 * methods so that they can be used as part of Spring test's application context caches. 042 * <p> 043 * Note that {@code TypeExcludeFilters} are initialized very early in the application 044 * lifecycle, they should generally not have dependencies on any other beans. They are 045 * primarily used internally to support {@code spring-boot-test}. 046 * 047 * @author Phillip Webb 048 * @since 1.4.0 049 */ 050public class TypeExcludeFilter implements TypeFilter, BeanFactoryAware { 051 052 private BeanFactory beanFactory; 053 054 @Override 055 public void setBeanFactory(BeanFactory beanFactory) throws BeansException { 056 this.beanFactory = beanFactory; 057 } 058 059 @Override 060 public boolean match(MetadataReader metadataReader, 061 MetadataReaderFactory metadataReaderFactory) throws IOException { 062 if (this.beanFactory instanceof ListableBeanFactory 063 && getClass() == TypeExcludeFilter.class) { 064 Collection<TypeExcludeFilter> delegates = ((ListableBeanFactory) this.beanFactory) 065 .getBeansOfType(TypeExcludeFilter.class).values(); 066 for (TypeExcludeFilter delegate : delegates) { 067 if (delegate.match(metadataReader, metadataReaderFactory)) { 068 return true; 069 } 070 } 071 } 072 return false; 073 } 074 075 @Override 076 public boolean equals(Object obj) { 077 throw new IllegalStateException( 078 "TypeExcludeFilter " + getClass() + " has not implemented equals"); 079 } 080 081 @Override 082 public int hashCode() { 083 throw new IllegalStateException( 084 "TypeExcludeFilter " + getClass() + " has not implemented hashCode"); 085 } 086 087}