028-86922220

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

Spring中IoC源码怎么写

本篇文章给大家分享的是有关Spring中IoC源码怎么写,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

成都创新互联-专业网站定制、快速模板网站建设、高性价比新会网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式新会网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖新会地区。费用合理售后完善,十多年实体公司更值得信赖。

一、 IoC 理论

IoC 全称为 Inversion of Control,翻译为 “控制反转”,它还有一个别名为 DI(Dependency Injection),即依赖注入。

二、IoC方式

Spring为IoC提供了2种方式,一种是基于xml,另一种是基于注解。

本次文章我们就来分析下基于注解的IoC原理,在看文章之前我们可以带一些疑问,这样有助于我们更好的理解。

  1. @Bean是干什么用的?

  2. @Controller、@Service又是干啥的?

  3. @CompoentScan注解是怎么起作用的?

  4. Spring是怎么发现@Bean、@Controller、@Service这些注解修饰的类的?

  5. 发现之后是怎么注册到IOC容器中的?

  6. IOC容器到底是个啥?

三、源码分析

首先看下段代码:

AnnotationConfigApplicationContext aac =
				new AnnotationConfigApplicationContext("com.mydemo");

AnnotationConfigApplicationContext可以实现基于Java的配置类(包括各种注解)加载Spring的应用上下文。避免使用application.xml进行配置。相比XML配置,更加便捷。

3.1、类结构图

Spring中IoC源码怎么写 主要类或接口说明:

3.2 构造函数

        //默认构造函数,初始化一个空容器,容器不包含任何 Bean 信息,需要在稍后通过调用其register()
	//方法注册配置类,并调用refresh()方法刷新容器,触发容器对注解Bean的载入、解析和注册过程
	public AnnotationConfigApplicationContext() {
		this.reader = new AnnotatedBeanDefinitionReader(this);
		this.scanner = new ClassPathBeanDefinitionScanner(this);
	}

	
	public AnnotationConfigApplicationContext(DefaultListableBeanFactory beanFactory) {
		super(beanFactory);
		this.reader = new AnnotatedBeanDefinitionReader(this);
		this.scanner = new ClassPathBeanDefinitionScanner(this);
	}

	
	//最常用的构造函数,通过将涉及到的配置类传递给该构造函数,以实现将相应配置类中的Bean自动注册到容器中
	public AnnotationConfigApplicationContext(Class... annotatedClasses) {
		//调用无参构造函数,初始化AnnotatedBeanDefinitionReader 和 ClassPathBeanDefinitionScanner
		this();
		register(annotatedClasses);
		refresh();
	}

	
	//该构造函数会自动扫描以给定的包及其子包下的所有类,并自动识别所有的Spring Bean,将其注册到容器中
	public AnnotationConfigApplicationContext(String... basePackages) {
	        //初始化ClassPathBeanDefinitionScanner和AnnotatedBeanDefinitionReader
		this();//step1
		//扫描包、注册bean
		scan(basePackages);//step2
	        refresh();//step3
	}

主要属性:

这里我们用的是最后一种构造函数,即传入一个包路径。

3.3 IoC 之 构造函数初始化

首先看step1,调用了本类的无参构造函数:

public AnnotationConfigApplicationContext() {
		this.reader = new AnnotatedBeanDefinitionReader(this);
		this.scanner = new ClassPathBeanDefinitionScanner(this);
	}

然后初始化AnnotatedBeanDefinitionReader和ClassPathBeanDefinitionScanner
我们来看下ClassPathBeanDefinitionScanner的构造函数

public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry) {
		this(registry, true);
	}

继续跟踪下去,最后调用的是这个方法:

public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
			Environment environment, @Nullable ResourceLoader resourceLoader) {

		Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
		//为容器设置加载Bean定义的注册器
		this.registry = registry;

		//是否使用默认过滤规则
		if (useDefaultFilters) {
			registerDefaultFilters();
		}
		//设置环境
		setEnvironment(environment);
		//为容器设置资源加载器
		setResourceLoader(resourceLoader);
	}

这里面最主要的是registerDefaultFilters()方法,初始化spring扫描默认过滤规则,对应@ComponentScan注解,如果没有自定义规则,就初始化默认过滤规则。
这里调用的是ClassPathScanningCandidateComponentProvider类中的registerDefaultFilters()方法:

//向容器注册过滤规则
@SuppressWarnings("unchecked")
protected void registerDefaultFilters() {
	//向要包含的过滤规则中添加@Component注解类
	//@Service和@Controller都是Component,因为这些注解都添加了@Component注解
	this.includeFilters.add(new AnnotationTypeFilter(Component.class));
	//获取当前类的类加载器
	ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
	try {
		//向要包含的过滤规则添加JavaEE6的@ManagedBean注解
		this.includeFilters.add(new AnnotationTypeFilter(
				((Class) 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 {
		//向要包含的过滤规则添加@Named注解
		this.includeFilters.add(new AnnotationTypeFilter(
				((Class) 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.
	}
}

这里面有两个关键变量:

includeFilters表示要包含的注解,即只有包含includeFilters中的注解,才会被扫描
excludeFilters表示要排除的注解,即包含excludeFilters中的注解不会被扫描

在这个方法中,includeFilters集合中添加了@Component、JavaEE6的@ManagedBean和JSR-330的@Named注解
而excludeFilters集合没做任何变动,即没有要排除的注解

总结:
所以默认规则就是,只要包含了@Component、JavaEE6的@ManagedBean和JSR-330的@Named这3个注解中的任意一个,就会被扫描

以上就是Spring中IoC源码怎么写,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


网站题目:Spring中IoC源码怎么写
当前地址:http://www.tsicrk.com/article/ghidis.html

其他资讯

让你的专属顾问为你服务

1.5066s