package cn.itcast.proxy;// 真实业务对象
public class ChunChun implements ChunChunInterface { @Override
public void sing(double money) {
// System.out.println("如果你能直接找到春春,演出免费");
System.out.println("春春 唱歌 唱支山歌给党听!");
} @Override
public void dance() {
System.out.println("春春 跳舞 !");
}}
接着定义了一个接口
package cn.itcast.proxy;public interface ChunChunInterface {
//跳舞
public interface Danceable {
public void dance();//唱歌
public interface Singable {
public void sing(double money);
}}然后实现动态代理代码如下
package cn.itcast.proxy;import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;// 代理处理程序,提供代理对象
public class ChunProxy {
// 获得春春代理对象 . 返回值必须是 接口
// 根据真实 业务对象创建代理对象
public static ChunChunInterface getChunChunProxy(
final ChunChunInterface chunChun) {
// 第一个参数 被代理对象 类加载器 --- 必须先获得Class对象
// 第二个参数 被代理对象 实现接口 ---- 必须先获得 Class对象
// 第三个参数 代理对象 所有代理行为
ChunChunInterface chunChunInterface = (ChunChunInterface) Proxy
.newProxyInstance(chunChun.getClass().getClassLoader(),
chunChun.getClass().getInterfaces(),
new InvocationHandler() { // 无论程序想访问春春的哪个方法,都将执行 invoke
// 第一个参数 代理对象本身
// 第二个参数 当前代理方法
// 第三个参数 方法参数
@Override
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
System.out.println("春春代理人,拦截!");
// 唱歌 少于10w 不唱、判断当前是想让春春唱歌
if (method.getName().equals("sing")) {
// 想找春春唱歌
double money = (Double) args[0];
if (money < 100000) {
System.out.println("春春比较忙,没时间唱歌!");
} else {
System.out.println("代理人 收取费用...");
// 已经满足条件,找春春唱歌
// 真实业务对象 方法参数
return method.invoke(chunChun, args);
}
}
return null;
}
});
return chunChunInterface;
}
}
我的疑惑时这句代码
ChunChunInterface chunChunInterface = (ChunChunInterface) Proxy
.newProxyInstance(chunChun.getClass().getClassLoader(),
chunChun.getClass().getInterfaces(),
new InvocationHandler() {}) 为什么是chunChun.getClass() 前面不是定义了一个ChunChun类吗 我感觉应该是ChunChun.class还有为什么前面是小写的chunChun啊 求指教 谢谢
解决方案 »
- 用面向对象的方法设计
- 系统实现权限管理怎么样实现的。
- 哪位高手能讲解一下这段代码?整数范围怎么控制在10万以内
- 急!麻烦高手帮我看一个java类,并跟我解释一下每段代码的意思,谢谢!!
- 在ITBAS中使用dynamic获得动态查询SQL,请问在那里能打印出组装好的SQL语句吗?
- 请教GIF转JPG的问题,或者直接读取GIF并对其线性缩放~
- 请教一循环语句问题??
- 小弟初学ejb,想问一下,一般用什么j2ee服务器程序好??
- 内存泄露问题
- SWT browser程序运行久了,出现了no more handles,而且内存会不断的升高,导致网页出现白屏
- j2ee
- hibernate查询的一个问题, 求助高手!
final ChunChunInterface chunChun) {因为方法里面已经指定了他为ChunChunInterface···
只是Object没有class属性所以要用getClass获取,用定义的类名.class一样也是获取字节码
final ChunChunInterface chunChun) {
因为方法参数里面已经实现了ChunChunInterface接口 chunChun
举个例子:public class ObjectDynamicProxy implements InvocationHandler{ //真实对象引入
private Object proxyObj; public void setObj(Object obj) {
this.proxyObj = obj;
}
public ObjectDynamicProxy(Object obj){
this.proxyObj = obj;
} public Object invoke(Object object, Method method, Object[] args)
throws Throwable {
System.out.println("before dynamic proxy method.");
Object obj = method.invoke(proxyObj, args);
System.out.println("after dynamic proxy method.");
return obj;
}
/**
* 代理对象生产工厂
* @param obj
* @return
*/
public static Object factory(Object obj) {
//获取传入对象的Class对象
Class<?> classType = obj.getClass(); //生成代理对象并返回
//该对象实现传入对象所实现的接口
//生成的代理对象的所有方法都由第三个参数的invoke方法来接管和调用
return Proxy.newProxyInstance(classType.getClassLoader(), classType
.getInterfaces(), new ObjectDynamicProxy(obj));
}
}
至于楼主问的问题,我认为8楼已经回答了。
附上以前我写的代码。
package org.aop;
public interface Target
{
void doSomething();
}
///////////////
package org.aop;
public class MyTarget implements Target
{
@Override
public void doSomething()
{
System.out.println("doSomething^^^^^^");
}
}
///////////////////
package org.aop;
public class MyInterceptor
{
public void doBefore()
{
System.out.println("before++++++++++++++++");
}
public void doAfter()
{
System.out.println("after------------------");
}
}
///////////////////////////
package org.aop;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class MyHandler implements InvocationHandler
{
private Target target;
public MyHandler(Target target)
{
this.target = target;
}
private MyInterceptor myInterceptor = new MyInterceptor();
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
{
myInterceptor.doBefore();
Object res = method.invoke(target, args);
myInterceptor.doAfter();
return res;
}
}
////////////////////////
package org.aop;
import java.lang.reflect.Proxy;
public class MyProxy
{
public Target getProxy(Target obj)
{
Target target = new MyTarget();
MyHandler handler = new MyHandler(target);
return (Target) Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), handler);
}
}
///////////////////////////////
package org.aop;
public class Client
{
public static void main(String[] args)
{
Target target = new MyTarget();
MyProxy myProxy = new MyProxy();
Target proxy = myProxy.getProxy(target);
proxy.doSomething();
}
}
final ChunChunInterface chunChun) {
首先你方法里实例化了ChunChunInterface接口 为chunChun
而你chunChun.getClass()是获取这个接口 然后chunChun.getClass().getInterfaces()是获取该接口的内部接口 其实跟内部类差不多 多去看看方法java的方法定义和内部类
final ChunChunInterface chunChun) {
这个是你的方法入口,这里传入了对象chunChun啊.....