7.CodeMemberMethod类
表示类里的一个成员方法,只有无参构造函数Name 表示方法名Attributes 方法的描述符,可以通过|进行或运算Parameters 方法的参数集合,通过Add方法添加参数Statements 方法中的语句集合,通过Add方法添加
//构造一个方法对象CodeMemberMethod LoadDataByProcessIDMethod = new CodeMemberMethod();//指定方法名LoadDataByProcessIDMethod.Name = "LoadDataByProcessID";//指定方法的修饰符LoadDataByProcessIDMethod.Attributes = MemberAttributes.Public | MemberAttributes.Final;//给方法添加一个类型为int,名称为ProcessID的参数,通过CodeParameterDeclarationExpression来表示一个参数LoadDataByProcessIDMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "ProcessID"));LoadDataByProcessIDMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(DbTransaction), "objTrans"));
//给定方法的引用,表明要调用一个方法CodeMethodReferenceExpression LoadDataExpression = new CodeMethodReferenceExpression();//调用的方法名LoadDataExpression.MethodName = "LoadDataBy";
//定义方法调用的表达式,并将调用方法的引用传入,使用CodeVariableReferenceExpression来表示调用方法时传入的引用CodeMethodInvokeExpression LoadDataInvoke = new CodeMethodInvokeExpression(LoadDataExpression, new CodeVariableReferenceExpression("C_STR_PROCESSID + "=" + ProcessID"), new CodeVariableReferenceExpression("objTrans")); 方便。1、传统使用JDBC的事务管理以往使用JDBC进行数据操作,使用DataSource,从数据源中得到Connection,我们知道数据源是线程安全的,而连接不是线程安全的,所以对每个请求都是从数据源中重新取出一个连接。一般的数据源由容器进行管理,包括连接池。例如TOMCAT,WEBSPHERE,WEBLOGIC等这些J2EE商业容器都提供了这个功能。以往的我们使用JDBC在写代码时,事务管理可能会是这样:Connection conn = null;try{ conn = DBConnectionFactory.getConnection;conn.setAutoCommit(false); //do somethingconn.commit(); //commit transcation}catch(Exception e){ conn.rollback();}finally{ try{conn.close();}catch(SQLException se){//do sth.} //close ResultSet,PreparedStatement,Connection//notice:Maybe ocurr Exception when u close rs,pstmt,conn}按照以往的思路来写代码,代码量比较长,而且容易疏忽,忘掉一些try/catch,引发一些异常无法catch,虽然有时候我们会写DBTool类,来关闭这些资源,并且保证在关闭这些资源时,不向外抛异常,但是这样做会导致额外的麻烦。2、Spring提供的编程式的事务处理Spring提供了几个关于事务处理的类:TransactionDefinition //事务属性定义TranscationStatus //代表了当前的事务,可以提交,回滚。PlatformTransactionManager这个是spring提供的用于管理事务的基础接口,其下有一个实现的抽象类AbstractPlatformTransactionManager,我们使用的事务管理类例如DataSourceTransactionManager等都是这个类的子类。我们使用编程式的事务管理流程可能如下:(1) 声明数据源。(2) 声明一个事务管理类,例如:DataSourceTransactionManager,HibernateTransactionManger,JTATransactionManager等(3) 在我们的代码中加入事务处理代码:TransactionDefinition td = new TransactionDefinition();TransactionStatus ts = transactionManager.getTransaction(td);try{ //do sth transactionManager.commit(ts);}catch(Exception e){transactionManager.rollback(ts);}使用Spring提供的事务模板TransactionTemplate:void add(){ transactionTemplate.execute( new TransactionCallback(){public Object doInTransaction(TransactionStatus ts){//do sth}}}TransactionTemplate也是为我们省去了部分事务提交、回滚代码;定义事务模板时,需注入事务管理对象。3、Spring声明式事务处理Spring声明式事务处理也主要使用了IoC,AOP思想,提供了TransactionInterceptor拦截器和常用的代理类TransactionProxyFactoryBean,可以直接对组件进行事务代理。使用TransactionInterceptor的步骤:(1)定义数据源,事务管理类(2)定义事务拦截器,例如:<bean id = "transactionInterceptor"class="org.springframework.transaction.interceptor.TransactionInterceptor"><property name="transactionManager"><ref bean="transactionManager"/></property><property name="transactionAttributeSource"><value>com.test.UserManager.*r=PROPAGATION_REQUIRED</value></property></bean>(3)为组件声明一个代理类:ProxyFactoryBean<bean id="userManager"class="org.springframework.aop.framework.ProxyFactoryBean"><property name="proxyInterfaces"><value>com.test.UserManager</value></property><property name="interceptorNames"><list><idref local="transactionInterceptor"/></list></property></bean>使用TransactionProxyFactoryBean:<bean id="userManager"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"><property name="transactionManager"><ref bean="transactionManager"/></property><property name="target"><ref local="userManagerTarget"/></property><property name="transactionAttributes"><props><prop key="insert*">PROPAGATION_REQUIRED</prop><prop key="update*">PROPAGATION_REQUIRED</prop><prop key="*">PROPAGATION_REQUIRED,readOnly</prop></props></property></bean>TransactionProxyFactoryBean只是为组件的事务代理,如果我们要给组件添加一些业务方面的验证等,可以使用TransactionTemplate加拦截器方式,为组件添加多个拦截器,spring AOP中提供了三类Advice,即前增强,后增强,抛出异常时的增强,可以灵活使用。结束语Spring可以简单的把普通的java class纳入事务管理,声明性的事务操作起来也很容易。有了Spring之后,声明性事务不再是EJB独有,我们不必为了获得声明性事务的功能而去忍受EJB带来的种种不便。Spring还提供了惟一的事务管理抽象,它能够在各种底层事务管理技术(如JTA或者JDBC)之上提供一个一致的编程模型。
//将方法调用的表达式添加到方法额语句集合中LoadDataByProcessIDMethod.Statements.Add(LoadDataInvoke);//将方法添加到类中ETClass.Members.Add(LoadDataByProcessIDMethod);CodeParameterDeclarationExpression是用指定的类型和参数名来初始化一个方法的参数。CodeVariableReferenceExpression用来表明一个局部变量名的引用。

