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);
}

猜你喜欢

转载自blog.csdn.net/u013621398/article/details/108978237
今日推荐