-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
taste jdk dynamic proxy and cglib proxy
- Loading branch information
Liuxey
committed
Oct 27, 2015
1 parent
7ad6ada
commit edca053
Showing
10 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package proxy.cglib; | ||
|
||
import net.sf.cglib.proxy.MethodInterceptor; | ||
import net.sf.cglib.proxy.MethodProxy; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* Created by Liuxey on 2015/10/27. | ||
*/ | ||
public class LogProxy implements MethodInterceptor{ | ||
|
||
private Object target; | ||
|
||
public LogProxy(Object target) { | ||
this.target = target; | ||
} | ||
|
||
@Override | ||
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { | ||
Object result = null; | ||
System.out.println("++++++++++++++++++++++++++Invoke start: " + method.getName()); | ||
result = method.invoke(target, objects); | ||
System.out.println("++++++++++++++++++++++++++END"); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package proxy.cglib; | ||
|
||
import net.sf.cglib.proxy.Enhancer; | ||
import net.sf.cglib.proxy.MethodInterceptor; | ||
|
||
/** | ||
* Created by Liuxey on 2015/10/27. | ||
*/ | ||
public class ProxyFactory { | ||
|
||
public static <T> T newProxy(T t, MethodInterceptor proxy) { | ||
Enhancer enhancer = new Enhancer(); | ||
enhancer.setSuperclass(t.getClass()); | ||
enhancer.setCallback(proxy); | ||
return (T) enhancer.create(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package proxy.cglib; | ||
|
||
import net.sf.cglib.proxy.MethodInterceptor; | ||
|
||
/** | ||
* Created by Liuxey on 2015/10/27. | ||
*/ | ||
public class TestProxy { | ||
|
||
public static void main(String[] args) { | ||
UserService userService = new UserService(); | ||
MethodInterceptor handler = new LogProxy(userService); | ||
UserService proxy = ProxyFactory.newProxy(userService, handler); | ||
proxy.add("jack"); | ||
proxy.delete("jack"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package proxy.cglib; | ||
|
||
/** | ||
* Created by Liuxey on 2015/10/27. | ||
*/ | ||
public class UserService { | ||
|
||
public void add(String username) { | ||
System.out.println("User:" + username + " added."); | ||
} | ||
|
||
public void delete(String username) { | ||
System.out.println("User:" + username + " deleted."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package proxy.dynamic; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* Created by Liuxey on 2015/10/27. | ||
*/ | ||
public class LogProxy implements InvocationHandler { | ||
|
||
private Object target; | ||
|
||
public LogProxy(Object target) { | ||
this.target = target; | ||
} | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
Object result = null; | ||
System.out.println("++++++++++++++++++++++++++Invoke start: " + method.getName()); | ||
result = method.invoke(target, args); | ||
System.out.println("++++++++++++++++++++++++++END"); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package proxy.dynamic; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Proxy; | ||
|
||
/** | ||
* Created by Liuxey on 2015/10/27. | ||
*/ | ||
public class ProxyFactory { | ||
|
||
public static <T> T newProxy(T t, InvocationHandler proxy) { | ||
return (T) Proxy.newProxyInstance(t.getClass().getClassLoader(), t.getClass().getInterfaces(), proxy); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package proxy.dynamic; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
|
||
/** | ||
* Created by Liuxey on 2015/10/27. | ||
*/ | ||
public class TestProxy { | ||
|
||
public static void main(String[] args) { | ||
UserServiceImpl target = new UserServiceImpl(); | ||
InvocationHandler proxy = new LogProxy(target); | ||
UserService userService = ProxyFactory.newProxy(target, proxy); | ||
userService.add("jack"); | ||
userService.delete("jack"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package proxy.dynamic; | ||
|
||
/** | ||
* Created by Liuxey on 2015/10/27. | ||
*/ | ||
public interface UserService { | ||
|
||
public void add(String username); | ||
|
||
public void delete(String username); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package proxy.dynamic; | ||
|
||
/** | ||
* Created by Liuxey on 2015/10/27. | ||
*/ | ||
public class UserServiceImpl implements UserService { | ||
|
||
@Override | ||
public void add(String username) { | ||
System.out.println("User:" + username + " added."); | ||
} | ||
|
||
@Override | ||
public void delete(String username) { | ||
System.out.println("User:" + username + " deleted."); | ||
} | ||
} |