Spring AOP(获取入参和返回值)及其原理:动态代理

此篇会先演示Spring AOP(获取入参和返回值),之后再演示AOP的原理:动态代理。

 

Spring AOP:

AOP思想:

AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善。OOP引入封装、继承、多态等概念来建立一种对象层次结构,用于模拟公共行为的一个集合。不过OOP允许开发者定义纵向的关系,但并不适合定义横向的关系,例如日志功能。日志代码往往横向地散布在所有对象层次中,而与它对应的对象的核心功能毫无关系对于其他类型的代码,如安全性、异常处理和透明的持续性也都是如此,这种散布在各处的无关的代码被称为横切(cross cutting),在OOP设计中,它导致了大量代码的重复,而不利于各个模块的重用。

AOP技术恰恰相反,它利用一种称为”横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其命名为”Aspect”,即切面。所谓”切面”,简单说就是那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块之间的耦合度,并有利于未来的可操作性和可维护性。

使用”横切”技术,AOP把软件系统分为两个部分:核心关注点和横切关注点。业务处理的主要流程是核心关注点,与之关系不大的部分是横切关注点。横切关注点的一个特点是,他们经常发生在核心关注点的多处,而各处基本相似,比如权限认证、日志、事务。AOP的作用在于分离系统中的各种关注点,将核心关注点和横切关注点分离开来。

AOP核心概念

1、横切关注点

对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点

2、切面(aspect)

类是对物体特征的抽象,切面就是对横切关注点的抽象

3、连接点(joinpoint)

被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器

4、切入点(pointcut)

对连接点进行拦截的定义

5、通知(advice)

所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类

6、目标对象

代理的目标对象

7、织入(weave)

将切面应用到目标对象并导致代理对象创建的过程

8、引入(introduction)

在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段

示例代码:

首先定义一个HelloWorld接口:

/**
 * 接口:HelloWorld
 */
public interface HelloWorld {
    /**
     * @Return:java.lang.String
     * @Date:Create in 15:07 2018/2/28
     * @Description:演示抽象方法
     */
    String  printHelloWorld(String name);
}

HelloWorld接口实现类HelloWorldFirstImpl:

/**
 * HelloWorld接口的实现类
 * Created by wanda on 2018/1/31.
 */
public class HelloWorldFirstImpl implements HelloWorld {
    @Override
    public String  printHelloWorld(String name) {
        System.out.println("我是演示方法printHelloWorld");
        return "first";
    }
}

环绕增强,日志LogHandle:

public class LogHandle {
    /**
     * @Param:[point]
     * @Return:void
     * @Description:演示的日志切面方法(环绕增强,且得到切点的参数和返回值)
     */
    public void printLog(ProceedingJoinPoint  point) {
        Object[] args = point.getArgs();
        for (Object arg : args) {
            System.out.println("参数="+arg+" ");
        }
        try {
            Object object=point.proceed();
            System.out.println("返回值="+object);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println("环绕增强:日志log");
    }
}

 

前后增强,计算时间TimeHandle:

 

public class TimeHandle {
    /**
     * @Param:[]
     * @Return:void
     * @Description:演示的时间切面方法(前后增强),记录方法开始时间和结束时间
     */
    public void printTime(){
        System.out.println("CurrentTime="+System.currentTimeMillis());
    }
}

 

aop.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: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="helloWorldImplFirst" class="demo.AopDemo.helloworldimpl.HelloWorldFirstImpl"/>

    <bean id="timeHandle" class="demo.AopDemo.TimeHandle"/>
    <bean id="logHandle" class="demo.AopDemo.LogHandle"/>

    <!--前后增强-->
    <aop:config>
        <!--order越小距离方法越远-->
        <aop:aspect id="time" ref="timeHandle" order="1">
            <aop:pointcut id="addAllMethod" expression="execution(* demo.AopDemo.helloworldimpl.*.*(..))"/>
            <aop:before method="printTime" pointcut-ref="addAllMethod"/>
            <aop:after method="printTime" pointcut-ref="addAllMethod"/>
        </aop:aspect>
    </aop:config>

   <!--环绕增强-->
    <aop:config>
        <!--order越大距离方法越近-->
        <aop:aspect id="log" ref="logHandle" order="2">
            <aop:pointcut id="addLog" expression="execution(* demo.AopDemo.helloworldimpl.*.*(..))"/>
            <aop:around method="printLog" pointcut-ref="addLog"/>
        </aop:aspect>
    </aop:config>

</beans>

演示类TestMain:

public class TestMain {
    /**
     * @Param:[args]
     * @Return:void
     * @Description:测试AOP的main函数
     */
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("aop.xml");

        HelloWorld hw1=(HelloWorld) ctx.getBean("helloWorldImplFirst");

        hw1.printHelloWorld("xiaoming");
    }
}

控制台输出:

《Spring AOP(获取入参和返回值)及其原理:动态代理》

第一个时间是前置增强,第二个时间是后置增强。

两者中间的是环绕增强的输出,除了“我是演示方法printHelloWorld”这句话.

而且这句话总是于“返回值=first”之前打印,原因很简单:这句话的打印语句,处于方法体的return之前。

 

——-我是—————————————–分割线————————————————-

趁热打铁

接着演示Aop的原理–动态代理:

示例代码:

首先需要一个接口Interface:

/**
 * @Description:一个接口,两个抽象方法
 */
public interface Interface {
    void doSomething();
    void somethingElse(String arg);
}

然后RealObject类实现接口Interface:

/**
 * @Description:Interface接口的实现类
 */
public class RealObject implements Interface {
    @Override
    public void doSomething() {
        System.out.println("do Something");
    }

    @Override
    public void somethingElse(String arg) {
        System.out.println("do else thing:"+arg);
    }
}

最后还需要一个InvocationHandler接口的实现类DynamicProxyHandler:

/**
 * @Description:动态代理对象处理器
 */
public class DynamicProxyHandler implements InvocationHandler {
    private Object proxyed;

    public DynamicProxyHandler(Object proxyed){
        this.proxyed=proxyed;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("代理对象开始工作了-----");
        return method.invoke(proxyed,args);
    }
}

主函数演示类TestMain:

/**
 * @Description:动态代理演示主函数类
 */
public class TestMain {
    public static void main(String[] args) {
        RealObject real=new RealObject();
        //通过调用Proxy静态方法Proxy.newProxyInstance()可以创建动态代理,
        // 这个方法需要得到一个类加载器,一个你希望该代理实现的接口列表(不是类或抽象类),以及InvocationHandler的一个实现类。
        Interface proxy=(Interface) Proxy.newProxyInstance(
                Interface.class.getClassLoader(),new Class[]{Interface.class},new DynamicProxyHandler(real)
        );
        proxy.doSomething();
        proxy.somethingElse("test");
    }
}

控制台打印:

《Spring AOP(获取入参和返回值)及其原理:动态代理》

SpringAop的增强处理点:就在于InvocationHandler接口的实现类DynamicProxyHandler的重写方法invoke中。

“代理对象开始工作了”这句输出打印,就是一种前置增强(before增强)。

注:

动态代理部分转载于:https://www.cnblogs.com/luoxn28/p/5686794.html

如有侵权,请联系删除。

 

—————————————————————————不关注我“象话”吗?

如有疑惑,请评论留言。

如有错误,也请评论留言。

    原文作者:AOP
    原文地址: https://blog.csdn.net/qq_34908167/article/details/79219769
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