2、CountImpl.java
-
package net.battier.dao.impl;
-
import net.battier.dao.Count;
-
/**
-
* 委托类(包含业务逻辑)
-
*
-
* @author Administrator
-
*
-
*/
-
public class CountImpl implements Count {
-
@Override
-
public void queryCount() {
-
System.out.println("查看账户方法...");
-
}
-
@Override
-
public void updateCount() {
-
System.out.println("修改账户方法...");
-
}
-
}
-
、CountProxy.java
-
package net.battier.dao.impl;
-
import net.battier.dao.Count;
-
/**
-
* 这是一个代理类(增强CountImpl实现类)
-
*
-
* @author Administrator
-
*
-
*/
-
public class CountProxy implements Count {
-
private CountImpl countImpl;
-
/**
-
* 覆盖默认构造器
-
*
-
* @param countImpl
-
*/
-
public CountProxy(CountImpl countImpl) {
-
this.countImpl = countImpl;
-
}
-
@Override
-
public void queryCount() {
-
System.out.println("事务处理之前");
-
// 调用委托类的方法;
-
countImpl.queryCount();
-
System.out.println("事务处理之后");
-
}
-
@Override
-
public void updateCount() {
-
System.out.println("事务处理之前");
-
// 调用委托类的方法;
-
countImpl.updateCount();
-
System.out.println("事务处理之后");
-
}
-
}
3、TestCount.java
-
package net.battier.test;
-
import net.battier.dao.impl.CountImpl;
-
import net.battier.dao.impl.CountProxy;
-
/**
-
*测试Count类
-
*
-
* @author Administrator
-
*
-
*/
-
public class TestCount {
-
public static void main(String[] args) {
-
CountImpl countImpl = new CountImpl();
-
CountProxy countProxy = new CountProxy(countImpl);
-
countProxy.updateCount();
-
countProxy.queryCount();
-
}
-
}
-
package net.battier.dao.impl;
-
import net.battier.dao.BookFacade;
-
public class BookFacadeImpl implements BookFacade {
-
@Override
-
public void addBook() {
-
System.out.println("增加图书方法。。。");
-
}
-
}
-
、BookFacadeProxy.java
-
package net.battier.proxy;
-
import java.lang.reflect.InvocationHandler;
-
import java.lang.reflect.Method;
-
import java.lang.reflect.Proxy;
-
/**
-
* JDK动态代理代理类
-
*
-
* @author student
-
*
-
*/
-
public class BookFacadeProxy implements InvocationHandler {
-
private Object target;
-
/**
-
* 绑定委托对象并返回一个代理类
-
* @param target
-
* @return
-
*/
-
public Object bind(Object target) {
-
this.target = target;
-
//取得代理对象
-
return Proxy.newProxyInstance(target.getClass().getClassLoader(),
-
target.getClass().getInterfaces(), this); //要绑定接口(这是一个缺陷,cglib弥补了这一缺陷)
-
}
-
@Override
-
/**
-
* 调用方法
-
*/
-
public Object invoke(Object proxy, Method method, Object[] args)
-
throws Throwable {
-
Object result=null;
-
System.out.println("事物开始");
-
//执行方法
-
result=method.invoke(target, args);
-
System.out.println("事物结束");
-
return result;
-
}
-
}
3、BookFacadeProxy.java
-
package net.battier.proxy;
-
-
import java.lang.reflect.Method;
-
-
import net.sf.cglib.proxy.Enhancer;
-
import net.sf.cglib.proxy.MethodInterceptor;
-
import net.sf.cglib.proxy.MethodProxy;
-
-
/**
-
* 使用cglib动态代理
-
*
-
* @author student
-
*
-
*/
-
public class BookFacadeCglib implements MethodInterceptor {
-
private Object target;
-
-
/**
-
* 创建代理对象
-
*
-
* @param target
-
* @return
-
*/
-
public Object getInstance(Object target) {
-
this.target = target;
-
Enhancer enhancer = new Enhancer();
-
enhancer.setSuperclass(this.target.getClass());
-
// 回调方法
-
enhancer.setCallback(this);
-
// 创建代理对象
-
return enhancer.create();
-
}
-
-
@Override
-
// 回调方法
-
public Object intercept(Object obj, Method method, Object[] args,
-
MethodProxy proxy) throws Throwable {
-
System.out.println("事物开始");
-
proxy.invokeSuper(obj, args);
-
System.out.println("事物结束");
-
return null;
-
-
-
}
-
-
}