1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| public class BeanFactory {
public static BusinessService getBusinessService(boolean isProxy){ final BusinessService s = new BusinessServiceImpl(); if(isProxy){ BusinessService proxyS = (BusinessService)Proxy.newProxyInstance(s.getClass().getClassLoader(), s.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object rtValue = null; try{ long time = System.currentTimeMillis(); TransationManager.startTransaction(); rtValue = method.invoke(s, args); TransationManager.commit(); System.out.println(method.getName()+" cost time "+(System.currentTimeMillis()-time) +" millis second"); } catch (Exception e) { TransationManager.rollback(); throw new RuntimeException(e); }finally{ TransationManager.release(); } return rtValue; } }); return proxyS; }else{ return s; } } }
|