首页 > 编程语言 > Spring动态代理实现日志功能详解
2022
04-20

Spring动态代理实现日志功能详解

代理模式(Proxy)是通过代理对象访问目标对象,这样可以在目标对象基础上增强额外的功能,如添加权限,访问控制和审计等功能。

1.自定义业务接口

package com.haijiao12138.demo.spring.agentLog0815;
 
/**
 * @author: haijiao12138
 * @ClassName: BusinessClassService
 * @description: TODO
 * @date: 2021/8/15 14:33
 */
public interface BusinessClassService {
 
    public void doSomeThing();//自定义接口
 
}

2.自定义业务接口实现类

package com.haijiao12138.demo.spring.agentLog0815;
 
/**
 * @author: haijiao12138
 * @ClassName: BusinessClassServiceImpl
 * @description: 业务类
 * @date: 2021/8/15 14:34
 */
public class BusinessClassServiceImpl implements BusinessClassService {
 
    //执行某事
    @Override
    public void doSomeThing() {
        System.out.println("doing  somthing......");
    }
}

3.日志接口和实现类

package com.haijiao12138.demo.spring.agentLog0815;
 
import java.lang.reflect.Method;
 
/**
 * @author: haijiao12138
 * @ClassName: MyLogger
 * @description:日志类接口
 * @date: 2021/8/15 14:38
 */
public interface MyLogger {
 
    //记录进入方法时间
    public void saveIntoMethodTime(Method method);
 
    //记录退出方法的时间
    public void saveOutMethodTime(Method method);
}
package com.haijiao12138.demo.spring.agentLog0815;
 
import java.lang.reflect.Method;
import java.util.Date;
 
/**
 * @author: haijiao12138
 * @ClassName: MyLoggerImpl
 * @description: TODO  日志实现类
 * @date: 2021/8/15 14:40
 */
public class MyLoggerImpl  implements MyLogger{
    @Override
    public void saveIntoMethodTime(Method method) {
        System.out.println("进入" + method.getName()  +"方法时间为: " + new Date());
    }
 
    @Override
    public void saveOutMethodTime(Method method) {
        System.out.println("退出" + method.getName() + "方法时间为:" + new Date());
    }
}

4.下面是日志类的handler实现:

package com.haijiao12138.demo.spring.agentLog0815;
 
import java.lang.reflect.Method;
import java.util.Date;
 
/**
 * @author: haijiao12138
 * @ClassName: MyLoggerImpl
 * @description: TODO  日志实现类
 * @date: 2021/8/15 14:40
 */
public class MyLoggerImpl  implements MyLogger{
    @Override
    public void saveIntoMethodTime(Method method) {
        System.out.println("进入" + method.getName()  +"方法时间为: " + new Date());
    }
 
    @Override
    public void saveOutMethodTime(Method method) {
        System.out.println("退出" + method.getName() + "方法时间为:" + new Date());
    }
}

5.测试类

package com.haijiao12138.demo.spring.agentLog0815;
 
import java.lang.reflect.Proxy;
 
/**
 * @author: haijiao12138
 * @ClassName: MyLoggerTest
 * @description: TODO 日志测试类
 * @date: 2021/8/15 14:43
 */
public class MyLoggerTest {
    public static void main(String[] args) {
        /** 实例化真实项目中业务类 **/
        BusinessClassService businessClassService = new BusinessClassServiceImpl();
        /** 日志类的handler **/
        MyLoggerHandler myLoggerHandler = new MyLoggerHandler(businessClassService);
        /** 获得代理类对象 **/
        BusinessClassService businessClass = (BusinessClassService) Proxy.newProxyInstance(businessClassService.getClass().getClassLoader(), businessClassService.getClass().getInterfaces(),myLoggerHandler);
        /** 执行代理类方法 **/
        businessClass.doSomeThing();
 
    }
 
}

结果输出:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

编程技巧