spring boot фильтер загружаемых бинов
Не работает фильтр загрузки бинов. Что здесь можно поправить? ExcludePackageTypeFilter реализован корректно. После работы ExcludePackageTypeFilter он еще раз добавляет бины.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import ru.cft.aml.lists.configuration.ExcludePackageTypeFilter;
@SpringBootApplication(scanBasePackages = "ru")
@ComponentScan(excludeFilters = @ComponentScan.Filter(
type = FilterType.CUSTOM, classes = {ExcludePackageTypeFilter.class}),
basePackages = {"ru"})
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
import org.jetbrains.annotations.NotNull;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;
import java.util.ArrayList;
import java.util.List;
public class ExcludePackageTypeFilter implements TypeFilter, EnvironmentAware {
private Environment env;
@Override
public boolean match(@NotNull MetadataReader metadataReader, @NotNull MetadataReaderFactory metadataReaderFactory) {
List<String> ignoredClassPaths = getIgnoredClassPaths(env);
for (String path : ignoredClassPaths) {
if (!ignoredClassPaths.isEmpty()
&& metadataReader.getClassMetadata().getClassName().startsWith(path)) {
System.out.println("не загружаем bean: " + metadataReader.getClassMetadata().getClassName());
return true;
}
}
return false;
}
private List<String> getIgnoredClassPaths(Environment env) {
List<String> result = new ArrayList<>();
//....
return result;
}
@Override
public void setEnvironment(@NotNull Environment environment) {
this.env = environment;
}
}