File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <project xmlns =" http://maven.apache.org/POM/4.0.0"
3+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
4+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
5+ <modelVersion >4.0.0</modelVersion >
6+
7+ <groupId >com.cz</groupId >
8+ <artifactId >basics.code</artifactId >
9+ <version >1.0-SNAPSHOT</version >
10+
11+ <dependencies >
12+ <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
13+ <dependency >
14+ <groupId >org.springframework</groupId >
15+ <artifactId >spring-aop</artifactId >
16+ <version >4.3.8.RELEASE</version >
17+ </dependency >
18+
19+ <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop -->
20+ <dependency >
21+ <groupId >org.springframework.boot</groupId >
22+ <artifactId >spring-boot-starter-aop</artifactId >
23+ <version >1.5.11.RELEASE</version >
24+ </dependency >
25+
26+
27+ </dependencies >
28+ </project >
Original file line number Diff line number Diff line change 1+ package com .basics ;
2+
3+ import com .basics .agency .UserService ;
4+
5+ /**
6+ * @PACKAGE_NAME: com.basics
7+ * @PROJECT_NAME: JavaCode
8+ * @Date: 2018/4/9
9+ * @author: chenzhuo
10+ */
11+ public class UserServiceImpl implements UserService {
12+ public void add () {
13+ System .err .println ("-----------add -------------- " );
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package com .basics .agency ;
2+
3+ /**
4+ * @PACKAGE_NAME: com.basics
5+ * @PROJECT_NAME: JavaCode
6+ * @Date: 2018/4/9
7+ * @author: chenzhuo
8+ */
9+ public class MethodPerformace {
10+ private long begin ;
11+ private long end ;
12+ private String serviceMethod ;
13+
14+ public MethodPerformace (String serviceMethod ) {
15+ reset (serviceMethod );
16+ }
17+
18+ public void printPerformace () {
19+ end = System .currentTimeMillis ();
20+ long elapse = end - begin ;
21+ System .out .println (serviceMethod + "花费" + elapse + "毫秒。" );
22+ }
23+
24+ public void reset (String serviceMethod ) {
25+ this .serviceMethod = serviceMethod ;
26+ this .begin = System .currentTimeMillis ();
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ package com .basics .agency ;
2+
3+ import java .lang .reflect .InvocationHandler ;
4+ import java .lang .reflect .Method ;
5+ import java .lang .reflect .Proxy ;
6+
7+ /**
8+ * @PACKAGE_NAME: com.basics
9+ * @PROJECT_NAME: JavaCode
10+ * @Date: 2018/4/9
11+ * @author: chenzhuo
12+ * 代理类 jdk 静态代理
13+ */
14+ public class MyInvocationHandler implements InvocationHandler {
15+ private Object target ;
16+ public MyInvocationHandler (Object target ) {
17+ super ();
18+ this .target = target ;
19+ }
20+
21+
22+ public Object invoke (Object proxy , Method method , Object [] args ) throws Throwable {
23+ PerformanceMonior .begin (target .getClass ().getName ()+"." +method .getName ());
24+ System .out .println ("-----------------begin " +method .getName ()+"-----------------" );
25+ /* Object result = method.invoke(target, args);
26+ System.err.println("result Length:"
27+ + result.getClass().getMethods().length);*/
28+ System .out .println ("-----------------end " +method .getName ()+"-----------------" );
29+ PerformanceMonior .end ();
30+ return null ;
31+ }
32+
33+ /**
34+ * 返回新的代理
35+ * @return
36+ */
37+ public Object getProxy (){
38+ return Proxy .newProxyInstance (Thread .currentThread ().getContextClassLoader (),
39+ target .getClass ().getInterfaces (),
40+ this );
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ package com .basics .agency ;
2+
3+ /**
4+ * @PACKAGE_NAME: com.basics
5+ * @PROJECT_NAME: JavaCode
6+ * @Date: 2018/4/9
7+ * @author: chenzhuo
8+ * 拦截明细
9+ */
10+ public class PerformanceMonior {
11+ private static ThreadLocal <MethodPerformace > performaceRecord = new ThreadLocal ();
12+ public static void begin (String method ) {
13+ System .out .println ("begin monitor..." );
14+ MethodPerformace mp = performaceRecord .get ();
15+ if (mp == null ) {
16+ mp = new MethodPerformace (method );
17+ performaceRecord .set (mp );
18+ } else {
19+ mp .reset (method );
20+ }
21+ }
22+ public static void end () {
23+ System .out .println ("end monitor..." );
24+ MethodPerformace mp = performaceRecord .get ();
25+ mp .printPerformace ();
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ package com .basics .agency ;
2+
3+ /**
4+ * @PACKAGE_NAME: com.basics
5+ * @PROJECT_NAME: JavaCode
6+ * @Date: 2018/4/9
7+ * @author: chenzhuo
8+ * jdk 代理
9+ *
10+ */
11+ public interface UserService {
12+ public abstract void add ();
13+ }
Original file line number Diff line number Diff line change 1+ package com .basics .agency .main ;
2+
3+ import com .basics .UserServiceImpl ;
4+ import com .basics .agency .MyInvocationHandler ;
5+ import com .basics .agency .UserService ;
6+
7+ import java .lang .reflect .Field ;
8+ import java .util .Properties ;
9+
10+ /**
11+ * @PACKAGE_NAME: com.basics.agency.main
12+ * @PROJECT_NAME: JavaCode
13+ * @Date: 2018/4/9
14+ * @author: chenzhuo
15+ * 主方法
16+ */
17+ public class AgencyMain {
18+
19+ public static void main (String []args ) throws NoSuchFieldException , IllegalAccessException {
20+ //生成的代理类保存到磁盘
21+ /* Field field = System.class.getDeclaredField("props");
22+ field.setAccessible(true);
23+ Properties props = (Properties) field.get(null);
24+ props.put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");*/
25+ System .getProperties ().put ("sun.misc.ProxyGenerator.saveGeneratedFiles" ,true );
26+
27+ UserService service = new UserServiceImpl ();
28+ MyInvocationHandler handler = new MyInvocationHandler (service );
29+ UserService proxy = (UserService ) handler .getProxy ();
30+ proxy .add ();
31+ }
32+
33+ }
Original file line number Diff line number Diff line change 1+ package com .sun .proxy ;
2+
3+ /**
4+ * @PACKAGE_NAME: com.sun.proxy
5+ * @PROJECT_NAME: JavaCode
6+ * @Date: 2018/4/9
7+ * @author: chenzhuo
8+ */
9+ public class Test {
10+ }
You can’t perform that action at this time.
0 commit comments