
Java的动态代理中invoke方法是顺序执行的吗?我的怎么这么奇怪?
我在使用Java的动态代理的时候,implementsInvocationHandler,实现它的方法invoke,代码是这样的:publicObjectinvoke(O...
我在使用Java的动态代理的时候,implements InvocationHandler,实现它的方法invoke,代码是这样的:
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
Object result = null;
System.out.println("before invoke method :" + method.getName());
result = method.invoke(this.targetObj, args);
System.out.println("after invoke method : " + method.getName());
return result;
}
----------------------------------------------------------------
但是在运行后,在控制台是这样的显示:
before invoke method :getTheNum
after invoke method : getTheNum
the number is : 100
为什么会先执行1.3句,然后再执行第二句??
请大侠赐教! 展开
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
Object result = null;
System.out.println("before invoke method :" + method.getName());
result = method.invoke(this.targetObj, args);
System.out.println("after invoke method : " + method.getName());
return result;
}
----------------------------------------------------------------
但是在运行后,在控制台是这样的显示:
before invoke method :getTheNum
after invoke method : getTheNum
the number is : 100
为什么会先执行1.3句,然后再执行第二句??
请大侠赐教! 展开
1个回答
2013-11-12
展开全部
是这样用的吧,我的一个代码用来在方法前开启事务和方法后关闭的,执行流程应该不会有问题的,你的问题很奇怪啊
用动态代理 在getlist的方法用动态代理包装下,加入事务
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.hibernate.*
import com.strutslet.demo.service.SystemException;
public final class TransactionWrapper {
/**
* 装饰原始的业务代表对象,返回一个与业务代表对象有相同接口的代理对象
*/
public static Object decorate(Object delegate) {
return Proxy.newProxyInstance(delegate.getClass().getClassLoader(),
delegate.getClass().getInterfaces(), new XAWrapperHandler(
delegate));
}
//动态代理技术
static final class XAWrapperHandler implements InvocationHandler {
private final Object delegate;
XAWrapperHandler(Object delegate) {
this.delegate = delegate;
}
//简单起见,包装业务代表对象所有的业务方法
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object result = null;
session = SessionFactory.getSession();
try {
//开始一个事务
Transaction tx = session.beginTransaction();
//调用原始业务对象的业务方法
result = method.invoke(delegate, args);
session.flush();
tx.commit();
} catch (Throwable t) {
//回滚
ta.rollback();
throw new SystemException(t);
}
return result;
}
}
}
bo层事务就不用写了直接写个工厂,return
TransactionWrapper.decorate(new ColumnsDao())
用动态代理 在getlist的方法用动态代理包装下,加入事务
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.hibernate.*
import com.strutslet.demo.service.SystemException;
public final class TransactionWrapper {
/**
* 装饰原始的业务代表对象,返回一个与业务代表对象有相同接口的代理对象
*/
public static Object decorate(Object delegate) {
return Proxy.newProxyInstance(delegate.getClass().getClassLoader(),
delegate.getClass().getInterfaces(), new XAWrapperHandler(
delegate));
}
//动态代理技术
static final class XAWrapperHandler implements InvocationHandler {
private final Object delegate;
XAWrapperHandler(Object delegate) {
this.delegate = delegate;
}
//简单起见,包装业务代表对象所有的业务方法
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object result = null;
session = SessionFactory.getSession();
try {
//开始一个事务
Transaction tx = session.beginTransaction();
//调用原始业务对象的业务方法
result = method.invoke(delegate, args);
session.flush();
tx.commit();
} catch (Throwable t) {
//回滚
ta.rollback();
throw new SystemException(t);
}
return result;
}
}
}
bo层事务就不用写了直接写个工厂,return
TransactionWrapper.decorate(new ColumnsDao())
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询