記錄千鋒day01
一、什么是Spring的AOP(面向切面編程)采取橫向機制即動態(tài)代理的方式創(chuàng)建一個代理對象。
1械媒、動態(tài)代理即為生成一個和原來類方法一摸一樣的類冰蘑,在方法內(nèi)調(diào)用原來類的方法和泌,在方法前后可以加入額外的功能代碼。
二祠肥、目前流行的AOP框架有兩個武氓,Spring的AOP和AspectJ
三、AOP的實現(xiàn),jdk的動態(tài)代理聋丝,CGLIB的動態(tài)代理索烹。
1、基于jdk的動態(tài)代理弱睦,用到java.lang.reflectt.Proxy類
UserService userService=new UserServiceImpl();
UserService userService1 = (UserService)Proxy.newProxyInstance(BeanFactor.class.getClassLoader(),userService.getClass().getInterfaces(),new InvocationHandler() {
@Override
? ? public Object invoke(Object proxy,Method method,Object[]args)throws Throwable {
System.out.println("before");
//userService原來類的執(zhí)行方法
? ? ? ? Object obj =method.invoke(userService,args);
System.out.println("after");
return obj;
}
});
newProxyInstance()后的參數(shù)第一個放類加載器百姓,第二個放原本類的接口,第三個給一個實現(xiàn)InvocationHandler接口的類况木。
2垒拢、基于CGLIB的動態(tài)代理,用到cglib.proxy.Enhancer類
Enhancer enhancer=new Enhancer();
UserService userService=new UserServiceImpl();
enhancer.setSuperclass(UserService.class);
enhancer.setCallback(new MethodInterceptor() {
@Override
? ? public Object intercept(Object o,Method method,Object[]objects,MethodProxy methodProxy)throws Throwable {
System.out.println("before");
Object obj =method.invoke(userService,objects);
System.out.println("after");
return obj;
}
});
Object obj =enhancer.create();
第一setSuperclass設置原來類的父類火惊,第二setCallback調(diào)回原來的方法調(diào)用求类,參數(shù)給個實現(xiàn)MethodInterceptor接口的類,該接口為cglib.proxy.MethodInterceptor包的接口屹耐。
JDK動態(tài)代理相比較CGLIB動態(tài)代理有一定局促行尸疆,前者必須要實現(xiàn)接口才行,而CGLIB不需要實現(xiàn)接口惶岭,它采用反射對目標類生成子類寿弱,并對子類進行增強。
四按灶、Spring框架中集成了CGLIB所需要的jar包症革。
1、基于xml方式動態(tài)代理
<bean id="is" class="com.qianfeng.IServiceImpl"></bean>
? ? <bean id="as" class="com.qianfeng.service.Aspect2" ></bean>
? ? <bean id="bf" class="org.springframework.aop.framework.ProxyFactoryBean">
? ? ? ? <property name="interfaces" value="com.qianfeng.IService"/>
? ? ? ? <property name="target" ref="is"/>
? ? ? ? <property name="interceptorNames" value="as"/>
? ? ? ? <property name="optimize" value="true"/>
? ? </bean>
public class Aspect implements MethodInterceptor {
public void before(){
System.out.println("---before---");
}
public void after(){
System.out.println("---after---");
}
@Override
? ? public Object invoke(MethodInvocation invocation)throws Throwable {
before();
Object obj =invocation.proceed();
after();
return obj;
}
}
這里用到的是org.springframework.aop.framework.ProxyFactoryBean類鸯旁,該類中設置4個屬性噪矛,interfaces即為接口類或者父類,target即為目標代理的類铺罢,后面為ref屬性艇挨,interceptorNames為寫入的增強方法的類(該類需要實現(xiàn)MethodInterceptor接口)該接口為org.aopalliance.intercept包下。
五畏铆、ProxyBeanFactory雷袋、ApplicationContext和BeanFactory的區(qū)別
BeanFactory是頂級接口,是Spring的核心容器辞居。它是管理bean的工廠楷怒,提供了完整的IoC(控制反轉(zhuǎn))服務支持,負責初始化各種的bean瓦灶。
ProxyBeanFactory是BeanFactory的實現(xiàn)類鸠删,它是負責管理動態(tài)代理類的bean工廠,負責動態(tài)代理類的初始化贼陶。
ApplicationContext是BeanFactory的子接口刃泡,也被稱為應用上下文巧娱,是Spring的另一個常用的核心容器。它不僅包含了BeanFactory的所有功能烘贴,還添加了國際化禁添、資源訪問、事件傳播等方面的支持桨踪。(實現(xiàn)ApplicationContext的兩種方式老翘,ClassPathXmlApplicationContext、FileSystemXmlApplicationContext).