Introductions应用(简介的advice,作用:在不改变对象和类的代码的前提下,为对象和类添加新的方法。)
Introduction定义:简介允许一个切面声明一个实现指定接口的通知对象,并且提供一个接口实现类(接口的实现类,来代表实例化好的Bean)来代表这些对象。
Introductions的实现:由<aop:aspect>中的<aop:declare-parents>元素声明,该元素用于声明所有匹配的类型,拥有一个新的parent(接口声明的对象可以指向getBean获得的对象)。
<aop:declare-parents>属性
type-matching="expression表达式(表示匹配什么样的类型)。
implement-interface="接口的路径"(切面类的接口)。
default-impl=“切面类的路径”。
案例:
步骤1:编写逻辑功能接口(限制逻辑功能),并提供它的实现类。
public interface ServiceInterfaceFunction {
public void print();
}
public class ServiceFunction implements ServiceInterfaceFunction {
@Override
public void print() {
System.out.println("逻辑功能接口实现类的方法执行了");
}
}
public class ServiceGongNeng {
}
步骤2:配置<aspect:config>(有接口的SpringAOP实现,使用JavaSE动态代理,也就是JDK代理,JDK动态代理只能对实现了接口的类生成代理,而不能针对类,所以要配置<aop:declare-parents>)。
步骤3:测试
@Test
public void testaop(){
ApplicationContext ac=new ClassPathXmlApplicationContext("spring-aop-schema-advice.xml");
ServiceInterfaceFunction sif=(ServiceInterfaceFunction)ac.getBean("serviceGongNeng",ServiceGongNeng.class);
sif.print();
}
结果:正常可以执行接口实现类里的方法。(注意:types-matching="springAOP.ServiceGongNeng"没有execution)
重点扩展:所有基于配置文件的aspects,只支持单例模式(Single Model)。

这个是所有的接口实现类的切面类都需要这么配置么?
Aspect instantiation models
schema-defined aspects只支持singleton model
很重要
<aop:declare-parents types-matching="com.mooc.aop.schema.advice.biz.*(+)"
implement-interface="com.mooc.aop.schema.advice.Fit"
default-impl="com.mooc.aop.schema.advice.FitImpl"
>
代码调用:
Fit fit = (Fit)super.getBean("aspecctBiz");(强制转换成接口实现类中的默认)
fit.filter();
Introductions
简介允许一个切面声明一个实现指定接口的通知对象,并且提供了一个接口实现类来代表这些对象
由<aop:aspect>中的<aop:declare-parents>元素声明该元素用于声明所匹配的类型拥有一个新的parent(因此得名)
Introductions
declare-parents 用来声明匹配到的类有一个新的 parent
declare-parents 用来声明匹配到的类有一个新的 parent,其中的
type-matching="expression表达式(表示匹配什么样的类型)。
implement-interface="接口的路径"(切面类的接口)。
default-impl 指定接口默认的实现类。
位于<aop:config>标签下的<aop:aspect>标签下
Introductions应用(简介的advice,作用:在不改变对象和类的代码的前提下,为对象和类添加新的方法。)
Introduction定义:简介允许一个切面声明一个实现指定接口的通知对象,并且提供一个接口实现类(接口的实现类,来代表实例化好的Bean)来代表这些对象。
Introductions的实现:由<aop:aspect>中的<aop:declare-parents>元素声明,该元素用于声明所有匹配的类型,拥有一个新的parent(接口声明的对象可以指向getBean获得的对象)。
<aop:declare-parents>属性
type-matching="expression表达式(表示匹配什么样的类型)。
implement-interface="接口的路径"(切面类的接口)。
default-impl=“切面类的路径”。
案例:
步骤1:编写逻辑功能接口(限制逻辑功能),并提供它的实现类。
public interface ServiceInterfaceFunction {
public void print();
}
public class ServiceFunction implements ServiceInterfaceFunction {
@Override
public void print() {
System.out.println("逻辑功能接口实现类的方法执行了");
}
}
public class ServiceGongNeng {
}
步骤2:配置<aspect:config>(有接口的SpringAOP实现,使用JavaSE动态代理,也就是JDK代理,JDK动态代理只能对实现了接口的类生成代理,而不能针对类,所以要配置<aop:declare-parents>)。
步骤3:测试
@Test
public void testaop(){
ApplicationContext ac=new ClassPathXmlApplicationContext("spring-aop-schema-advice.xml");
ServiceInterfaceFunction sif=(ServiceInterfaceFunction)ac.getBean("serviceGongNeng",ServiceGongNeng.class);
sif.print();
}
结果:正常可以执行接口实现类里的方法。(注意:types-matching="springAOP.ServiceGongNeng"没有execution)
重点扩展:所有基于配置文件的aspects,只支持单例模式(Single Model)。
<aop:declare-parents types-matching = "com.imooc.aop.shema.advice.biz.*(+)" implement-interface = "com.imooc.aop.shema.advice.Fit" default-impl = "com.imooc.aop.shema.advice.FitImpl"/> 对所有匹配到的类都增加一个方法实现,FitImpl,这里是所有匹配到的biz类都可以转为Fit类 >
Introductions的配置
shcema-defined aspects 只支持单例模式
Introductions简介声明
在XML文件中配置的aspect,只支持单例的模式
declare-parents使用介绍:声明一个实现指定接口的通知对象 ,对匹配到的类getBean时可进行强制类型转换成该接口类型
type-matching:匹配对象
implement-interface:实现的接口
default-impl:接口实现类
Introductions简介
Introductions 可以为一些类在代码范围以外指定新的父类,并且指定一个默认的实现类
Introductions 声明的切面只支持单例模式
可以理解为引入,就是新的接口实现类,通过配在不改变原有的代码获得新的类和方法
Introductions 可以为一些类在代码范围以外指定新的父类,并且指定一个默认的实现类
所有introduction声明的切面只支持单例模式
Introductions简介
introductions
简介允许一个切面声明一个实现指定接口的通知对象,并且提供了一个接口实现类来代表这些对象
由<aop:aspect>中的<aop:declare-parents>元素声明该元素用于声明所匹配的类型拥有一个新的parent(因此得名)
=============
Aspect instantiation models
schema-defined aspects(所有基于配置文件的aspects)只支持singleton model(单例模式)
inplement-interface 一个指定的接口
default-impl 指定接口的实现类
Aspect instantiation models
Introductions配置
Introductions简介
注入构造函数
Introductions 允许一个实现指定接口的“通知对象”,提供一个实现类。
强制转型为接口类。
Introductions
