[Java] Spring aop

36 篇文章 0 订阅
1 篇文章 0 订阅

第一章 AOP简介

1.1 JDK 的动态代理

不多bb,直接上代码

//接口
package com.itheima.proxy.jdk;

public interface TargetInterface {

    void save();
}

//实现类
package com.itheima.proxy.jdk;

public class Target implements TargetInterface{
    public void save() {
        System.out.println("我是原来的方法");
    }
}
//我们定义的增强方法
package com.itheima.proxy.jdk;

public class Advice {

    public void before(){
        System.out.println("前置增强!");
    }

    public void after(){
        System.out.println("后置增强!");
    }
}

//动态代理
package com.itheima.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyTest {

    public static void main(String[] args) {

        //创建目标对象
        final Target target = new Target();

        //获得增强对象
        final Advice advice = new Advice();

        //这是生成的动态代理的对象
        TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                advice.before();

                method.invoke(target, args);

                advice.after();
                return null;
            }
        });

        //调用动态代理对象的方法
        proxy.save();


    }
}


//运行结果
前置增强!
我是原来的方法 
后置增强!

1.2 cglib的动态代理

我们会发现,Spring-core中含有cglib这个包,所以我们引入了Spring-context之后可以直接使用

他的原理就是:目标对象作为父类,然后代理对象是继承了他的一个子类,他的实例用目标对象的这个类来接受(多态)

代码如下:

//目标对象
package com.itheima.proxy.cglib;


public class Target {
    public void save() {
        System.out.println("我是原来的方法");
    }
}

//定义增强方法
package com.itheima.proxy.cglib;

public class Advice {

    public void before(){
        System.out.println("前置增强!");
    }

    public void after(){
        System.out.println("后置增强!");
    }
}

//代理的实现
package com.itheima.proxy.cglib;


import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class ProxyTest {

    public static void main(String[] args) {

        //创建目标对象
        final Target target = new Target();

        //获得增强对象
        final Advice advice = new Advice();

        //这是生成的动态代理的对象  基于cglib , 以后写框架可以掌握
        //1 需要创建增强器(cglib提供)
        Enhancer enhancer = new Enhancer();

        //2 设置父类(目标)
        enhancer.setSuperclass(Target.class);
        //3 设置回调
        enhancer.setCallback(new MethodInterceptor() {
            public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
                advice.before();
                method.invoke(target,args);
                advice.after();

                return null;
            }
        });
        //4 生成代理对象
        Target proxy =(Target) enhancer.create();
        //5 调用方法
        proxy.save();


    }
}

1.3 AOP相关概念

Spring的AOP实现底层就是对上面的动态代理的代码进行了封装,封装后我们只需要对需要关注的部分进行代码编写,并通过配置的方式完成指定目标的方法增强。

常见术语如下

  • Target(目标对象): 代理的目标对象
  • Proxy(代理): 一个类被AOP织入增强后,就产生一个结果代理类
  • Joinpoint(连接点): 所谓连接点是指那些被拦截到的点,在spring中,这些点指的是方法,因为spring只支持方法类型的连接点,可以被增强的方法叫做连接点
  • Pointcut(切入点): 所谓切入点就是我们要对哪些JoinPoint进行拦截的定义,切入点就是我们对连接点进行定义了之后形成的点
  • Advice(通知/增强): 所谓通知就是指拦截到 Joinpoint 之后所要做的事情就是通知
  • Aspect(切面): 是切入点(pointcut)和通知(advice)的结合
  • Weaving(织入): 是指把增强应用到目标对象来创建新的代理对象的过程,spring采用动态代理植入,而Aspect采用编译期织入类装载期织入

1.4 AOP开发的明确的事项

1 需要编写的内容

  • 编写核心业务代码(目标类和方法)
  • 编写切面类,切面类中有通知(增强功能的方法)
  • 在配置文件中,配置织入关系, 即将哪些通知与哪些连接点进行结合

2 AOP 技术实现的内容

Spring框架监控切入点方法的执行,一旦监控到切入点的方法被运行,使用代理机制,动态创建目标对象的代理对象,根据通知类别,在代理对象的对应位置,将通知对象的功能织入,完成完整的代码逻辑运行

3 AOP底层使用哪种代理方式

在spring中,框架会根据目标类是否实现了接口来决定采用哪种动态代理的方式

第二章 基于 XML 的 AOP 开发

2.1 快速入门

步骤如下

  1. 导入 AOP 相关坐标
  2. 创建目标接口和目标类
  3. 创建切面类(MyAspect)
  4. 将目标类和切面类的对象创建权交给 spring
  5. 在applicationContext.xml中配置织入关系
  6. 测试代码

代码如下

//目标对象接口
package com.itheima.aop;

public interface TargetInterface {

    void save();
}
//目标对象的类
package com.itheima.aop;

public class Target implements TargetInterface {
    public void save() {
        System.out.println("我是原来的方法");
    }
}

//我们定义的切面类
package com.itheima.aop;

public class MyAspect {
    public void before(){
        System.out.println("我是前置增强方法");
    }
}

//测试类
package com.itheima.test;

import com.itheima.aop.TargetInterface;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {

//    需要什么,就注入什么,这里用他的`接口来接收
    @Autowired
    private TargetInterface target;

    @Test
    public void test01(){
        target.save();
        //结果就是
        //我是前置增强方法
        //我是原来的方法
    }
}

