Spring之XML 配置AOP 事务管理

举报
陶然同学 发表于 2022/06/24 00:56:02 2022/06/24
【摘要】 目录 XML中配置AOP XML中配置事务管理 XML中配置AOP         切面类 正常写通知 不用加注解 public class MyAspect { //前置通知 public void mybefore(){ System.out.pri...

目录

XML中配置AOP

XML中配置事务管理


XML中配置AOP

        切面类 正常写通知 不用加注解


  
  1. public class MyAspect {
  2. //前置通知
  3. public void mybefore(){
  4. System.out.println("前置通知");
  5. }
  6. //后置通知
  7. public void myaftereturning(Object obj){
  8. System.out.println("后置通知");
  9. }
  10. //环绕通知
  11. public void myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
  12. System.out.println("环绕通知11");
  13. Object proceed = proceedingJoinPoint.proceed();
  14. System.out.println("环绕通知11");
  15. }
  16. //最终通知
  17. public void myafter(){
  18. System.out.println("最终通知");
  19. }
  20. //异常通知
  21. public void myafterThrowing(Throwable e){
  22. System.out.println("异常通知");
  23. System.out.println(e.getMessage());
  24. }
  25. }

        目标类


  
  1. public class UserServiceImpl implements UserService {
  2. public void eat(){
  3. int i = 1 / 0;
  4. System.out.println("吃饭");
  5. }
  6. }

        xml:        

        步骤:        

                1.配置UserServiceImpl 将UserServcieImpl放入Spring容器 

                2.配置切面类 

                3.配置切面 切入点 通知


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xmlns:mvc="http://www.springframework.org/schema/mvc"
  8. xmlns:tx="http://www.springframework.org/schema/tx"
  9. xsi:schemaLocation="http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans.xsd
  11. http://www.springframework.org/schema/mvc
  12. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  13. http://www.springframework.org/schema/aop
  14. http://www.springframework.org/schema/aop/spring-aop.xsd
  15. http://www.springframework.org/schema/context
  16. http://www.springframework.org/schema/context/spring-context.xsd
  17. http://www.springframework.org/schema/tx
  18. http://www.springframework.org/schema/tx/spring-tx.xsd">
  19. <!--配置userService 将UserServiceImpl放入SpringIoc容器-->
  20. <bean id = "userService" class = "com.czxy.demo04.service.impl.UserServiceImpl"></bean>
  21. <!--配置切面类-->
  22. <bean id = "myAspect" class="com.czxy.demo04.aspect.MyAspect"></bean>
  23. <!--配置切面-->
  24. <aop:config>
  25. <aop:aspect ref = "myAspect">
  26. <!--切入点-->
  27. <aop:pointcut id = "myPointcut" expression = "execution(* com.czxy.demo04.service.impl.UserServiceImpl.*(..))"></aop:pointcut>
  28. <aop:before method = "mybefore" pointcut-ref = "myPointcut"></aop:before>
  29. <aop:after-returning method="myaftereturning" pointcut-ref="myPointcut" returning="obj"></aop:after-returning>
  30. <aop:around method="myAround" pointcut-ref="myPointcut"></aop:around>
  31. <aop:after method="myafter" pointcut-ref="myPointcut"></aop:after>
  32. <aop:after-throwing method="myafterThrowing" pointcut-ref="myPointcut" throwing="e"></aop:after-throwing>
  33. </aop:aspect>
  34. </aop:config>
  35. </beans>

        测试类


  
  1. @RunWith(SpringRunner.class)
  2. @ContextConfiguration(locations = {"classpath:demo04.xml"})
  3. public class TestA {
  4. @Resource(name = "userService")
  5. private UserService userService;
  6. @Test
  7. public void test01(){
  8. userService.eat();
  9. }
  10. }


