cglib代理
package com.qst.spring.dyanmicProxy.cglib;
public class MyAspect {
public void check(){
System.out.println("check user.......");
}
public void log(){
System.out.println("logging。。。。。。");
}
}
package com.qst.spring.dyanmicProxy.cglib;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class MyProxy<T> implements MethodInterceptor {
private T byProxy;
private MyAspect myAspect;
public MyProxy(T byProxy, MyAspect myAspect) {
this.byProxy = byProxy;
this.myAspect = myAspect;
}
public T create(){
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(byProxy.getClass());
enhancer.setCallback(this);
T proxy = (T)enhancer.create();
return proxy;
}
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
myAspect.check();
method.invoke(byProxy,objects);
myAspect.log();
return null;
}
}
package com.qst.spring.dyanmicProxy.cglib;
public class UserDaoImpl {
public void save(String name) {
System.out.println("save user......"+name);
}
public void delete() {
System.out.println("delete user.......");
}
}
package com.qst.spring.dyanmicProxy.cglib;
public class UserService {
public void save(){
System.out.println("save ...");
}
}
package com.qst.spring.dyanmicProxy.cglib;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
public class Test {
public static void main(String[] args) {
//被代理对象
UserDaoImpl userDaoImpl =new UserDaoImpl();
UserService userService = new UserService();
MyAspect myAspect = new MyAspect();
MyProxy myProxy = new MyProxy(userService,myAspect);
UserService service = (UserService) myProxy.create();
service.save();
}
}
jdk动态代理
package com.qst.spring.dyanmicProxy.jdk;
public class MyAspect {
public void check(){
System.out.println("check user.......");
}
public void log(){
System.out.println("logging。。。。。。");
}
}
package com.qst.spring.dyanmicProxy.jdk;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class MyInvocationHandler implements InvocationHandler {
private UserDao userDao;
private MyAspect myAspect;
public MyInvocationHandler(UserDao userDao,MyAspect myAspect) {
this.userDao = userDao;
this.myAspect= myAspect;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
myAspect.check();
method.invoke(userDao,args);
myAspect.log();
return null;
}
}
package com.qst.spring.dyanmicProxy.jdk;
public interface UserDao {
public void save(String name);
public void delete(String name);
}
package com.qst.spring.dyanmicProxy.jdk;
import java.sql.SQLOutput;
public class UserDaoImpl implements UserDao{
@Override
public void save(String name) {
System.out.println("save user......"+name);
}
@Override
public void delete(String name) {
System.out.println("delete user......."+name);
}
}
package com.qst.spring.dyanmicProxy.jdk;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
public class Test {
public static void main(String[] args) {
UserDao userDao = new UserDaoImpl();
MyAspect myAspect = new MyAspect();
ClassLoader classLoader = userDao.getClass().getClassLoader();
Class<?>[] interfaces =userDao.getClass().getInterfaces();
InvocationHandler handler = new MyInvocationHandler(userDao,myAspect);
UserDao proxy = (UserDao) Proxy.newProxyInstance(classLoader,interfaces,handler);
proxy.save("lp");
proxy.delete("king");
}
}