cglib动态代理模式

 
--->cglib代理模式------------》添加cglib-nodep-2.1_3.jar

package com.dao;
import net.sf.cglib.proxy.Enhancer;
public class MYClass {
 public int method(){
  System.out.println(  "我的方法。。。" );
  return 1;
 }
 
 public static void main(String[] args) {
  
  Enhancer enhancer = new Enhancer();
  enhancer.setSuperclass(MYClass.class);
  enhancer.setCallback(new MethodInterceptorImpl());
  
  MYClass  my = (MYClass) enhancer.create();
  
  System.out.println(my.method());
 }
}
package com.dao;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class MethodInterceptorImpl  implements MethodInterceptor {
 @Override
 public Object intercept(Object obj, Method method, Object[] args,
   MethodProxy proxy) throws Throwable {
  
    System.out.println("开始日志。。");
    Object objs=  proxy.invokeSuper(obj, args);
    
     System.out.println("结束日志。。");
    
     return objs;
  
 }
}
--》使用CallbackFilter
package com.dao;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.CallbackFilter;
public class CallbackFilterImpl implements CallbackFilter {
 @Override
 public int accept(Method method) {
  if( method.getName().equalsIgnoreCase("method2") ){
   return 1;
  }
  return 0;
 } 
}
package com.dao;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.CallbackFilter;
public class CallbackFilterImpl implements CallbackFilter {
 @Override
 public int accept(Method method) {
  if( method.getName().equalsIgnoreCase("method2") ){
   return 1;
  }
  return 0;
 }
}

package com.dao;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;
public class MYClass {
 public int method(){
  System.out.println(  "我的方法1。。。" );
  return 1;
 }
 
 public int method2(){
  System.out.println(  "我的方法2。。。" );
  return 2;
 }
 public int method3(){
  System.out.println(  "我的方法3。。。" );
  return 3;
 }
 
 public static void main(String[] args) {
  
   Callback[] callbacks =new Callback[] { new MethodInterceptorImpl(),  NoOp.INSTANCE };
  
  Enhancer enhancer = new Enhancer();
  
  enhancer.setSuperclass(MYClass.class);
  
  enhancer.setCallbacks(callbacks);
  
  enhancer.setCallbackFilter(new CallbackFilterImpl());
  
  MYClass  my = (MYClass) enhancer.create();
  
  System.out.println(my.method());
  System.out.println(my.method2());
  System.out.println(my.method3());
  
 }
}
---》使用
public interface MYA {
  public void methodA();
}
package com.dao.impl;
import com.dao.MYA;
public class MYAI implements MYA{
 public MYAI(){
  System.out.println("asfsafads");
 }
 @Override
 public void methodA() {
   System.out.println( "a借口的方法。。。。" );
 }
}
--测试。。
public static void main(String[] args) {
  Class []interces = new Class[]{MYA.class  };
  Object[] delete = new Object[]{ new MYAI() };
  Object obj = Mixin.create(interces, delete);
  MYA mya =   (MYA)obj;
  mya.methodA(); 
  MYA mya3 =   (MYA)obj;
  mya3.methodA();
   
}
---》对象之间补充说明   属性名称类型一致
 BeanCopier copier = BeanCopier.create(Myth.class, Other.class, false);           
copier.copy(myth, other, null); 
 

你可能感兴趣的:(java,cglib代理,java,Java,JAVA)