Aop和动态代理的使用

1.问题提出:

在传统的业务处理代码中,通常都会进行事务处理、日志记录等操作。
虽然使用OOP可以通过组合或者继承的方式来达到代码的重用,
但如果要实现某个功能(如日志记录),同样的代码仍然会分散到各个方法中。
这样,如果想要关闭某个功能,或者对其进行修改,就必须要修改所有的相关方法。
这不但增加了开发人员的工作量,而且提高了代码的出错率。

2.解决办法

 (1)抽取个方法。如果方法很多,还需要在很多地方调用。
 (2)动态代理: 实现方式有两种:
 		 [1]JDK原生动态代理----缺点:必须基于接口完成 
 		 [2]cglib动态代理
  (3)AOP面向切面编程:AOP的底层实现就是基于动态代理。

3.动态代理解决问题

3.1 JDK原生动态代理

(我们仅仅建立一个maven工程不需要web即可)
第一步:建立一个接口及实现类

public interface MathService {

    public double add(double a, double b);

    public double sub(double a, double b);

    public double mul(double a, double b);

    public double div(double a, double b);
}
public class MathServiceImpl implements MathService {
    public double add(double a, double b) {
        double result=a+b;
        return result;
    }

    public double sub(double a, double b) {
        double result=a-b;
        return result;
    }

    public double mul(double a, double b) {
        double result=a*b;
        return result;
    }

    public double div(double a, double b) {
        double result=a/b;
        int c = 2/0;
        return result;
    }
}

第二步:创建代理类

public class ProxyFactory {
    //被代理对象
    private Object target;

    public ProxyFactory(Object object) {
        this.target = object;
    }
    //获取代理对象
    /**
     * ClassLoader loader, 被代理对象的类加载器
     * Class<?>[] interfaces, 被代理对象实现的接口
     * InvocationHandler h: 当代理对象执行被代理的方法时,会触发该对象中的invoke功能
     */
    public Object getProxy(){
        /*固定语法*/
        ClassLoader loader = target.getClass().getClassLoader();
        Class<?>[] interfaces = target.getClass().getInterfaces();

        InvocationHandler h = new InvocationHandler(){
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                /*这里是运行之前执行 method.getName() 得到方法名 Arrays.asList(args)转化为能看的懂的数组   */
                System.out.println(method.getName()+"\nbefore=========="+ Arrays.asList(args)); //根据个人写自己的业务需求
                Object result = method.invoke(target, args);//代理对象回调该方法 固定写法
                /*运行之后执行*/
                System.out.println("after======="); //根据个人写自己的业务需求
                return result;
            }
        };
       return  Proxy.newProxyInstance(loader,interfaces,h);
    }
}

第三步:测试

public class Test {
    public static void main(String[] args) {
        MathServiceImpl mathService = new MathServiceImpl();
        ProxyFactory proxyFactory = new ProxyFactory(mathService);
        MathService proxy = (MathService)proxyFactory.getProxy();
        Object r = proxy.add(5, 6);
        System.out.println(r);
    }
}

运行结果
在这里插入图片描述
所以在不修改源代码的情况下 可以加入自己想加入的业务需求

3.2 cjlib动态代理

步骤:第一步 引入依赖

 		<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.15.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>repMaven.cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.2.5</version>
        </dependency>

第二步:创建代理工厂

/**
 *    (1)引入cglib的jar包.
 *    (2)创建一个代理类工厂并实现接口MethodInterceptor
 **/
public class ProxyFactory  implements MethodInterceptor {

    private Object target;

    public ProxyFactory(Object target) {
        this.target = target;
    }
    //获取代理对象
    public Object getProxy(){
        Enhancer enhancer=new Enhancer();
        //指定被代理对象的父类
        enhancer.setSuperclass(target.getClass()); 
        //指定回调类
        enhancer.setCallback(this);
        //创建代理对象
        return enhancer.create();
    }

    //当代理对象执行代理方法时触发的方法
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        System.out.println("before*************************");//这里书写自己的业务代码
        Object r = method.invoke(target, args);//固定写法
        System.out.println("after===========================");//这里书写自己的业务代码
        return r;
    }
}

第三步:测试

public class Test {
    public static void main(String[] args) {
        MathServiceImpl mathService=new MathServiceImpl();
        ProxyFactory proxyFactory=new ProxyFactory(mathService);
        MathServiceImpl proxy = (MathServiceImpl) proxyFactory.getProxy();
       Object r= proxy.sub(10,5);
        System.out.println(r);
    }
}

在这里插入图片描述
两者区别:cjlib可以不实现接口 但jdk必须要实现接口

3.3 AOP动态代理
AOP(Aspect-Oriented Programming, 面向切面编程): 
是一种新的方法论, 是对传统 OOP(Object-OrientedProgramming, 面向对象编程)的补充.

AOP 的主要编程对象是切面(aspect), 而切面模块化横切关注点在应用 AOP 编程时, 
仍然需要定义公共功能, 但可以明确的定义这个功能在哪里, 
以什么方式应用, 并且不必修改受影响的类. 
这样一来横切关注点就被模块化到特殊的对象(切面)里.

AOP 的好处: 每个事物逻辑位于一个位置, 代码不分散, 便于维护和升级 业务模块更简洁, 
只包含核心业务代码.

AOP中的方法
	before 目标方法执行前执行,前置通知
	after 目标方法执行后执行,后置通知
	afterReturning 目标方法返回时执行 ,后置返回通知
	afterThrowing 目标方法抛出异常时执行 异常通知
	around 在目标函数执行中执行,可控制目标函数是否执行,环绕通知

步骤
(1)引入依赖

		<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.15.RELEASE</version>
        </dependency>
         <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.2.15.RELEASE</version>
        </dependency>

(2)修改spring.xml配置文件

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--包扫描-->
    <context:component-scan base-package="com.wx" />
    <!--开启切面驱动-->
    <aop:aspectj-autoproxy />
</beans>

(3)创建代理工厂

/*这里注解必须要加 因为要交给spring管理 所以在service层 还要加上@Service*/
@Component
@Aspect
public class ProxyFactory  {

    /*目标方法之前调用执行*/
    @Before(value = "execution(* com.wx.service.impl.MathServiceImpl.add(..))")
    public void MathBefore(){
        System.out.println("before=====");
    }

    /*目标方法之后调用执行*/
    @After(value = "execution(* com.wx.service.impl.MathServiceImpl.add(..))")
    public void MathAfter(){
        System.out.println("after=====");
    }

    /*有异常的时候调用执行*/
    @AfterThrowing(value = "execution(* com.wx.service.impl.MathServiceImpl.div(..))")
    public void MathAfterThrowing(){
        System.out.println("afterThrowing=====");
    }

    //有返回值的时候调用
    @AfterReturning(value = "execution(* com.wx.service.impl.MathServiceImpl.*(..))",returning = "r")
    public void MathAfterReturning(JoinPoint joinPoint,Object r){
        Object[] args = joinPoint.getArgs();
        System.out.println("参数:"+ Arrays.asList(args));
        System.out.println(r);
        System.out.println("AfterReturning=====");
    }
}

测试类

public class Test {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("spring.xml");
        MathService ms = (MathService)app.getBean("mathServiceImpl");
        //System.out.println(ms.div(2, 5));
        //System.out.println(ms.div(2,0));
        double add = ms.mul(1, 2);
        System.out.println(add);
    }
}

AOP的底层实现就是基于动态代理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值