Spring5框架 AOP

什么是AOP

在软件行业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一统领的一种技术。AOP是OOP的延续

(1)面向切面(方面)编程,利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

(2)通俗描述: 不通过修改源代码方式,在主干功能里面添加新功能。

(3)使用登录例子说明AOP

AOP(底层原理)

1、AOP底层使用动态代理

第一种 有接口情况,增强JDK动态代理

创建接口实现类代理对象,增强类的方法

动态代理有接口

第二种没有接口情况,使用CGLIB动态代理

动态代理没有接口情况

 AOP(JDK动态代理)

1、使用JDK动态代理,使用Proxy类里面的方法创建代理对象

java.lang.reflect

Class Proxy

java.lang.Object

        java.lang.reflect.Proxy

(1)调用newProxyInstance方法

static Object                 newProxyInstance(ClassLoader loader,类<?>[] interfaces,InvocationHandler h)        //返回指定接口的代理类的实例,该接口将方法调用分派给指定的调用处理程序。

方法有三个参数

第一个参数,类加载器

第二个参数,增强方法所在的类,这个类实现的接口,支持多个接口

第三个参数,实现这个接口InvocationHandler,创建代理对象,写增强的方法

2、编写JDK动态代理方法

(1)创建接口,定义方法

public interface UserDao {
    public int add(int a,int b);
    public String update(String id);
}

(2)创建接口实现类,定义方法

package com.atguigu.spring5;

/**
 * @ClassName UserDaoImpl
 * @Description
 * @Author 小黄debug
 * @Date 2022/4/18 20:22
 * @Version 1.0
 **/
public class UserDaoImpl implements UserDao{
    @Override
    public int add(int a,int b){
        return a+b;
    }
    @Override
    public String update(String id) {
        return id;
    }
}

(3)使用Proxy类创建接口代理对象

package com.atguigu.spring5;

public interface UserDao {
    public int add(int a,int b);
    public String update(String id);
}
package com.atguigu.spring5;

/**
 * @ClassName UserDaoImpl
 * @Description
 * @Author 小黄debug
 * @Date 2022/4/18 20:22
 * @Version 1.0
 **/
public class UserDaoImpl implements UserDao{
    @Override
    public int add(int a,int b){
        return a+b;
    }
    @Override
    public String update(String id) {
        return id;
    }
}
package com.atguigu.spring5;

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

/**
 * @ClassName JDKProxy
 * @Description
 * @Author 小黄debug
 * @Date 2022/4/18 20:27
 * @Version 1.0
 **/
public class JDKProxy {
    public static void main(String[] args) {
        //创建接口实现类代理对象
        Class[] interfaces = {UserDao.class};
//        Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new InvocationHandler() {
//            @Override
//            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//                return null;
//            }
//        });
        UserDaoImpl userDao = new UserDaoImpl();
        UserDao dao = (UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new UserDaoProxy(userDao));

        int result = dao.add(1, 2);
        System.out.println(result);


    }
}
//创建代理对象代码
class UserDaoProxy implements InvocationHandler{

    //1、创建的是谁的代理对象,把谁传递过来
    //有参数构造传递
    private Object obj;
    public UserDaoProxy(Object obj){
        this.obj = obj;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //方法之前
        System.out.println("方法之前执行。。。"+method.getName()+":传递的参数。。。"+ Arrays.toString(args));

        
        
        //被增强的方法执行
        Object invoke = method.invoke(obj, args);
        //方法之后
        System.out.println("方法之后执行。。。"+obj);

        return invoke;
    }
}

AOP术语

1、连接点

类里面哪些方法可以被增强,这些方法称为连接点

2、切入点

实际被真正增强的方法,称为切入点

3、通知(增强)

(1)实际增强的逻辑部分称为通知

(2)通知有多种类型

五种通知:

前置通知

后置通知

环绕通知

异常通知

最终通知

4、切面

是动作

