AnnotationConfigApplicationContext->this()->AnnotationConfigApplicationContext

public static void main(String[] args) {
	AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(AppClassCofig.class);	
}
public AnnotationConfigApplicationContext(Class<?>... componentClasses) {	
	this();
	register(componentClasses);
	refresh();
}
public AnnotationConfigApplicationContext() {
	//注册一个读取器
	this.reader = new AnnotatedBeanDefinitionReader(this);
	//创建一个扫描器
	this.scanner = new ClassPathBeanDefinitionScanner(this);
}

一、注册读取器

this.reader = new AnnotatedBeanDefinitionReader(this);

public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
		BeanDefinitionRegistry registry, @Nullable Object source) {

	//获取DefaultListableBeanFactory,可以理解就是我们平时说的BeanFactory
	DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
	if (beanFactory != null) {
		if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {
			//AnnotationAwareOrderComparator主要功能解析@Order注解和@Priority
			beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
		}
		if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) {
			//ContextAnnotationAutowireCandidateResolver提供处理延迟加载的功能
			beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
		}
	}

	//设置一个装BeanDefinitionHolder的容器,为什么固定是8?因为spring内部需要添加的就8个。
	Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<>(8);

	/*
	从beanDefinitionMap里面判断是否有“配置类解析器”。添加“配置类处理器”,这个东西非常的核心,
	会解析一个类中出是否是配置类,判断一个类是否是配置类可以进一步查看源码。
	* */
	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));
	}

	//添加“自动装配处理器” 解析@Autowired注解
	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));
	}

	//添加“常见注解处理器”    JSR是java规范的标准,250只是其中一个版本。
	// 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,如果支持添加PersistenceAnnotationProcessor,PersistenceAnnotationProcessor做什么的。
	if (jpaPresent && !registry.containsBeanDefinition(PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {
	RootBeanDefinition def = new RootBeanDefinition();
		def.setBeanClass(ClassUtils.forName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME,AnnotationConfigUtils.class.getClassLoader()));
		def.setSource(source);
		beanDefs.add(registerPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));
	}

	//“事件监听器处理器”EventListenerProcessor 做什么用的。处理@Eventlstener注解
	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));
	}

	//“事件监听器工厂” EventListenerFactory做什么的。
	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;
}

1、向DefaultListableBeanFactory添加比较器,比较器的功能还是比较多的,可以控制后置处理器的执行顺序等。
2、向DefaultListableBeanFactory添加“自动装填解析器”。
3、自动装配解析器(ContextAnnotationAutowireCandidateResolver)和“自动装配后置处理器(AutowiredAnnotationBeanPostProcessor)”什么区别。

二、添加扫描器

this.scanner = new ClassPathBeanDefinitionScanner(this);

通过this创建一个扫描器。能扫描出路径上带有特殊注解的类,并且把它转换成bd

protected void registerDefaultFilters() {
	this.includeFilters.add(new AnnotationTypeFilter(Component.class));

	ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
	
	this.includeFilters.add(new AnnotationTypeFilter(((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));
		
	this.includeFilters.add(new AnnotationTypeFilter(((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Named", cl)), false));
}

往includeFilters添加下面3种过滤器
1、Component类型的过滤器
2、javax.annotation.ManagedBean过滤器
3、javax.inject.Named过滤器
这些过滤器会识别对应的注解@Component,@javax.annotation.ManagedBean @javax.inject.Named而且三个注解是可以相互替换的。
在使用扫描器的时候,扫描就会根据这三种过滤器来获取路径上,被扫描到的Bean

Spring 中,可以使用配置文件来进行依赖注入。有两种常用的配置文件方式:XML 配置和注解配置。 1. XML 配置方式: 首先,在配置文件中定义 bean,指定其 id 和 class。然后,通过构造函数或者属性的方式注入依赖项。以下是一个示例: ```xml <!-- 配置文件:applicationContext.xml --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 定义一个需要注入的 bean --> <bean id="userService" class="com.example.UserService"> <!-- 构造函数注入 --> <constructor-arg ref="userRepository" /> </bean> <!-- 定义另一个 bean --> <bean id="userRepository" class="com.example.UserRepository"> <!-- 属性注入 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 定义数据源 bean --> <bean id="dataSource" class="com.example.DataSource" /> </beans> ``` 在代码中使用 ApplicationContext 加载配置文件,并获取需要的 bean: ```java import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyApp { public static void main(String[] args) { // 加载配置文件 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // 获取需要的 bean UserService userService = context.getBean("userService", UserService.class); userService.doSomething(); } } ``` 2. 注解配置方式: 使用注解配置可以更简洁地实现依赖注入。在类上使用 @Component 或其派生注解进行标记,表示将该类注册为一个 bean。然后,使用 @Autowired 注解自动注入依赖项。以下是一个示例: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class UserService { private UserRepository userRepository; @Autowired public UserService(UserRepository userRepository) { this.userRepository = userRepository; } // 其他方法... } @Component public class UserRepository { private DataSource dataSource; @Autowired public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } // 其他方法... } @Component public class DataSource { // 数据源相关配置... } ``` 在代码中使用 ApplicationContext 加载配置文件,并获取需要的 bean: ```java import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class MyApp { public static void main(String[] args) { // 加载配置类 ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); // 获取需要的 bean UserService userService = context.getBean(UserService.class); userService.doSomething(); } } ``` 需要注意的是,使用注解配置方式时,需要在配置类上使用 @Configuration 注解,来指示它是一个配置类,例如: ```java import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { // 配置相关的 bean } ``` 这样就完成了 Spring 的依赖注入配置文件的介绍。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

信仰_273993243

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值