jdk和CGLIB动态代理

jdk动态代理和CGLIB动态代理的区别:
    jdk动态代理只能针对实现了接口的目标代理, CGLIB是针对类来实现动态代理,当没有实现接口的类需要代理时,就需要通过CGLIB来实现动态代理。

jdk动态代理:
   User.java

public class User {


   private String uname;
   private String email;
   public String getUname() {
       return uname;
   }
   public void setUname(String uname) {
       this.uname = uname;
   }
   public String getEmail() {
       return email;
   }
   public void setEmail(String email) {
       this.email = email;
   }


}

 UserDao.java

public interface UserDao {

	@Test
	public void read(String uname,String email);
	
	@Test
	public void write(String uname,String email);
}

 UserDaoImpl.java

public class UserDaoImpl implements UserDao {

	@Override
	public void read(String uname, String email) {
		System.out.println("read " + uname + "'s Email is :"+email);
	}
	
	@Override
	public void write(String uname, String email) {
		System.out.println("write " + uname + "'s Email is :"+email);
	}

}

 UserInvocationHandler.java

public class UserInvocationHandler implements InvocationHandler {

	private Object obj = null;
	
	public UserInvocationHandler(Object obj){
		this.obj = obj;
	}
	
	private String getMethodName(Method method){
		return method.getName();
	}
	
	private Annotation[] getAnnotation(Method method){
		return method.getAnnotations();
	}
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		Object result = null;
		 
		System.out.println(proxy.getClass().getSimpleName()); 
		
		System.out.println("begin...");
		//获取方法名
		String methodName = getMethodName(method);
		System.out.println("方法名称:"+methodName);
		//获取方法上的注解
		 Annotation[] annotation = getAnnotation(method);
		 System.out.println("注解名称:"+annotation[0].toString());
				 
		result = method.invoke(obj, args);
		
		System.out.println("end...");
		
		return result;
	}

}

 
   UserTest.java

public class UserTest {

	public static void main(String[] args) {
		UserDao uDao = new UserDaoImpl();
		UserInvocationHandler invocationHandler = new UserInvocationHandler(uDao);
		UserDao proxy = (UserDao) Proxy.newProxyInstance(uDao.getClass().getClassLoader(), uDao.getClass().getInterfaces(), invocationHandler);
		proxy.write("xuwei", "[email protected]");
		proxy.read("xuwei", "[email protected]");
	}
}

 

 运行结果:

$Proxy0
begin...
方法名称:write
注解名称:@org.junit.Test(expected=class org.junit.Test$None, timeout=0)
write xuwei's Email is :[email protected]
end...
$Proxy0
begin...
方法名称:read
注解名称:@org.junit.Test(expected=class org.junit.Test$None, timeout=0)
read xuwei's Email is :[email protected]
end...

 

 

 

 

 

 

 

CGLIB动态代理:

 

 

    BookDao.java

public interface BookDao {

	public void create();
	
	public void query();
	
	public void update();
	
	public void delete();
	
}

 BookDaoImpl.java

public class BookDaoImpl implements BookDao{

	@Override
	public void create() {
		System.out.println("create() is running...");
		
	}

	@Override
	public void query() {
		System.out.println("query() is running...");
		
	}

	@Override
	public void update() {
		System.out.println("update() is running...");
		
	}

	@Override
	public void delete() {
		System.out.println("delete() is running...");
		
	}

}

 MyCglibProxy.java

public class MyCglibProxy implements MethodInterceptor {

	public Enhancer enhancer = new Enhancer();

	private String name;

	public MyCglibProxy(String name) {
		this.name = name;
	}

	

	@Override
	public Object intercept(Object object, Method method, Object[] args,
			MethodProxy methodProxy) throws Throwable {
		System.out.println("调用的方法是:" + method.getName());
		// 对用户进行判断
		if (!"boss".equals(name)) {
			System.out.println("你没有权限!");
			return null;
		}
		Object result = methodProxy.invokeSuper(object, args);
		return null;
	}

}

 

MyProxyFilter.java

public class MyProxyFilter implements CallbackFilter {

	@Override
	public int accept(Method arg0) {
		if (!"query".equalsIgnoreCase(arg0.getName()))
			return 0;
		return 1;

	}

}

 MyCglibProxyFactory.java

public class MyCglibProxyFactory {

	public static BookDaoImpl getProxyInstance(MyCglibProxy myProxy) {
		Enhancer en = new Enhancer();
		// 进行代理
		en.setSuperclass(BookDaoImpl.class);
		en.setCallback(myProxy);
		// 生成代理实例
		return (BookDaoImpl) en.create();
	}
	
	public static BookDaoImpl getAuthInstanceByFilter(MyCglibProxy myProxy) {
		Enhancer en = new Enhancer();
		// 进行代理
		en.setSuperclass(BookDaoImpl.class);
		en.setCallbacks(new Callback[]{myProxy,NoOp.INSTANCE});     
		en.setCallbackFilter(new MyProxyFilter());
		// 生成代理实例
		return (BookDaoImpl) en.create();
	}

}

 MyCglibClient.java

public class MyCglibClient {
	
	public static void main(String[] args) {
		BookDaoImpl service = MyCglibProxyFactory.getProxyInstance(new MyCglibProxy("boss"));  
		service.create();
		BookDaoImpl service2 = MyCglibProxyFactory.getProxyInstance(new MyCglibProxy("john"));  
		service2.create();

		BookDaoImpl service3 = MyCglibProxyFactory.getAuthInstanceByFilter(new MyCglibProxy("join"));  
		service3.create();
		BookDaoImpl service4 = MyCglibProxyFactory.getAuthInstanceByFilter(new MyCglibProxy("john"));  
		service4.query();
	}

}

 运行结果:

调用的方法是:create
create() is running...
调用的方法是:create
你没有权限!
调用的方法是:create
你没有权限!
query() is running...

 

你可能感兴趣的:(java,动态代理,jdk,cglib,java技术)