使用 cglib 代理的实现



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);

}
}





[quote]

<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>


[/quote]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值