配置文件

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
                            ">
    <!--配置目标对象-->
    <bean id="target" class="com.itheima.aop.Target"/>

    <!--切面对象-->
    <bean id="myAspect" class="com.itheima.aop.MyAspect"/>



    <!--配置织入,告诉spring哪些方法需要增强-->
    <aop:config>
        <!--声明切面-->
        <aop:aspect ref="myAspect">
            <!--切面 = 切点+ 通知-->
            <aop:before method="before" pointcut="execution(public void com.itheima.aop.Target.save())" />
        </aop:aspect>
    </aop:config>


</beans>

pom.xml中导入的东西

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>itheima_spring_aop</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.15.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.15.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

2.2 AOP切点表达式的写法

表达式语法

execution([修饰符] 返回值类型 包名.类名.方法名(参数))
  • 访问修饰符可以省略,
  • 返回值的作用是: 限定这个方法的返回值类型必须是符合这个修饰符的
  • 返回值类型,包名,类名,方法名可以使用星号 代表任意*
  • 包名与类名之间的一个点,代表当前包下的类,两个点…代表当前包及其子包下的类
  • 参数列表之间可以有两个点…表示任意个数,任意类型的参数列表

举例

//指定所有的包的所有返回值类型为void的方法
execution( void *..*.*(**))
 
//寻找com.itheima.aop包及其子包下的所有的类的所有的方法,参数不限,返回值类型不限
execution(* com.itheima.aop..*.*(..))

2.3 通知的类型

通知的配置语法:

<aop:通知类型 method="切面类中方法名" pointcut="切点表达式"></aop:通知类型>

有如下这些类型

请添加图片描述

演示

//切面类
package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyAspect {
    public void before(){
        System.out.println("我是前置增强方法");
    }

    public void after_returning(){
        System.out.println("我是后置方法");
    }

    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("我是环绕方法-前");
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("我是环绕方法-后");

        return proceed;

    }

    public void throwing(){
        System.out.println("我是报错方法");
    }

    public void after(){
        System.out.println("我是最终方法");
    }
}

//Target类和前面例子一样

//测试类也和前面一样

//运行结果
我是前置增强方法
我是环绕方法-前
我是原来的方法
我是环绕方法-后
我是后置方法

注意就是around环绕通知的写法比较特殊

2.4 切点表达式的抽取

当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用 point-ref属性代替pointcut属性来引用抽取后的切点表达式

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
                            ">
    <!--配置目标对象-->
    <bean id="target" class="com.itheima.aop.Target"/>

    <!--切面对象-->
    <bean id="myAspect" class="com.itheima.aop.MyAspect"/>


    <!--配置织入,告诉spring哪些方法需要增强-->
    <aop:config>
        <!--声明切面-->
        <aop:aspect ref="myAspect">
            <!--            切点表达式抽取-->
            <aop:pointcut id="myPointcut" expression="execution(public void com.itheima.aop.Target.save())"/>

            <!--切面 = 切点+ 通知-->
            <aop:before method="before" pointcut-ref="myPointcut"/>
            <aop:after-returning method="after_returning" pointcut-ref="myPointcut"/>
            <aop:around method="around" pointcut-ref="myPointcut"/>
        </aop:aspect>
    </aop:config>


</beans>

第三章 基于注解的 AOP 开发

3.1 快速入门

基于注解的aop开发步骤

  • 创建目标接口和目标类
  • 创建切面类
  • 将上面的类的创建权交给spring,就是添加bean
  • 在切面类中使用注解配置织入关系
  • 在配置文件中开启组件扫描和AOP的自动代理
  • 测试
//目标类
package com.itheima.anno;


import org.springframework.stereotype.Component;

@Component("target")
public class Target implements TargetInterface {
    public void save() {
        System.out.println("我是原来的方法");
    }
}

//切面类
package com.itheima.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;


@Component("myAspect")
@Aspect
public class MyAspect {

    @Before("execution(public void com.itheima.anno.Target.save())")
    public void before(){
        System.out.println("我是前置增强方法");
    }
   
}


//测试类
package com.itheima.test;

import com.itheima.anno.TargetInterface;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-anno.xml")
public class AopTest {

//    需要什么,就注入什么
    @Autowired
    private TargetInterface target;

    @Test
    public void test01(){
        target.save();
    }
}

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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                            ">
    <!--    引入Context命名空间-->
    <context:component-scan base-package="com.itheima.anno"/>

    <!--    自动代理aop-->
    <aop:aspectj-autoproxy/>

</beans>

3.2 注解通知的类型&切点表达式的抽取

通知的配置语法: @通知注解("切点表达式")

有下列几种类型的注解

请添加图片描述

那么我们如何用注解进行切点表达式的抽取呢?

  • 我们在切面内定义方法,在该方法(这个方法里面什么都不用写,就是一个宿主)的上面添加@Pointcut注解定义切点表达式,然后在增强注解中进行引用

代码如下

package com.itheima.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


@Component("myAspect")
@Aspect
public class MyAspect {

    @Before("MyAspect.pointcut()")
    public void before(){
        System.out.println("我是前置增强方法");
    }

    @Pointcut("execution(public void com.itheima.anno.Target.save())")
    public void pointcut(){}
}

  • 我们在切面内定义方法,在该方法(这个方法里面什么都不用写,就是一个宿主)的上面添加@Pointcut注解定义切点表达式,然后在增强注解中进行引用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值