Skip to content

Commit 378be56

Browse files
basics code for cz
1 parent 5f3ae3d commit 378be56

15 files changed

Lines changed: 248 additions & 20 deletions

basics-code.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4" />

pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77
<groupId>com.cz</groupId>
88
<artifactId>basics.code</artifactId>
99
<version>1.0-SNAPSHOT</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>8</source>
17+
<target>8</target>
18+
</configuration>
19+
</plugin>
20+
</plugins>
21+
</build>
1022

1123
<dependencies>
1224
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
@@ -23,6 +35,15 @@
2335
<version>1.5.11.RELEASE</version>
2436
</dependency>
2537

38+
<!--添加动态代理包-->
39+
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
40+
<dependency>
41+
<groupId>cglib</groupId>
42+
<artifactId>cglib</artifactId>
43+
<version>2.1_3</version>
44+
</dependency>
45+
46+
2647

2748
</dependencies>
2849
</project>

src/main/java/com/basics/UserServiceImpl.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/main/java/com/basics/agency/UserService.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,13 @@
99
*
1010
*/
1111
public interface UserService {
12-
public abstract void add();
12+
void add();
13+
14+
void sub();
15+
16+
void hello(String zlx);
17+
18+
void service(String zlx);
19+
20+
1321
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.basics.agency;
2+
3+
4+
/**
5+
* @PACKAGE_NAME: com.basics
6+
* @PROJECT_NAME: JavaCode
7+
* @Date: 2018/4/9
8+
* @author: chenzhuo
9+
*/
10+
public class UserServiceImpl implements UserService {
11+
public void add() {
12+
System.err.println("-----------add -------------- ");
13+
}
14+
15+
public void sub() {
16+
System.err.println("-----------sub -------------- ");
17+
}
18+
19+
public void hello(String hello) {
20+
System.err.println("-----------hello -------------- " + hello);
21+
}
22+
23+
public void service(String service) {
24+
System.err.println("-----------service -------------- " + service);
25+
}
26+
}

src/main/java/com/basics/agency/main/AgencyMain.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package com.basics.agency.main;
22

3-
import com.basics.UserServiceImpl;
3+
import com.basics.agency.UserServiceImpl;
44
import com.basics.agency.MyInvocationHandler;
55
import com.basics.agency.UserService;
66

7-
import java.lang.reflect.Field;
8-
import java.util.Properties;
9-
107
/**
118
* @PACKAGE_NAME: com.basics.agency.main
129
* @PROJECT_NAME: JavaCode
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @PACKAGE_NAME: com.basics.agency
3+
* @PROJECT_NAME: JavaCode
4+
* @Date: 2018/4/9
5+
* @author: chenzhuo
6+
* jdk 静态代理
7+
*/
8+
package com.basics.agency;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.basics.dynamicAgecy;
2+
3+
import com.basics.agency.PerformanceMonior;
4+
import net.sf.cglib.proxy.Callback;
5+
import net.sf.cglib.proxy.Enhancer;
6+
import net.sf.cglib.proxy.MethodInterceptor;
7+
import net.sf.cglib.proxy.MethodProxy;
8+
9+
import java.lang.reflect.Method;
10+
11+
/**
12+
* @PACKAGE_NAME: com.basics.dynamicAgecy
13+
* @PROJECT_NAME: JavaCode
14+
* @Date: 2018/4/9
15+
* @author: chenzhuo
16+
*/
17+
public class CglibProxy implements MethodInterceptor {
18+
19+
private Enhancer enhancer = new Enhancer();
20+
21+
public Object getProxy(Class clazz) {
22+
enhancer.setSuperclass(clazz);
23+
enhancer.setCallback(this);
24+
return enhancer.create();
25+
}
26+
27+
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
28+
PerformanceMonior.begin(o.getClass().getName()+"."+method.getName());
29+
Object result = methodProxy.invokeSuper(o, objects);
30+
PerformanceMonior.end();
31+
32+
Object proxyObject = getProxy(this.getClass());
33+
return result;
34+
}
35+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.basics.dynamicAgecy.main;
2+
3+
import com.basics.agency.UserService;
4+
import com.basics.agency.UserServiceImpl;
5+
import com.basics.dynamicAgecy.CglibProxy;
6+
import net.sf.cglib.core.DebuggingClassWriter;
7+
8+
/**
9+
* @PACKAGE_NAME: com.basics.dynamicAgecy.main
10+
* @PROJECT_NAME: JavaCode
11+
* @Date: 2018/4/9
12+
* @author: chenzhuo
13+
*/
14+
public class CglibProxyMain {
15+
public static void main(String []args){
16+
//生成代理类到本地
17+
System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY,
18+
"D:\\basicsCode\\JavaCode\\src\\main\\java\\com\\sun\\proxy\\");
19+
UserServiceImpl service = new UserServiceImpl();
20+
CglibProxy cp = new CglibProxy();
21+
UserService proxy = (UserService) cp.getProxy(service.getClass());
22+
proxy.add();
23+
proxy.sub();
24+
proxy.hello("hello");
25+
proxy.service("hello service");
26+
proxy.toString();
27+
}
28+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @PACKAGE_NAME: com.basics.dynamicAgecy
3+
* @PROJECT_NAME: JavaCode
4+
* @Date: 2018/4/9
5+
* @author: chenzhuo
6+
* clib 动态代理
7+
*/
8+
package com.basics.dynamicAgecy;

0 commit comments

Comments
 (0)