提供一种统一、抽象的编程模型来管理不同的事务API,如,JavaTransactionAPI(JTA),JDBC,Hibernate,Java Persistence API (JPA)以及Java Data Objects (JDO),是选择Spring Transaction做事务管理最直接的理由。
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- this is the service object that we want to make transactional --><beanid="fooService"class="x.y.service.DefaultFooService"/><!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) --><tx:adviceid="txAdvice"transaction-manager="txManager"><!-- the transactional semantics... --><tx:attributes><!-- all methods starting with 'get' are read-only --><tx:methodname="get*"read-only="true"/><!-- other methods use the default transaction settings (see below) --><tx:methodname="*"/></tx:attributes></tx:advice><!-- ensure that the above transactional advice runs for any execution of an operation defined by the FooService interface --><aop:config><aop:pointcutid="fooServiceOperation"expression="execution(* x.y.service.FooService.*(..))"/><aop:advisoradvice-ref="txAdvice"pointcut-ref="fooServiceOperation"/></aop:config><!-- don't forget the DataSource --><beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><propertyname="driverClassName"value="oracle.jdbc.driver.OracleDriver"/><propertyname="url"value="jdbc:oracle:thin:@rj-t42:1521:elvis"/><propertyname="username"value="scott"/><propertyname="password"value="tiger"/></bean><!-- similarly, don't forget the PlatformTransactionManager --><beanid="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dataSource"/></bean><!-- other <bean/> definitions here --></beans>
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- this is the service object that we want to make transactional --><beanid="fooService"class="x.y.service.DefaultFooService"/><!-- enable the configuration of transactional behavior based on annotations --><tx:annotation-driventransaction-manager="txManager"/><!-- a PlatformTransactionManager is still required --><beanid="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- (this dependency is defined somewhere else) --><propertyname="dataSource"ref="dataSource"/></bean><!-- other <bean/> definitions here --></beans>