Spring AOP的实现以及JDK动态代理与CGLIB的实现

:AOP:(Aspect Oriented Programming)即面向切面编程,是在OOP基础上增加了对公共方法的统一调度管理;

:AOP的核心概念:

  1. 横切关注点:对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点;
  2. 切面:类是对物体特征的抽象,切面就是对横切关注点的抽象;
  3. 连接点:被拦截到的点,因为Spring只支持方法类型的连接点,所以Spring中连接点的就是被拦截到的方法,实际上连接点还可以是字段或者构造器;
  4. 切入点:对连接点进行拦截的定义;
  5. 通知:所谓通知指的就是拦截到连接点时候要执行的代码,通知分为前置,后置,异常,最终,环绕通知五类;
  6. 目标对象:代理的目标对象;
  7. 织入:将切面应用到目标对象并导致代理对象创建的过程;
  8. 引入:在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段;

:Spring创建代理的规则:

  1. 默认使用动态代理来创建AOP代理,这样就可以为任何接口的实例创建代理了;
  2. 当需要代理的类不是代理接口的时候,Spring会切换为使用CGLIB代理,也可强制使用CGLIB代理

:JDK动态代理与CGLIB代理的实现

  1. JDK动态代理的实现:

接口:

package com.deng.design;

/*

 * JDK动态代理只能针对接口做代理,不能针对类做代理

 * */

public interface Dao {

                                       public void insert();

                                      

                                       public void delete();

                                      

                                       public void update();

}

 

接口的实例对象

package com.deng.design;

 

public class DaoImpl implements Dao {

 

                                       @Override

                                       public void insert() {

                                             System.out.println("This is insert()");

                                       }

 

                                       @Override

                                       public void delete() {

                                             System.out.println("This is delete()");

 

                                       }

 

                                       @Override

                                       public void update() {

                                             System.out.println("This is update()");

 

                                       }

 

}

 

 

代理过程的实现:

package com.deng.design;

 

import java.lang.reflect.InvocationHandler;

import java.lang.reflect.Method;

 

import javax.script.Invocable;

import javax.script.ScriptException;

 

public class LogInvocationHandler implements InvocationHandler {

                                      

                                       private Object obj;

                                      

                                       public LogInvocationHandler(Object obj) {

                                             super();

                                             this.obj = obj;

                                       }

                                      

                                       @Override

                                       public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                                             String methodName = method.getName();

                                             if("insert".equals(methodName) || "update".equals(methodName)) {

                                                      System.out.println(methodName + "开始执行::" + System.currentTimeMillis() );

                                                      Object result = method.invoke(obj, args);

                                                      System.out.println(methodName + "执行结束:" + System.currentTimeMillis());

                                                      return result;

                                             }

                                             return method.invoke(obj, args);

                                       }

 

 

}

测试:

package com.deng.design;

 

import java.lang.reflect.Proxy;

 

public class JDK_Proxy {

                                       public static void main(String[] args) {

                                             Dao dao = new DaoImpl();

                                            

                                             //Proxy内库的newProxyInstance方法返回被代理对象(DaoImpl)的一个实例,然后向上转型转化为对应的接口

                                             Dao proxyDao = (Dao) Proxy.newProxyInstance(LogInvocationHandler.class.getClassLoader(),

                                                               new Class<?>[] { Dao.class }, new LogInvocationHandler(dao));

 

                                             proxyDao.insert();

                                             System.out.println("----------分割线----------");

                                             proxyDao.delete();

                                             System.out.println("----------分割线----------");

                                             proxyDao.update();

                                       }

}


2.CGLIB代理的实现(接口与接口的实例与JDK的动态代理一致)

因为在该过程中实现了MethodInterceptor接口,需要两个cglib-jar和asm-jar包

CGLIB代理过程:

package com.deng.design;

 

import java.lang.reflect.Method;

 

import net.sf.cglib.proxy.MethodInterceptor;

import net.sf.cglib.proxy.MethodProxy;

 

public class CGLIBDaoProxy implements MethodInterceptor{

                                      

                                       @Override