XML中配置事务管理

        转账 当出现错误的时候 转账失败 并且余额不变

        xml配置类步骤:

                1.加载属性配置文件

                2.配置连接池

                3.配置sessionFactory

                4.扫描dao的包

                5.配置service

                6.定义事务管理器

                7.配置事务属性

                8.配置事务切入点

        domain:


  
  1. @Entity(name = "account")
  2. public class Account {
  3. @Id
  4. private Integer id;
  5. private String name;
  6. private Float money;

        mapper:


  
  1. public interface AccountMapper extends Mapper<Account> {
  2. }

        转账类:


  
  1. public class AccountServiceImpl implements AccountService {
  2. private AccountMapper accountMapper;
  3. public void setAccountMapper(AccountMapper accountMapper) {
  4. this.accountMapper = accountMapper;
  5. }
  6. public void change(Integer outId, Integer inId, Float money) {
  7. Account outAccount = accountMapper.selectByPrimaryKey(outId);
  8. outAccount.setMoney(outAccount.getMoney() - money);
  9. accountMapper.updateByPrimaryKey(outAccount);
  10. int i = 1 / 0;
  11. Account inAccount = accountMapper.selectByPrimaryKey(inId);
  12. inAccount.setMoney(inAccount.getMoney() + money);
  13. accountMapper.updateByPrimaryKey(inAccount);
  14. }
  15. }

        测试类:


  
  1. @RunWith(SpringRunner.class)
  2. @ContextConfiguration(locations = "classpath:demo05.xml")
  3. public class TestA {
  4. @Resource(name = "accountService")
  5. private AccountService accountService;
  6. @Test
  7. public void testDemo(){
  8. accountService.change(1,2,3f);
  9. System.out.println("转账成功");
  10. }
  11. }

        xml配置类:


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xmlns:mvc="http://www.springframework.org/schema/mvc"
  8. xmlns:tx="http://www.springframework.org/schema/tx"
  9. xsi:schemaLocation="http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans.xsd
  11. http://www.springframework.org/schema/mvc
  12. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  13. http://www.springframework.org/schema/aop
  14. http://www.springframework.org/schema/aop/spring-aop.xsd
  15. http://www.springframework.org/schema/context
  16. http://www.springframework.org/schema/context/spring-context.xsd
  17. http://www.springframework.org/schema/tx
  18. http://www.springframework.org/schema/tx/spring-tx.xsd">
  19. <!-- 1.1 指定配置文件的位置-->
  20. <context:property-placeholder location="classpath:db.properties"/>
  21. <!-- 1.2 配置数据源 -->
  22. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  23. <property name="driverClassName" value="${jdbc.driver}"/>
  24. <property name="url" value="${jdbc.url}"/>
  25. <property name="username" value="${jdbc.username}"/>
  26. <property name="password" value="${jdbc.password}"/>
  27. <!-- 配置初始化大小、最小、最大连连接数量 -->
  28. <property name="initialSize" value="5"/>
  29. <property name="minIdle" value="10"/>
  30. <property name="maxActive" value="20"/>
  31. </bean>
  32. <!-- 2.1 sessionFactory -->
  33. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  34. <property name="dataSource" ref="dataSource" />
  35. <property name="typeAliasesPackage" value="com.czxy.demo05.domain" />
  36. <property name="configuration">
  37. <bean class="org.apache.ibatis.session.Configuration">
  38. <property name="mapUnderscoreToCamelCase" value="true" />
  39. </bean>
  40. </property>
  41. <property name="plugins">
  42. <array>
  43. <bean class="com.github.pagehelper.PageHelper">
  44. <property name="properties">
  45. <props>
  46. <prop key="dialect">mysql</prop>
  47. <prop key="rowBoundsWithCount">true</prop>
  48. </props>
  49. </property>
  50. </bean>
  51. </array>
  52. </property>
  53. </bean>
  54. <!-- 2.2 扫描Dao的包 -->
  55. <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
  56. <!--指定会话工厂 -->
  57. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  58. <!-- 指定 mybatis 接口所在的包 -->
  59. <property name="basePackage" value="com.czxy.demo05.mapper"/>
  60. </bean>
  61. <!-- 3 配置service -->
  62. <bean id="accountService" class="com.czxy.demo05.service.impl.AccountServiceImpl">
  63. <property name="accountMapper" ref="accountMapper" />
  64. </bean>
  65. <!-- 4 配置事务 -->
  66. <!-- 4.1 定义事务管理器-->
  67. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  68. <property name="dataSource" ref="dataSource"/>
  69. </bean>
  70. <!-- 4.2 配置事务属性 -->
  71. <tx:advice id="txAdvice" transaction-manager="transactionManager" >
  72. <tx:attributes>
  73. <tx:method name="change" propagation="REQUIRED"/>
  74. <tx:method name="*" read-only="true" />
  75. </tx:attributes>
  76. </tx:advice>
  77. <!-- 4.3 配置事务切入点 -->
  78. <aop:config>
  79. <aop:pointcut id="txPointCut" expression="execution(* com.czxy.demo05.service..*.*(..))"/>
  80. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" />
  81. </aop:config>
  82. </beans>

文章来源: blog.csdn.net,作者:陶然同学,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_45481821/article/details/121763584

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。