(1)把通知应用到切入点过程

AOP操作(准备)

1、Spring框架是一般都是基于AspectJ实现AOP操作

(1)什么是AspectJ

AspectJ不是Spring组成部分,独立AOP框架,一般把AspectJ和Spring框架一起使用,进行AOP操作

2、基于AspectJ实现AOP操作

(1)基于xml配置文件实现

(2)基于注解方式实现(使用)

3、在项目工程里面引入AOP相关依赖

4、切入点表达式

(1)切入点表达式作用:知道对哪个类的哪个方法进行增强

(2)语法结构

execution([权限修饰符] [返回类型] [类全路径] [方法名称] [参数列表])

举例1:对com.atguigu.dao.BookDao类里面的add进行增强

execution(* com.atguigu.dao.BookDao.add(..))

举例2:对com.atguigu.dao.BookDao类里面的所有的方法进行增强

execution(* com.atguigu.dao.BookDao.*(..))

举例3:对com.atguigu.dao包里所有类,类里面所有方法进行增强

execution(* com.atguigul.dao.*.*(..))

AOP操作(AspectJ注解)

1、创建类,在类里面定义方法

package com.atguigu.spring5.aopanno;

/**
 * @ClassName User
 * @Description
 * @Author 小黄debug
 * @Date 2022/4/23 10:28
 * @Version 1.0
 **/
public class User {
    public void add(){
        System.out.println("add........");
    }
}

2、创建增强类(编写增强的逻辑)

(1)在增强类里面,创建方法,让不同方法代表不同的通知类型

package com.atguigu.spring5.aopanno;

/**
 * @ClassName UserProxy
 * @Description
 * @Author 小黄debug
 * @Date 2022/4/23 10:29
 * @Version 1.0
 **/
//增强的类
public class UserProxy {
    public void before(){
        System.out.println("before.....");
    }
    public void after(){
        System.out.println("after....");
    }
}

3、进行通知配置

(1)在Spring配置文件中,开启注解扫描

<?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: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 http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解扫描-->
    <context:component-scan base-package="com.atguigu.spring5.aopanno">

    </context:component-scan>

<!--    开启Aspect生成代理对象-->
    <aop:aspectj-autoproxy>

    </aop:aspectj-autoproxy>

</beans>

(2)使用注解创建User和UserProxy对象

package com.atguigu.spring5.aopanno;

import org.springframework.stereotype.Component;

/**
 * @ClassName User
 * @Description
 * @Author 小黄debug
 * @Date 2022/4/23 10:28
 * @Version 1.0
 **/
@Component
public class User {
    public void add(){
        System.out.println("add........");
    }
}

(3)在增强的类上面添加注解@Aspect

package com.atguigu.spring5.aopanno;

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

/**
 * @ClassName UserProxy
 * @Description
 * @Author 小黄debug
 * @Date 2022/4/23 10:29
 * @Version 1.0
 **/
//增强的类
    @Component
    @Aspect
public class UserProxy {
        //前置通用
    //@Before注解表示作为前置通知
    @Before(value="execution(* com.atguigu.spring5.aopanno.User.add(..))")
    public void before(){
        System.out.println("before.....");
    }
    public void after(){
        System.out.println("after....");
    }
}

(4)在spring配置文件中开户生成代理对象

4、配置不同类型的通知

(1)在增强类的里面作为通知方法上面添加通知类型注解,使用切入点表达式配置

//测试

package com.atguigu.spring5.test;

import com.atguigu.spring5.aopanno.User;
import javafx.application.Application;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @ClassName TestAop
 * @Description
 * @Author 小黄debug
 * @Date 2022/4/23 11:38
 * @Version 1.0
 **/
public class TestAop {
    @Test
    public void testAnno() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");

        User user = context.getBean("user", User.class);
        user.add();
        //before.....
        //add........
    }
}

AOP操作(AspectJ配置文件)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值