提供網(wǎng)站建設(shè)備案公司微信上怎么做廣告推廣
文章目錄
- 一、聲明式事務(wù)之全注解式開發(fā)
- 1、新建springConfig類
- 2、測試程序
- 3、測試結(jié)果
- 二、聲明式事務(wù)之XML實(shí)現(xiàn)方式
- 1、配置步驟
- 2、測試程序
- 3、運(yùn)行結(jié)果
- 附
一、聲明式事務(wù)之全注解式開發(fā)
基于之前的銀行轉(zhuǎn)賬系統(tǒng),將spring.xml配置文件嘎掉,變成全注解式開發(fā)。
加入事務(wù)的銀行轉(zhuǎn)賬
原spring.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><context:component-scan base-package="com.powernode.bank"></context:component-scan><!--配置數(shù)據(jù)源--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/dududu"></property><property name="username" value="root"></property><property name="password" value="123456"></property></bean><!--配置JDBCTemplate--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!--配置事務(wù)管理器--><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!--事務(wù)注解驅(qū)動(dòng)器--><tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven></beans>
1、新建springConfig類
1、@Configuration:表示代替spring.xml配置文件,在這個(gè)類當(dāng)中完成配置
2、@ComponentScan(“com.powernode.bank”):代替spring.xml文件中的組件掃描
3、@EnableTransactionManagement:代替spring.xml文件中的事務(wù)注解驅(qū)動(dòng)器
@Configuration //表示代替spring.xml配置文件,在這個(gè)類當(dāng)中完成配置
@ComponentScan("com.powernode.bank")
@EnableTransactionManagement //代替事務(wù)注解驅(qū)動(dòng)器
public class springConfig {
}
spring.xml文件中還剩余3個(gè)bean,都有屬性、值。
<!--配置數(shù)據(jù)源--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/dududu"></property><property name="username" value="root"></property><property name="password" value="123456"></property></bean><!--配置JDBCTemplate--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!--配置事務(wù)管理器--><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean>
4、@Bean+get方法,并且get方法執(zhí)行結(jié)束的返回值要求是spring.xml文件里bean標(biāo)簽的class對(duì)象
//spring框架看到這個(gè)@Bean注解后,會(huì)調(diào)用這個(gè)被標(biāo)注的方法,這個(gè)方法的返回值是一個(gè)Java對(duì)象,這個(gè)Java對(duì)象會(huì)自動(dòng)納入IoC容器管理//返回的對(duì)象就是spring容器當(dāng)中Bean對(duì)象@Bean(name = "dataSource")public DruidDataSource getDataSource(){DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/dududu");dataSource.setUsername("root");dataSource.setPassword("123456");return dataSource;}@Bean(name = "jdbcTemplate")public JdbcTemplate getJdbcTemplate(DataSource dataSource){ //spring在調(diào)用這個(gè)方法的時(shí)候會(huì)自動(dòng)給我們傳遞過來一個(gè)dataSource對(duì)象JdbcTemplate jdbcTemplate = new JdbcTemplate();jdbcTemplate.setDataSource(dataSource);return jdbcTemplate;}@Bean(name = "txManager")public DataSourceTransactionManager getTxManager(DataSource dataSource){DataSourceTransactionManager txManager = new DataSourceTransactionManager();txManager.setDataSource(dataSource);return txManager;}
2、測試程序
@Testpublic void testNoXML(){ApplicationContext ac = new AnnotationConfigApplicationContext(springConfig.class);AccountService accountService = (AccountService) ac.getBean("accountService");try {accountService.transfer("act_001","act_002",10000);System.out.println("轉(zhuǎn)賬成功");}catch (Exception e){e.printStackTrace();System.out.println("轉(zhuǎn)賬失敗");}}
3、測試結(jié)果
模擬異常,事務(wù)依然可以起作用,錢不會(huì)丟。
二、聲明式事務(wù)之XML實(shí)現(xiàn)方式
全XML式開發(fā),不使用注解。
那么原spring.xml文件中的事務(wù)注解驅(qū)動(dòng)就需要嘎掉
<!--事務(wù)注解驅(qū)動(dòng)器--><tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven>
嘎掉之后,那應(yīng)該怎么添加事務(wù)?
再創(chuàng)建一個(gè)工程 為了不和原來的混在一起
AccountServiceImpl業(yè)務(wù)實(shí)現(xiàn)類中,基于事務(wù)的注解@Transactional刪掉,原spring.xml文件中的事務(wù)注解驅(qū)動(dòng)就需要嘎掉,pom文件中添加aspectj依賴,
1、配置步驟
- 第一步:配置事務(wù)管理器
- 第二步:配置通知
- 第三步:配置切面
pps:需要添加AspectJ的依賴、添加aop命名空間
<!--配置通知--><tx:advice id="txAdvice" transaction-manager="txManager"><!--配置通知的相關(guān)屬性--><tx:attributes><!--之前所講的事務(wù)屬性,都可以在這個(gè)標(biāo)簽中配置--><tx:method name="transfer" propagation="REQUIRED" rollback-for="java.lang.Throwable"/></tx:attributes></tx:advice><!--配置切面--><aop:config><aop:pointcut id="txPointcut" expression="execution(* com.powernode.service..* (..))"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"></aop:advisor></aop:config>
<tx:method name=“transfer” propagation=“REQUIRED” rollback-for=“java.lang.Throwable”/>這里一般不會(huì)寫具體的方法,可以采用通配符的方式。
<tx:method name=“save*” propagation=“REQUIRED” rollback-for=“java.lang.Throwable”/>
<tx:method name=“delete*” propagation=“REQUIRED” rollback-for=“java.lang.Throwable”/>
<tx:method name=“insert*” propagation=“REQUIRED” rollback-for=“java.lang.Throwable”/>
<tx:method name=“modify*” propagation=“REQUIRED” rollback-for=“java.lang.Throwable”/>
<tx:method name=“query*” propagation=“REQUIRED” rollback-for=“java.lang.Throwable”/>
<tx:method name=“find*” propagation=“REQUIRED” rollback-for=“java.lang.Throwable”/>
<tx:method name=“get*” propagation=“REQUIRED” rollback-for=“java.lang.Throwable”/>
等等
2、測試程序
@Testpublic void testNoAnnotation(){ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");AccountService accountService = ac.getBean("accountService", AccountService.class);try{accountService.transfer("act_001","act_002",20000);System.out.println("轉(zhuǎn)賬成功");}catch(Exception e){e.printStackTrace();System.out.println("轉(zhuǎn)賬失敗");}}
3、運(yùn)行結(jié)果
模擬異常:
空指針異常 事務(wù)回滾
錢不會(huì)丟
附
spring.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><context:component-scan base-package="com.powernode"></context:component-scan><!--配置數(shù)據(jù)源--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/dududu"></property><property name="username" value="root"></property><property name="password" value="123456"></property></bean><!--配置JDBCTemplate--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!--配置事務(wù)管理器--><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!--配置通知--><tx:advice id="txAdvice" transaction-manager="txManager"><!--配置通知的相關(guān)屬性--><tx:attributes><!--之前所講的事務(wù)屬性,都可以在這個(gè)標(biāo)簽中配置--><tx:method name="transfer" propagation="REQUIRED" rollback-for="java.lang.Throwable"/></tx:attributes></tx:advice><!--配置切面--><aop:config><aop:pointcut id="txPointcut" expression="execution(* com.powernode.service..* (..))"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"></aop:advisor></aop:config>
</beans>