|
自动配置代理使用AOP,实现比较简单,下面展示的是如何半自动的实现事务代理,下面代码在spring xml中配置,applicationContext.xml
- <!-- 配置数据源 -->
- <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
- <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?useSSL=false"></property>
- <property name="user" value="root"></property>
- <property name="password" value="1234"></property>
- </bean>
- <!-- 配置业务逻辑层对象 -->
- <bean id="IAccountService" class="com.hongwei.service.AccountServiceImpl">
- <property name="accountDao" ref="IAccountDao"></property>
- </bean>
- <!-- 配置事务代理 -->
- <bean id="transactionProxyFactoryBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
- <property name="proxyInterfaces" value="com.hongwei.service.IAccountService"></property>
- <property name="target" ref="IAccountService"></property>
- <property name="transactionManager" ref="dataSourceTransactionManager"></property>
- <property name="transactionAttributes">
- <props>
- <!--
- service 代理对象
- 4.1 proxyInterfaces 接口
- 4.2 target 目标类
- 4.3 transactionManager 事务管理器
- 4.4 transactionAttributes 事务属性(事务详情)
- prop.key :确定哪些方法使用当前事务配置
- prop.text:用于配置事务详情
- 格式:PROPAGATION,ISOLATION,readOnly,-Exception,+Exception
- 传播行为 隔离级别 是否只读 异常回滚 异常提交
- 例如:
- <prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop> 默认传播行为,和隔离级别
- <prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly</prop> 只读
- <prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,+java.lang.ArithmeticException</prop> 有异常扔提交
- -->
- <prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
- </props>
- </property>
- </bean>
- <!-- 配置事务管理器 -->
- <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="comboPooledDataSource"></property>
- </bean>
复制代码
|
|