Day22SSM之Spring AOP***

Spring AOP概念

  • (1)AOP(Aspect Oriented Programming)是面向切面编程
    就是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
    简单说 就是在不改变方法原代码的基础上,对方法进行功能增强
    本质上是生成了一个新的类,叫做代理类
  • (2)AOP对程序的扩展方式采用动态代理的方式. (JDK动态代理Cglib动态代理两种方式)
    在这里插入图片描述
    在这里插入图片描述

Spring 动态代理

  • (1)JDK的动态代理
    》Proxy类的方法
    Proxy类的静态方法可以创建代理对象
    static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
    》三个参数
    参数1:ClassLoader loader 类加载器 , 用来加载代理对象
    参数2:Class<?>[] interfaces 目标类的字节码对象数组. 因为代理的是接口,需要知道接口中所有的方法
    参数3:InvocationHandler h 执行句柄, 代理对象处理的核心逻辑就在该接口中
  • (2)案例:老总吃饭
    老总类
    秘书类

TestJdkProxy


public class TestJdkProxy {
    public static void main(String[] args) {
        //Jdk代理
        //ILaoZong
        LaoZong laoZong = new LaoZong();
        MiShu miShu = new MiShu();
        //1 创建一个代理类,创建该类的对象
        ClassLoader classLoader = LaoZong.class.getClassLoader();
        Class[] interfaces= new Class[]{ILaoZong.class};
        //处理器
        InvocationHandler handler = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //调 laibeijiu()
                miShu.laiBeiJiu();
                //调 eat()
                //laoZong.eat();
                //method 被增加的方法
                Object returnValue = method.invoke(laoZong,args);
                //调 laiGenYan()
                miShu.laiGenYan();
                return returnValue;
            }
        };
        ILaoZong iLaoZong = (ILaoZong) Proxy.newProxyInstance(classLoader,interfaces,handler);
        iLaoZong.eat();
    }
}

LaoZong

public class LaoZong implements ILaoZong{
    public void eat(){
        System.out.println("eat san xia guo");
        System.out.println("eat wa wa cai");
    }
}

ILaoZong

public interface ILaoZong {
    void eat();
}

MiShu

public class MiShu {
    public void laiBeiJiu(){
        System.out.println("laiBeiJiu");
    }
    public void laiGenYan(){
        System.out.println("laiGenYan");
    }
}

案例:日志系统

给一个类的所有方法加log
在这里插入图片描述

Demo02


public class Demo02 {
    public static void main(String[] args) {
        Person p = new Person("jack","123456");
        //生成代理类,创建该类对象
        PersonDao2 personDao2 = new PersonDao2();
        Logger logger = LoggerFactory.getLogger(PersonDao2.class);
        ClassLoader classLoader=PersonDao2.class.getClassLoader();//与原来类一样
        Class<?>[] interfaces=PersonDao2.class.getInterfaces();//与原来类一样
        InvocationHandler handler = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//                调用Dao类的update(person)
//                调用Logger类的debug(“update  parameter return”)
                //update delete add 的执行
                //开始
                long start = System.currentTimeMillis();
                Object returnVal = method.invoke(personDao2,args);
                long time = System.currentTimeMillis()-start;
                logger.debug("方法名:"+method.getName()+" 参数"+ Arrays.toString(args)+" 返回值:"+returnVal+" 耗时"+time);
                return returnVal;
            }
        };
        IPersonDao2 personDao= (IPersonDao2) Proxy.newProxyInstance(classLoader,interfaces,handler);
 //       personDao.add(p);
        personDao.update(p);
//        personDao.delete(1);
    }
}

PersonDao2

public class PersonDao2 implements IPersonDao2{

    public void add(Person p){
        System.out.println("执行 数据库的insert");
    }
    public void update(Person p){
        System.out.println("执行 数据库的update");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void delete(int id){
        System.out.println("执行 数据库的delete");
    }
}

IPersonDao2

public interface IPersonDao2 {
     void add(Person p);
     void update(Person p);
     void delete(int id);
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

翁老师的教学团队

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值