                                       public Object intercept(Object object, Method method, Object[] objects, MethodProxy proxy) throws Throwable {

                                             String methodName = method.getName();

                                             if("insert".equals(methodName) || "update".equals(methodName)) {

                                                      System.out.println(methodName + "()方法开始时间:" + System.currentTimeMillis());

                                                      proxy.invokeSuper(object, objects);

                                                      System.err.println(methodName + "()结束时间:" + System.currentTimeMillis());

                                                     

                                                      return object;

                                                     

                                             }

                                             proxy.invokeSuper(object, objects);

                                             return object;

                                       }

 

}

测试:

package com.deng.design;

 

import net.sf.cglib.proxy.Enhancer;

 

public class CGLIB_test {

                                       public static void main(String[] args) {

                                             CGLIBDaoProxy daoProxy = new CGLIBDaoProxy();

 

                                             Enhancer enhancer = new Enhancer();

                                             enhancer.setSuperclass(DaoImpl.class);

                                             enhancer.setCallback(daoProxy);

 

                                             Dao dao = (DaoImpl) enhancer.create();

                                             dao.insert();

                                             System.out.println("----------分割线----------");

                                             dao.delete();

                                             System.out.println("----------分割线----------");

                                             dao.update();

                                       }

}

 

                                         

,基于SpringAOP简单实现:

  1. 定义一个接口:

public interface HelloWorld
{
   void 
printHelloWorld();
   void 
doPrint();
}

  1. 定义两个接口的实现类:

public class HelloWorldImpl1 implements HelloWorld
{
   public void 
printHelloWorld()
   {
       System.out.println(
"Enter HelloWorldImpl1.printHelloWorld()");
   }
   
   public void 
doPrint()
   {
       System.out.println(
"Enter HelloWorldImpl1.doPrint()");
       return ;
   }
}

public class HelloWorldImpl2 implements HelloWorld
{
   public void 
printHelloWorld()
   {
       System.out.println(
"Enter HelloWorldImpl2.printHelloWorld()");
   }
   
   public void 
doPrint()
   {
       System.out.println(
"Enter HelloWorldImpl2.doPrint()");
       return ;
   }
}

  1. 定义横切关注点,这里的方法是打印时间

public class TimeHandler
{
   public void 
printTime()
   {
       System.out.println(
"CurrentTime = " + System.currentTimeMillis());
   }
}

  1. 接下来是Spring aop.xml的配置:

从这里可以看出:SpringAOP代理由SpringIOC容器负责生成,管理,其依赖关系也由IOC容器负责管理,因此,AOP代理可以直接使用容器中的其它bean实例作为目标,这种关系可由IOC容器的依赖注入提供;

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"
>
       
     
<bean id="helloWorldImpl1" class="com.xrq.aop.HelloWorldImpl1" />
     <bean id="helloWorldImpl2" class="com.xrq.aop.HelloWorldImpl2" />
     <bean id="timeHandler" class="com.xrq.aop.TimeHandler" />
       
     
<aop:config>
       <aop:aspect id="time" ref="timeHandler">
          <aop:pointcut id="addAllMethod" expression="execution(* com.xrq.aop.HelloWorld.*(..))" />
          <aop:before method="printTime" pointcut-ref="addAllMethod" />
          <aop:after method="printTime" pointcut-ref="addAllMethod" />
       </aop:aspect>
     </aop:config>
</beans>

  1. 在主函数中测试:

public static void main(String[] args)
{

   ApplicationContext ctx = 
           new ClassPathXmlApplicationContext("aop.xml");
       
   HelloWorld hw1 = (HelloWorld)ctx.getBean(
"helloWorldImpl1");
   HelloWorld hw2 = (HelloWorld)ctx.getBean("helloWorldImpl2");
   hw1.printHelloWorld();
   System.out.println();
   hw1.doPrint();
   
   System.out.println();
   hw2.printHelloWorld();
   System.out.println();
   hw2.doPrint();
}

结果:

CurrentTime = 1446129611993
Enter HelloWorldImpl1.printHelloWorld()
CurrentTime = 1446129611993

CurrentTime = 1446129611994
Enter HelloWorldImpl1.doPrint()
CurrentTime = 1446129611994

CurrentTime = 1446129611994
Enter HelloWorldImpl2.printHelloWorld()
CurrentTime = 1446129611994

CurrentTime = 1446129611994
Enter HelloWorldImpl2.doPrint()
CurrentTime = 1446129611994

可以看到接口的两个实现类的所有方法都加上了代理,代理内容就是打印时间;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值