MyInvocationHander

Time: 2024-05-15 Wednesday 23:20:01
Author: Jackasher

MyInvocationHander

这是JDK代理的切面操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package org.example.sercice;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
* @author Jackasher
* @version 1.0
* @className MyinvokationHander
* @since 1.0
**/
public class MyinvokationHander implements InvocationHandler {

private OrderService orderService;

public MyinvokationHander(OrderService orderService) {
this.orderService = orderService;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

long begin = System.currentTimeMillis();
Object value = method.invoke(orderService, args);
long end = System.currentTimeMillis();
System.out.println("该方法用时:" + (end - begin) + "ms");

return value;
}
}

测试调用

1
2
3
4
5
6
7
8
9
10
11
12
@Test
public void ProxyTest() throws InterruptedException {
OrderServiceImpl target = new OrderServiceImpl();

MyinvokationHander myinvokationHander = new MyinvokationHander(target);
OrderService orderService = (OrderService) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), myinvokationHander);

orderService.detail();
orderService.modify();
orderService.generate();

}

MyInvocationHander
http://example.com/2024/05/15/MyInvocationHander/
作者
Jack Asher
发布于
2024年5月15日
许可协议