spring-aop源码解析:declare-parent
来源:互联网 发布:sql 每小时 编辑:程序博客网 时间:2024/04/30 20:09
spring-aop源码解析:declare-parent
借助Aop的引入,我们可以不侵入性的改变现有的实现,对现有实现类无侵入性的增加方法。
1、举个例子:Einstein从小爱发明,是个发明家,我们创建一个Einstein类,实现发明家(Inventor)接口。随着知识的积累,Einstein也成为了数学家,我们可以在不改变Eistein类的前提下使用declare-parent为Eistein添加数学家的特性。
package com.zhc.aop.declareparent;public interface Inventor {public void invent();}
package com.zhc.aop.declareparent;public class Einstein implements Inventor {@Overridepublic void invent() {// TODO Auto-generated method stubSystem.out.println("create new thing");}}
package com.zhc.aop.declareparent;public interface Mathematician {public void calculate();}
package com.zhc.aop.declareparent;public class MathematicianImpl implements Mathematician {@Overridepublic void calculate() {System.out.println("calculate the result of the formulae");}}
spring-aop.xml配置declare-parent,为Einstein添加特性
<bean id="Einstein" class="com.zhc.aop.declareparent.Einstein"></bean> <aop:config> <aop:aspect> <aop:declare-parents types-matching="com.zhc.aop.declareparent.Einstein" implement-interface="com.zhc.aop.declareparent.Mathematician" default-impl="com.zhc.aop.declareparent.MathematicianImpl"/> </aop:aspect> </aop:config>
测试类:
import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class DeclareParentTest {@Testpublic void testEinstein(){String xml = "classpath:spring-aop.xml"; ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { xml }); Inventor inventor = (Inventor) context.getBean("Einstein"); inventor.invent(); Mathematician mathematician = (Mathematician) context.getBean("Einstein"); mathematician.calculate(); }}
2、源码分析
ConfigBeanDefinitionParser同过parseDeclareParents方法来解析declare-parents 标签
/** * Parse a '<code>declare-parents</code>' element and register the appropriate * DeclareParentsAdvisor with the BeanDefinitionRegistry encapsulated in the * supplied ParserContext. * 解析一个declare-parents标签,并且使用BeanDefinitionRegistry注册一个合适的DeclareParentsAdvisor * BeanDefinitionRegistry封装在了parserContext中 */private AbstractBeanDefinition parseDeclareParents(Element declareParentsElement, ParserContext parserContext) {//BeanDefinition是由BeanDefinitionBuilder创建的,在builder创建BeanDefinition之前,//要设置根bean定义(rootBeanDefinition),它指定了bean的Class//declare-parents标签内部实现方式是创建了一个切面BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DeclareParentsAdvisor.class);//添加构造函数的参数//这里的构造函数是指DeclareParentsAdvisor的构造函数builder.addConstructorArgValue(declareParentsElement.getAttribute(IMPLEMENT_INTERFACE));//新加的接口builder.addConstructorArgValue(declareParentsElement.getAttribute(TYPE_PATTERN));//类型匹配表达式String defaultImpl = declareParentsElement.getAttribute(DEFAULT_IMPL);//新加接口的默认实现String delegateRef = declareParentsElement.getAttribute(DELEGATE_REF);//新加接口的实现引用//新接口实现类和实现引用指定一个即可,且只能指定一个if (StringUtils.hasText(defaultImpl) && !StringUtils.hasText(delegateRef)) {builder.addConstructorArgValue(defaultImpl);}else if (StringUtils.hasText(delegateRef) && !StringUtils.hasText(defaultImpl)) {builder.addConstructorArgReference(delegateRef);}else {parserContext.getReaderContext().error("Exactly one of the " + DEFAULT_IMPL + " or " + DELEGATE_REF + " attributes must be specified",declareParentsElement, this.parseState.snapshot());}//获取bean定义AbstractBeanDefinition definition = builder.getBeanDefinition();definition.setSource(parserContext.extractSource(declareParentsElement));//注册bean定义parserContext.getReaderContext().registerWithGeneratedName(definition);return definition;}
0 0
- spring-aop源码解析:declare-parent
- spring源码解析-Aop
- Spring源码解析-aop
- Spring源码解析-AOP
- spring源码解析-AOP原理
- spring源码aop解析(一)
- spring AOP 源码解析 及其实现原理
- spring aop自动配置源码解析
- Spring源码解析之-Aop源码解析(2)
- Spring源码解析之四 ------ AOP原理和源码分析
- Spring源码分析:AOP源码解析(上篇)
- Spring源码分析:AOP源码解析(下篇)
- Spring源码分析:AOP源码解析(上篇)
- Spring源码分析:AOP源码解析(下篇)
- Spring Framework源码(六):Spring AOP之解析标签
- spring中parent属性解析
- Spring----AOP面向切面编程例子解析(附源码)
- spring 源码探索 -- aop 标签解析和创建代理
- mac git
- 15级新同学风面会
- C++ map使用详解
- 代码篇C#网络编程之tcp异步客户端
- 《哈利波特》学院
- spring-aop源码解析:declare-parent
- 聊一聊Android的消息机制
- RMQ_st
- 用Java编写一个简单的编译器
- 代码篇C#网络编程之UDP进程通信
- Linux进程理解与实践(四)wait函数处理僵尸进程
- plist属性列表 文件的读写
- 如何在dop中Key值
- C++--字符串