package com.xiangsoft.proxytraining.bean; import java.lang.reflect.Method; import java.text.Annotation; import java.util.Collections; import java.util.List; import com.xiangsoft.anno.Annotation1; import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; /** * This module is automatically included as part of the Tapestry IoC Registry, it's a good place to * configure and extend Tapestry, or to place your own service definitions. */ public class CglibBean { publicT proxyObject(Class T) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(T); enhancer.setInterfaces(T.getInterfaces()); enhancer.setCallback(new CallbackObj()); return (T) enhancer.create(); } } class CallbackObj implements MethodInterceptor{ public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { Annotation1 at = method.getAnnotation(Annotation1.class); if(at == null){ proxy.invokeSuper(obj, args); return null; } System.out.println(method.getName()); System.out.println(args); System.out.println(proxy.getSuperName()); return null; } }
package com.xiangsoft.proxytraining.proxy; import com.xiangsoft.anno.Annotation1; /** * Start page of application proxytraining. */ public class Cat implements Eatable { @Override public void eat() { System.out.println("cat eat..."); } @Annotation1 public void jump() { System.out.println("cat jump..."); } }
package com.xiangsoft.anno; import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.Target; @Retention(RUNTIME) @Target(ElementType.METHOD) public @interface Annotation1 { /** * */ String name() default ""; String action() default "Hello World"; /** * */ boolean required() default true; }
package com.xiangsoft.proxytraining.proxy; public interface Eatable { public void eat(); }
package com.xiangsoft.mainborad; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import com.xiangsoft.proxytraining.bean.CglibBean; import com.xiangsoft.proxytraining.proxy.Cat; public class Mainboard extends JFrame{ Cat mCat = null; private JPanel contentPane; public static void main(String[] args) { Mainboard mb = new Mainboard(); mb.setVisible(true); } public Mainboard(){ setTitle("设置窗体大小");// 设置窗体标题 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 默认关闭方式 setSize(400,300);// 设置窗体大小 contentPane=new JPanel();// 创建内容面板 contentPane.setLayout(new BorderLayout(0,0)); setContentPane(contentPane);// 设置内容面板 JLabel label=new JLabel("宽度400,高度300"); // 创建标签控件 contentPane.add(label,BorderLayout.CENTER);// 添加标签控件到窗体 JButton jb = new JButton("代理测试"); jb.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { CglibBean bean = new CglibBean(); mCat = bean.proxyObject(Cat.class); mCat.eat(); System.out.println("action 1"); mCat.jump(); System.out.println("action 2"); }}); contentPane.add(jb,BorderLayout.EAST); } }
引用