jdk和CGLIB动态代理_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > jdk和CGLIB动态代理

jdk和CGLIB动态代理

 2014/10/22 15:29:58  xuweilovejava  程序员俱乐部  我要评论(0)
  • 摘要:jdk动态代理和CGLIB动态代理的区别:jdk动态代理只能针对实现了接口的目标代理,CGLIB是针对类来实现动态代理,当没有实现接口的类需要代理时,就需要通过CGLIB来实现动态代理。jdk动态代理:User.javapublicclassUser{privateStringuname;privateStringemail;publicStringgetUname(){returnuname;}publicvoidsetUname(Stringuname){this.uname=uname;
  • 标签:jdk 代理

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

jdk动态代理:
?? User.java

class="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", "123@123.com");
		proxy.read("xuwei", "123@123.com");
	}
}

?

?运行结果:

$Proxy0
begin...
write
@org.junit.Test(expected=class org.junit.Test$None, timeout=0)
write xuwei's Email is :123@123.com
end...
$Proxy0
begin...
read
@org.junit.Test(expected=class org.junit.Test$None, timeout=0)
read xuwei's Email is :123@123.com
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...

?

发表评论
用户名: 匿名