(singleton,unlimited , poolable), public class BeanFactory { .... private Object getBeanProxy(BeanConfiguration config)throws UtilException { Object bean = null ; Object proxy = null ; TransactionDelegate delegate = null ; String beanName = config.getImpl(); String[] interfaceNames = config.getName(); Class[] cls = new Class[interfaceNames.length] ; try { for (int i = 0; i < interfaceNames.length; i++) { System.out.println("cls ="+interfaceNames); cls = Class.forName(interfaceNames.trim()) ; } if (BeanConfiguration.SINGLETON.equals(config.getMethod())) { bean = cache.getFromCache(beanName) ; if (bean == null) { bean = Class.forName(beanName).newInstance() ; cache.putIntoCache(beanName , bean) ; } delegate = new TransactionDelegate() ; delegate.setObject(bean) ; proxy = Proxy.newProxyInstance(delegate.getClass().getClassLoader() , cls , delegate) ; } else if (BeanConfiguration.UNLIMITED.equals(config.getMethod())) { bean = Class.forName(beanName).newInstance() ; delegate = new TransactionDelegate(); delegate.setObject(bean); proxy = Proxy.newProxyInstance(delegate.getClass().getClassLoader() , cls , delegate); } else if (BeanConfiguration.POOLED.equals(config.getMethod())) { ServiceLog.debug("beanPool instance ="+beanPool); if (beanPool == null) { throw new UtilException("系统没有发现相应的池,不支持池管理Bean" , -1); } ServiceLog.info("current facade bean pool active num is ="+beanPool.currentActiveNum(beanName)); ServiceLog.info("current facade bean pool idle num is ="+beanPool.currentIdleNum(beanName)); bean = beanPool.get(beanName) ; delegate = new TransactionDelegate() ; delegate.setObject(bean); ServiceLog.info("current facade bean pool size is ="+delegate); proxy = Proxy.newProxyInstance(delegate.getClass().getClassLoader() , cls , delegate); } else { throw new UtilException("错误的池的Bean管理方式" , -1); } System.out.println("从池中取出的bean 为="+bean); return proxy ; } catch (UtilException ex) { ex.printStackTrace(); ServiceLog.error(ex.getMessage() , ex); throw ex ; } catch (Exception ex) { ex.printStackTrace(); ServiceLog.error(ex.getMessage() , ex); throw new UtilException(ex); } } ...... } package com.goldenchance.amis.framework.proxy; import java.lang.reflect.Method; import java.lang.reflect.InvocationHandler; import com.goldenchance.common.framework.OperationService; import com.goldenchance.common.exception.FacadeAccessException; import com.goldenchance.common.util.TransactionContext; import com.goldenchance.common.util.TransactionContextFactory; import com.goldenchance.common.log.ServiceLog; import java.util.*; import java.lang.reflect.*; /** * 事务代理类 * <p>Title: 实时监控系统</p> * <p>Description: 通过动态代理技术,实现事务处理的动态封装</p> * @version 1.0 */ public class TransactionDelegate implements InvocationHandler { private final static List DEFAULT_METHOD_LIST = new ArrayList(); private TransactionContextFactory factory ; private Object object; public TransactionDelegate() { factory = TransactionContextFactory.newFactory(); } public void finalize() { this.release(); } public void release() { object = null ; } /** * 将Object对象中的方法设置为纳入默认忽略的方法集合中 */ static { DEFAULT_METHOD_LIST.add("equals"); DEFAULT_METHOD_LIST.add("getClass"); DEFAULT_METHOD_LIST.add("hashCode"); DEFAULT_METHOD_LIST.add("notify"); DEFAULT_METHOD_LIST.add("notifyAll"); DEFAULT_METHOD_LIST.add("toString"); DEFAULT_METHOD_LIST.add("wait"); } /** * 实现接口的invoke方法 * @see java.lang.reflect.InvocationHandler.invoke(Object , Method , Object[]) */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String name = method.getName() ; Object result = null ; ServiceLog.info("开始调用"+object.getClass().getName()+"的方法"+name); if (DEFAULT_METHOD_LIST.contains(name)) { result = method.invoke(object , args); ServiceLog.info("Start : 调用"+object.getClass().getName()+"的方法"+name); return result ; } else if (object instanceof OperationService){ //如果代理对象是OperationService实现类,则说明它支持事务处理 OperationService serivce = (OperationService)object ; TransactionContext tc = factory.getTransactionContext(); try { serivce.setTransactionContext(tc); if (name.startsWith("search")) { //如果是查询方法,则不进行事务封装 result = method.invoke(object , args) ; } else { tc.beginTransaction(); try { result = method.invoke(object , args) ; tc.commitTransaction(); } catch (Exception ex) { ServiceLog.error(ex.getMessage() , ex) ; if (ex instanceof FacadeAccessException) { FacadeAccessException actual = (FacadeAccessException) ex ; tc.rollbackTransaction() ; throw actual ; } throw ex ; } } return result ; } finally { tc.closeConnection(); ServiceLog.info("End : 调用"+object.getClass().getName()+"的方法"+name); } } else { result = method.invoke(object , args) ; return result ; } } public Object getObject() { return object; } public void setObject(Object object) { this.object = object; } }
http://www.jdon.com/jivejdon/thread/18095.html
you ge shi cuo wu............