spring声明式事务管理的两种方式

传统的:
<!---->  1  < bean  id ="dataSource"  class ="org.apache.commons.dbcp.BasicDataSource"  destroy-method ="close" >
 2           < property  name ="driverClassName"  value ="oracle.jdbc.driver.OracleDriver"   />
 3           < property  name ="url"  value ="jdbc:oracle:thin:@127.0.0.1:1521:dev"   />
 4           < property  name ="username"  value ="kaktos"   />
 5           < property  name ="password"  value ="kaktos"   />
 6       </ bean >
 7 
 8       < bean  id ="txManager"
 9          class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
10           < property  name ="dataSource"  ref ="dataSource"   />
11       </ bean >
12 
13       < bean  id ="businessBean"
14          class ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
15           < property  name ="transactionManager"  ref ="txManager"   />
16           < property  name ="target"  ref ="businessBeanTarget"   />
17           < property  name ="transactionAttributes" >
18               < props >                 
19                   < prop  key ="*" > PROPAGATION_REQUIRED </ prop >
20               </ props >
21           </ property >
22       </ bean >
23      
24       < bean  id ="businessBeanTarget"  class ="sample.spring.trans.BusinessBean" >
25           < property  name ="dataSource"  ref ="dataSource"   />
26       </ bean >

这样做的弊端就是不得不为每个需要事务的bean做一次声明,如果所有的bean都基本上有一致的配置,这样就太繁琐啦。
下面是第二种方式:
<!---->  1  < beans >
 2       < bean  id ="dataSource"  class ="org.apache.commons.dbcp.BasicDataSource"  destroy-method ="close" >
 3           < property  name ="driverClassName"  value ="oracle.jdbc.driver.OracleDriver"   />
 4           < property  name ="url"  value =" jdbc:oracle:thin:@127.0.0.1:1521:dev "   />
 5           < property  name ="username"  value ="kaktos"   />
 6           < property  name ="password"  value ="kaktos"   />
 7       </ bean >
 8 
 9       < bean  id ="txManager"
10          class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
11           < property  name ="dataSource"  ref ="dataSource"   />
12       </ bean >
13 
14       < bean  id ="matchAllWithPropReq"
15          class ="org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource" >
16           < property  name ="transactionAttribute"  value ="PROPAGATION_REQUIRED"   />
17       </ bean >
18      
19       < bean  id ="matchAllTxInterceptor"  class ="org.springframework.transaction.interceptor.TransactionInterceptor" >
20           < property  name ="transactionManager"  ref ="txManager"   />
21           < property  name ="transactionAttributeSource"  ref ="matchAllWithPropReq"   />
22       </ bean >
23 
24       < bean  id ="autoProxyCreator"
25          class ="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
26           < property  name ="interceptorNames" >
27               < list >
28                   < idref  local ="matchAllTxInterceptor"   />
29               </ list >
30           </ property >
31           < property  name ="beanNames" >
32               < list >
33                   < idref  local ="businessBean"   />
34               </ list >
35           </ property >
36       </ bean >
37      
38       <!--   my beans   -->
39       < bean  id ="businessBean"  class ="sample.spring.trans.BusinessBean" >
40           < property  name ="dataSource"  ref ="dataSource"   />
41       </ bean >
42  </ beans >

BeanNameAutoProxyCreator会在applicationcontext初始化后自动为beanNames属性中的bean建立proxy。



你可能感兴趣的:(spring,oracle,bean,jdbc,配置管理)