初识Spring
文章目录
入口代码
在启动Spring的时候,我们是编写如下的代码;本文我们分析的是创建AnnotationConfigApplicationContext()
;
public class SpringstudyApplication {
public static void main(String[] args) {
// 本文分析这一行
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
// 将类转化为bd并注入bean工厂
applicationContext.register(SpringstudyApplication.class);
applicationContext.refresh();
}
}
大致流程
代码分析
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
[1]AnnotationConfigApplicationContext剖析
如下图是该类的继承关系图,他就是整个sping的上下文,他可以进行类的解析,扫描,转换bd等等。那么我们启动spring的时候,就由他来初始化扫描器,解析器,类加载器,环境,过滤链等一系列初始化操作;
public class AnnotationConfigApplicationContext extends GenericApplicationContext implements AnnotationConfigRegistry {
public AnnotationConfigApplicationContext() {
// 创建一个类注解解析器,将上下文转换成了一个bean工厂(DefaultListableBeanFactory),
// bean工厂设置bean优先级排序器以及延迟加载等
// bean工厂注入进spring内置的8个基本bean
this.reader = new AnnotatedBeanDefinitionReader(this);
// 创建一个类的扫描器,可以扫描@Compoment等注解修饰的类
this.scanner = new ClassPathBeanDefinitionScanner(this);
}
}
[2]AnnotatedBeanDefinitionReader 剖析
这里呢是经历了多个构造方法才到了这里,他主要是定义了注册器(即spring上下文),还有系统环境对象,以及调用AnnotationConfigUtils中的方法,对bean工厂进行定义,以及扫描注解时进行初始化设置;
参数:
- registry:就是spring的上下文
annotationConfigApplicationContext
,因为他继承了Registry; - environment:是new了一个
StandardEnvironment
,他可以获取系统属性值,环境变量等;
public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment) {
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
Assert.notNull(environment, "Environment must not be null");
this.registry = registry;
this.conditionEvaluator = new ConditionEvaluator(registry, environment, null);
// 这块就是取出Bean工厂等步骤
AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
}
registerAnnotationConfigProcessors 方法
- 这个方法属于
AnnotationConfigUtils
工具类中的,首先从上下文中获取bean工厂,由于AnnotationConfigApplicationContext
是继承了GenericApplicationContext
了,所以直接返回GenericApplicationContext.beanFactory
;
private static DefaultListableBeanFactory unwrapDefaultListableBeanFactory(BeanDefinitionRegistry registry) {
if (registry instanceof DefaultListableBeanFactory) {
return (DefaultListableBeanFactory) registry;
}
// 走这里
else if (registry instanceof GenericApplicationContext) {
return ((GenericApplicationContext) registry).getDefaultListableBeanFactory();
}
else {
return null;
}
}
// 然后由于在GenericApplicationContext构造方法中,bean工厂被初始化
public GenericApplicationContext() {
this.beanFactory = new DefaultListableBeanFactory();
}
-
bean工厂设置对@Order和@Priority进行扫描排序以及设置对bean的延迟加载与自动注入等
-
注册初始的6个后置处理器(其中一个已经被废弃了),将其变成bd注册进工厂
-
将class变成bd的过程,可以交给
RootBeanDefinition
实现RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
-
beanName | beanClass | 目的 | 备注 |
---|---|---|---|
org.springframework .context .annotation .internalConfigurationAnnotationProcessor | ConfigurationClassPostProcessor.class | 后置处理器 扫描Configuration或者是加了@Component、@ComponentScan、@Import、@ImportResource或者是方法上加了@Bean的注解 | |
org.springframework. context.annotation. internalAutowiredAnnotationProcessor | AutowiredAnnotationBeanPostProcessor.class | 后置处理器 属性注入 | |
org.springframework.context. annotation.internalRequiredAnnotationProcessor | RequiredAnnotationBeanPostProcessor.class | 后置处理器 校验属性上是否有@Required并且是否注入值 | V5.1后取消了 |
org.springframework.context. annotation.internalCommonAnnotationProcessor | CommonAnnotationBeanPostProcessor.class | 后置处理器 解析@Resource、@WebServiceRef、@EJB三个注解 | |
org.springframework.context. annotation.internalPersistenceAnnotationProcessor | ClassUtils.forName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME) | 后置处理器 对JPA进行支持 解析@PersistenceUnit和@PersistenceContext | 若不引入jpa则不注入进bean工厂 |
org.springframework.context. event.internalEventListenerProcessor | EventListenerMethodProcessor.class | 后置处理器 解析@EventListener 设置一个默认的监听器工厂 | |
org.springframework.context. event.internalEventListenerFactory | DefaultEventListenerFactory.class | 后置处理器 也是处理@EventListener |
public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
BeanDefinitionRegistry registry, @Nullable Object source) {
// 根据registry获取registry中的bean工厂
DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
if (beanFactory != null) {
// bean工厂可以对@Order和@Priority进行扫描排序
if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {
beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
}
// bean工厂可以设置对bean的延迟加载与自动注入等
if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) {
beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
}
}
Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<>(8);
// 【bean工厂】后置处理器 扫描Configuration或者是加了@Component、@ComponentScan、@Import、@ImportResource或者是方法上加了@Bean的注解
if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
}
// 【bean】后置处理器 属性注入
if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
}
// 【bean】后置处理器 校验属性上是否有@Required并且是否注入值[V5.1后取消了]
if (!registry.containsBeanDefinition(REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(RequiredAnnotationBeanPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
}
// 【bean】后置处理器 解析@Resource、@WebServiceRef、@EJB三个注解
// Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor.
if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
}
// 后置处理器 对JPA进行支持 解析@PersistenceUnit和@PersistenceContext 若不引入jpa则不注入进bean工厂
// Check for JPA support, and if present add the PersistenceAnnotationBeanPostProcessor.
if (jpaPresent && !registry.containsBeanDefinition(PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition();
try {
def.setBeanClass(ClassUtils.forName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME,
AnnotationConfigUtils.class.getClassLoader()));
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException(
"Cannot load optional framework class: " + PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME, ex);
}
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));
}
// 后置处理器 解析@EventListener 设置一个默认的监听器工厂
if (!registry.containsBeanDefinition(EVENT_LISTENER_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(EventListenerMethodProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME));
}
// 后置处理器 也是处理@EventListener
if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME));
}
return beanDefs;
}
[3]ClassPathBeanDefinitionScanner剖析
上面创建完bean注解解析器后,这里创建BeanDefinition扫描器,我们可以用他来扫描包或者类,然后转换为bd
this.scanner = new ClassPathBeanDefinitionScanner(this);
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
Environment environment, @Nullable ResourceLoader resourceLoader) {
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
this.registry = registry;
// 初始时进入这里为true
if (useDefaultFilters) {
// 加了指定注解的类会被扫描到 带有@Component,@Repository,@Service,@Controller,@ManagedBean,@Named
registerDefaultFilters();
}
// 设置环境(可获取系统变量,环境变量等)
setEnvironment(environment);
// 设置类加载器(上下文实现了ResourceLoader接口,这里塞进去的就是上下文)
setResourceLoader(resourceLoader);
}
registerDefaultFilters剖析
这个方法表示:加了指定注解的类会被扫描到 ,比如带有@Component,@Repository,@Service,@Controller,@ManagedBean,@Named
protected void registerDefaultFilters() {
// @Repository @Controller @Service 注解上面都加了@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.debug("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.debug("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
}
catch (ClassNotFoundException ex) {
// JSR-330 API not available - simply skip.
}
}