使用 cglib 代理的实现 博客分类: cglib cglib
程序员文章站
2024-02-14 11:47:04
...
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 { public <T> T proxyObject(Class<T> 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); } }
引用
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
<properties>
</properties>
<modelVersion>4.0.0</modelVersion>
<groupId>com.xiangsoft</groupId>
<artifactId>proxytraining</artifactId>
<version>1.0.1</version>
<packaging>jar</packaging>
<name>proxytraining</name>
<dependencies>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<build>
<finalName>proxytraining</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>package-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
推荐阅读
-
Spring实现AOP的4种方式 博客分类: spring spring代理的AOP@AspectJ注解POJOAspectJ
-
Jdk动态代理与Cglib动态代理的简单使用
-
cglib实现动态代理
-
java中使用cglib中的BeanCopier类进行bean的复制
-
JAVA中的静态代理、动态代理以及CGLIB动态代理总结
-
详解Spring的两种代理方式:JDK动态代理和CGLIB动态代理
-
详解Spring的两种代理方式:JDK动态代理和CGLIB动态代理
-
你必须会的 JDK 动态代理和 CGLIB 动态代理
-
Java使用JDK与Cglib动态代理技术统一管理日志记录
-
Spring学习笔记_AOP的cglib动态代理底层实现方式