栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring注解驱动开发(springmvc注解)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring注解驱动开发(springmvc注解)

实例化完成后接下来就需要对类中的属性进行依赖注入操作,类里面属性和方法的依赖注入一般用@Autowired或者@Resource 注解,核心方法为applyMergedBeanDefinitionPostProcessors

protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class beanType, String beanName) {
	for (BeanPostProcessor bp : getBeanPostProcessors()) {
		if (bp instanceof MergedBeanDefinitionPostProcessor) {
			MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp;
			bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName);
		}
	}
}

也是beanPostProcessor接口的典型运用

CommonAnnotationBeanPostProcessor:
支持了@PostConstruct@PreDestroy,@Resource注解

AutowiredAnnotationBeanPostProcessor:
支持 @Autowired,@Value注解

一、CommonAnnotationBeanPostProcessor注解的收集

//到父类中取标记这两个注解的类
public CommonAnnotationBeanPostProcessor() {
		setOrder(Ordered.LOWEST_PRECEDENCE - 3);
		setInitAnnotationType(PostConstruct.class);
		setDestroyAnnotationType(PreDestroy.class);
		ignoreResourceType("javax.xml.ws.WebServiceContext");
}


@Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class beanType, String beanName) {
	//扫描@PostConstruct@PreDestroy
	super.postProcessMergedBeanDefinition(beanDefinition, beanType, beanName);
	//扫描方法或属性上是否有@Resource注解,有就收集起来封装成对象
	Injectionmetadata metadata = findResourcemetadata(beanName, beanType, null);
	metadata.checkConfigMembers(beanDefinition);
}

//扫描@PostConstruct@PreDestroy,如果缓存为空,则走此方法
private Lifecyclemetadata buildLifecyclemetadata(final Class clazz) {
		if (!AnnotationUtils.isCandidateClass(clazz, Arrays.asList(this.initAnnotationType, this.destroyAnnotationType))) {
			return this.emptyLifecyclemetadata;
		}

		List initMethods = new ArrayList<>();
		List destroyMethods = new ArrayList<>();
		Class targetClass = clazz;

		do {
			final List currInitMethods = new ArrayList<>();
			final List currDestroyMethods = new ArrayList<>();
			//遍历类中所有的方法
			ReflectionUtils.doWithLocalMethods(targetClass, method -> {
				//判断有没有@PostConstruct,有则加入到集合中
				if (this.initAnnotationType != null && method.isAnnotationPresent(this.initAnnotationType)) {
					LifecycleElement element = new LifecycleElement(method);
					currInitMethods.add(element);
					if (logger.isTraceEnabled()) {
						logger.trace("Found init method on class [" + clazz.getName() + "]: " + method);
					}
				}
				//判断有没有@PreDestroy,有则加入到集合中
				if (this.destroyAnnotationType != null && method.isAnnotationPresent(this.destroyAnnotationType)) {
					currDestroyMethods.add(new LifecycleElement(method));
					if (logger.isTraceEnabled()) {
						logger.trace("Found destroy method on class [" + clazz.getName() + "]: " + method);
					}
				}
			});
			//加入初始化和销毁方法
			initMethods.addAll(0, currInitMethods);
			destroyMethods.addAll(currDestroyMethods);
			targetClass = targetClass.getSuperclass();
		}
		while (targetClass != null && targetClass != Object.class);

		return (initMethods.isEmpty() && destroyMethods.isEmpty() ? this.emptyLifecyclemetadata :
				new Lifecyclemetadata(clazz, initMethods, destroyMethods));
}

获取@Resource注解的逻辑和上面差不多,从类中获取所有 Field 对象,循环 field 对象,判断 field 有没有@Resource 注解,如果有注解封装
成 ResourceElement,获取Method对象一样, 最终把两个 field 和 Method 封装的对象集合封装到 Injectionmetadata 对象的injectedElements属性中。

二、AutowiredAnnotationBeanPostProcessor注解的收集:

//定义收集的注解@Autowired和@Value
public AutowiredAnnotationBeanPostProcessor() {
		this.autowiredAnnotationTypes.add(Autowired.class);
		this.autowiredAnnotationTypes.add(Value.class);
		try {
			this.autowiredAnnotationTypes.add((Class)
					ClassUtils.forName("javax.inject.Inject", AutowiredAnnotationBeanPostProcessor.class.getClassLoader()));
			logger.trace("JSR-330 'javax.inject.Inject' annotation found and supported for autowiring");
		}
		catch (ClassNotFoundException ex) {
			// JSR-330 API not available - simply skip.
		}
	}

@Autowired注解的收集过程跟上述@Resource调用的是同一个方法,也是收集Field和Method上面的注解,然后放到Injectionmetadata 对象中。
上面收集到的 injectedElements 这个集合,会在以后的依赖注入的时候使用。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/773291.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号