spring_basic
spring基础
###@Componet####
- @RestController @Service 注解等都是基于@component
###@componentScan 源码中的useDefaultFilter####1
2
3
4
5@Configuration
@ComponentScan(value = "com.melon.app",
includeFilters ={@ComponentScan.Filter(type =
FilterType.CUSTOM,classes = {MyTypeFilter.class})},
useDefaultFilters = false)
其中useDefaultFilters为何要设置false的原因就是如果不设置都会扫描@component注解的类
源码分析1
2
3
4
5
6
7
8
9
10
11
12
13AnnotationConfigApplicationContext->AbstractApplicationContext.refresh()
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// 调用后置拦截器
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
````
>AbstractApplicationContextprotected
void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory)1
2//调用后置拦截器
>> PostProcessorRegistrationDelegate
->invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
->invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
->for (BeanDefinitionRegistryPostProcessor postProcessor : postProcessors) {
postProcessor.postProcessBeanDefinitionRegistry(registry);
}1
> ConfigureClassPostProcessor
->postProcessBeanDefinitionRegistry
->processConfigBeanDefinitions
-> parser.parse(candidates);1
> ConfigureClassParser
->parse(Set
->if (bd instanceof AnnotatedBeanDefinition) {
parse(((AnnotatedBeanDefinition) bd).getMetadata(), holder.getBeanName());
}
->parse(AnnotationMetadata metadata, String beanName)
->processConfigurationClass(ConfigurationClass configClass)
-> doProcessConfigurationClass(configClass, sourceClass);
->Set
this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());1
> ComponentScanAnnotationParser
->parse(AnnotationAttributes componentScan, final String declaringClass)
->ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(this.registry,
componentScan.getBoolean(“useDefaultFilters”), this.environment, this.resourceLoader);1
> ClassPathBeanDefinitionScanner
->ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
Environment environment, @Nullable ResourceLoader resourceLoader)1
2
3
4
5
6
7
8
9
10
11
12public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
Environment environment, @Nullable ResourceLoader resourceLoader) {
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
this.registry = registry;
if (useDefaultFilters) {
registerDefaultFilters();
}
setEnvironment(environment);
setResourceLoader(resourceLoader);
}
ClassPathScanningCandidateComponentProvider
····
protected void registerDefaultFilters() {
//默认情况下会扫描所有注解是component的类
this.includeFilters.add(new AnnotationTypeFilter(Component.class));
ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
try {
this.includeFilters.add(new AnnotationTypeFilter(
((Class<? extends Annotation>) ClassUtils.forName(“javax.annotation.ManagedBean”, cl)), false));
logger.trace(“JSR-250 ‘javax.annotation.ManagedBean’ found and supported for component scanning”);
}
catch (ClassNotFoundException ex) {
// JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.
}
try {
this.includeFilters.add(new AnnotationTypeFilter(
((Class<? extends Annotation>) ClassUtils.forName(“javax.inject.Named”, cl)), false));
logger.trace(“JSR-330 ‘javax.inject.Named’ annotation found and supported for component scanning”);
}
catch (ClassNotFoundException ex) {
// JSR-330 API not available - simply skip.
}
}
原文作者: 西瓜哥
原文链接: http://yoursite.com/2019/07/27/spring-basic/
版权声明: 转载请注明出处(必须保留原文作者署名原文链接)