AnnotationConfigApplicationContext的创建过程
1.AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
->AbstractApplicationContext 调用父类静态方法,声明三个常量
public static final String MESSAGE_SOURCE_BEAN_NAME = "messageSource";
public static final String LIFECYCLE_PROCESSOR_BEAN_NAME = "lifecycleProcessor";
public static final String APPLICATION_EVENT_MULTICASTER_BEAN_NAME = "applicationEventMulticaster";
static {
// Eagerly load the ContextClosedEvent class to avoid weird classloader issues
//急切加载ContextClosedEvent避免莫名其妙的事情发生
ContextClosedEvent.class.getName();
}
->GenericApplicationContext的无参构造
this.beanFactory = new DefaultListableBeanFactory();//返回beanFactory
->AbstractApplicationContext的无参构造
this.resourcePatternResolver = getResourcePatternResolver();//返回路径解析器
->return new PathMatchingResourcePatternResolver(this)
->AbstractApplicationContext
private final Set<ProtocolResolver> protocolResolvers = new LinkedHashSet<>(4);
private final Map<Class<?>, Map<Resource, ?>> resourceCaches = new ConcurrentHashMap<>(4);
接着无参构造
this.classLoader = ClassUtils.getDefaultClassLoader();
->ClassUtils的getDefaultClassLoader
默认Thread.currentThread().getContextClassLoader()
接着ClassUtils.class.getClassLoader()
接着ClassLoader.getSystemClassLoader()
AbstractApplicationContext里面的属性初始化
GenericApplicationContext的属性初始化
2.AnnotatedBeanDefinitionReader的创建
AnnotatedBeanDefinitionReader一个裸类
1.getOrCreateEnvironment(registry)
private static Environment getOrCreateEnvironment(BeanDefinitionRegistry registry) {
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
//通过继承图就看出为true
if (registry instanceof EnvironmentCapable) {
return ((EnvironmentCapable) registry).getEnvironment();
}
return new StandardEnvironment();
}
返回的代码
protected ConfigurableEnvironment createEnvironment() {
return new StandardEnvironment();
}
->AbstractEnvironment 几个环境参数
public static final String IGNORE_GETENV_PROPERTY_NAME = "spring.getenv.ignore";
public static final String ACTIVE_PROFILES_PROPERTY_NAME = "spring.profiles.active";
public static final String DEFAULT_PROFILES_PROPERTY_NAME = "spring.profiles.default";
protected static final String RESERVED_DEFAULT_PROFILE_NAME = "default";
//默认是default环境
private final MutablePropertySources propertySources = new MutablePropertySources();
后面两个路径解析器
private final MutablePropertySources propertySources = new MutablePropertySources();
private final ConfigurablePropertyResolver propertyResolver =
new PropertySourcesPropertyResolver(this.propertySources);
customizePropertySources(this.propertySources);构造里面的方法
给propertySources里面放了两个环境
->systemProperties
-> System.getProperties()
->stemEnvironment
->System.getenv()
2.构造里面属性赋值
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;//bean定义注册
this.conditionEvaluator = new ConditionEvaluator(registry, environment, null);
AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
}
3.new ConditionEvaluator
ConditionEvaluator是spring4.0新增的条件处理器
public ConditionEvaluator(@Nullable BeanDefinitionRegistry registry,
@Nullable Environment environment, @Nullable ResourceLoader resourceLoader) {
//初始化private final ConditionContextImpl context
this.context = new ConditionContextImpl(registry, environment, resourceLoader);
}
ConditionContextImpl是ConditionEvaluator的内部类
实现了ConditionContext接口
构造
public ConditionContextImpl(@Nullable BeanDefinitionRegistry registry,
@Nullable Environment environment, @Nullable ResourceLoader resourceLoader) {
this.registry = registry;//注册器有的
this.beanFactory = deduceBeanFactory(registry);//返回上下文里面的beanFactory
this.environment = (environment != null ? environment : deduceEnvironment(registry));//环境
this.resourceLoader = (resourceLoader != null ? resourceLoader : deduceResourceLoader(registry));//resourceLoader
this.classLoader = deduceClassLoader(resourceLoader, this.beanFactory);//classLoader
}
4.注册
AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
//这里注册,放回的是set集合
public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
BeanDefinitionRegistry registry, @Nullable Object source) {
//此时source=null
//获取beanFactory
DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
if (beanFactory != null) {//不为空进入
//不为AnnotationAwareOrderComparator就设置为
//AnnotationAwareOrderComparator是OrderComparator的子类,用来支持Spring的Ordered类、@Order注解和@Priority注解。
if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {
//进入设置
beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
}
//不是ContextAnnotationAutowireCandidateResolver后选择解析器就设置
if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) {
//进入设置
beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
}
}
//bean定义默认8
Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<>(8);
//判断有没有@Confgiratuon的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));
}
//有没有@Autowire的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));
}
//检查jsr250支持,biru @Resource等
// 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));
}
//检查jps支持
// 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));
}
//事件监听后置处理器
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));
}
//事件监听工厂
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;
}
到此框架自身的bean定义加载完毕
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;
if (useDefaultFilters) {//这里默认为true
registerDefaultFilters();
}
setEnvironment(environment);
setResourceLoader(resourceLoader);
}
//过滤增加@Component
//jar250标准 @ManagedBean,jsr330标准@Inject
->protected void registerDefaultFilters() {
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.
}
}
spring容器的refresh()方法
当spring程序换这么长时间,都不敢说懂他,crud伤不起