/** * Create a ClassPathScanningCandidateComponentProvider with the given {@link Environment}. * @param useDefaultFilters whether to register the default filters for the * {@link Component @Component}, {@link Repository @Repository}, * {@link Service @Service}, and {@link Controller @Controller} * stereotype annotations * @param environment the Environment to use * @see #registerDefaultFilters() */ public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters, Environment environment) { if (useDefaultFilters) { registerDefaultFilters(); } setEnvironment(environment); setResourceLoader(null); }
/** * Create a ClassPathScanningCandidateComponentProvider with the given {@link Environment}. * @param useDefaultFilters whether to register the default filters for the * {@link Component @Component}, {@link Repository @Repository}, * {@link Service @Service}, and {@link Controller @Controller} * stereotype annotations * @param environment the Environment to use * @see #registerDefaultFilters() */ public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters, Environment environment) { if (useDefaultFilters) { registerDefaultFilters(); } setEnvironment(environment); setResourceLoader(null); }
private ClassPathScanningCandidateComponentProvider createComponentProvider() { ClassPathScanningCandidateComponentProvider componentProvider = new ClassPathScanningCandidateComponentProvider( false); componentProvider.setEnvironment(this.applicationContext.getEnvironment()); componentProvider.setResourceLoader(this.applicationContext); for (ServletComponentHandler handler : HANDLERS) { componentProvider.addIncludeFilter(handler.getTypeFilter()); } return componentProvider; }
@Test public void defaultsWithScan() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); provider.setResourceLoader(new DefaultResourceLoader( CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader()))); testDefault(provider, ScannedGenericBeanDefinition.class); }
@Test public void antStylePackageWithScan() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); provider.setResourceLoader(new DefaultResourceLoader( CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader()))); testAntStyle(provider, ScannedGenericBeanDefinition.class); }
@Test public void customSupportedIncludeAndExcludedFilterWithScan() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.setResourceLoader(new DefaultResourceLoader( CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader()))); testCustomSupportedIncludeAndExcludeFilter(provider, ScannedGenericBeanDefinition.class); }
@Test public void bogusPackageWithScan() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); provider.setResourceLoader(new DefaultResourceLoader( CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader()))); Set<BeanDefinition> candidates = provider.findCandidateComponents("bogus"); assertEquals(0, candidates.size()); }
@Test public void customAssignableTypeIncludeFilterWithScan() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.setResourceLoader(new DefaultResourceLoader( CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader()))); testCustomAssignableTypeIncludeFilter(provider, ScannedGenericBeanDefinition.class); }
@Test public void customAnnotationTypeIncludeFilterWithScan() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.setResourceLoader(new DefaultResourceLoader( CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader()))); testCustomAnnotationTypeIncludeFilter(provider, ScannedGenericBeanDefinition.class); }
@Test public void excludeFilterWithScan() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); provider.setResourceLoader(new DefaultResourceLoader( CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader()))); provider.addExcludeFilter(new RegexPatternTypeFilter(Pattern.compile(TEST_BASE_PACKAGE + ".*Named.*"))); testExclude(provider, ScannedGenericBeanDefinition.class); }
@Test public void bogusPackageWithIndex() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER)); Set<BeanDefinition> candidates = provider.findCandidateComponents("bogus"); assertEquals(0, candidates.size()); }
@Test public void antStylePackageWithIndex() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER)); testAntStyle(provider, AnnotatedGenericBeanDefinition.class); }
@Test public void customAnnotationTypeIncludeFilterWithIndex() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER)); testCustomAnnotationTypeIncludeFilter(provider, AnnotatedGenericBeanDefinition.class); }
@Test public void customSupportedIncludeAndExcludeFilterWithIndex() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER)); testCustomSupportedIncludeAndExcludeFilter(provider, AnnotatedGenericBeanDefinition.class); }
@Test public void defaultsWithIndex() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER)); testDefault(provider, AnnotatedGenericBeanDefinition.class); }
@Test public void customAssignableTypeIncludeFilterWithIndex() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER)); testCustomAssignableTypeIncludeFilter(provider, AnnotatedGenericBeanDefinition.class); }
@Test public void excludeFilterWithIndex() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER)); provider.addExcludeFilter(new RegexPatternTypeFilter(Pattern.compile(TEST_BASE_PACKAGE + ".*Named.*"))); testExclude(provider, AnnotatedGenericBeanDefinition.class); }
@Test public void customNotSupportedIncludeFilterUseScan() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER)); provider.addIncludeFilter(new AssignableTypeFilter(FooDao.class)); Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE); assertTrue(containsBeanClass(candidates, StubFooDao.class)); assertEquals(1, candidates.size()); assertBeanDefinitionType(candidates, ScannedGenericBeanDefinition.class); }
@Test public void customSupportIncludeFilterWithNonIndexedTypeUseScan() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER)); // This annotation type is not directly annotated with Indexed so we can use // the index to find candidates provider.addIncludeFilter(new AnnotationTypeFilter(CustomStereotype.class)); Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE); assertTrue(containsBeanClass(candidates, DefaultNamedComponent.class)); assertEquals(1, candidates.size()); assertBeanDefinitionType(candidates, ScannedGenericBeanDefinition.class); }
@Test public void customFiltersFollowedByResetUseIndex() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER)); provider.addIncludeFilter(new AnnotationTypeFilter(Component.class)); provider.resetFilters(true); Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE); assertBeanDefinitionType(candidates, AnnotatedGenericBeanDefinition.class); }