代理模式(Proxy)是通过代理对象访问目标对象,这样可以在目标对象基础上增强额外的功能,如添加权限,访问控制和审计等功能。
1.自定义业务接口
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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.自定义业务接口实现类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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.日志接口和实现类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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.测试类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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(); } } |
结果输出:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_40604313/article/details/119713796