zoukankan      html  css  js  c++  java
  • AOP面向切面编程

    AOP面向切面编程:

    主要用于:日志、权限管理、事物;

    1:自定义事务管理类:

    方法执行之前,执行之后,抛出异常的时候分别可执行的操作:

    代码:

    spring-myAop.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" xmlns:aop="http://www.springframework.org/schema/aop"
     4        xsi:schemaLocation="http://www.springframework.org/schema/beans
     5         http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop
     6          http://www.springframework.org/schema/aop/spring-aop.xsd">
     7     <!--引入日志管理类-->
     8     <bean id="webAspectLog" class="com.day02.sation.aop.WebAspectLog"/>
     9     <!--配置切面-->
    10     <aop:config>
    11         <!--日志aop-->
    12         <aop:aspect ref="webAspectLog">
    13             <aop:pointcut id="pointcut" expression="execution(* com.day02.sation.controller.*Controller.*(..))"/>
    14            <!--在方法执行之前执行-->
    15             <aop:before method="beforeLog" pointcut-ref="pointcut"/>
    16             <!--在方法执行之后执行-->
    17             <aop:after method="XXX" pointcut-ref="pointcut"/>
    18             <!--抛出异常的时候执行-->
    19             <aop:after-throwing method="xxx2" pointcut-ref="pointcut"/>
    20            <!-- 注意如果要获取执行后的结果 必须配置参数 returning="对象为afterLog方法的参数对象名称"-->
    21             <aop:after-returning method="afterLog" pointcut-ref="pointcut" returning="returnObj"/>
    22         </aop:aspect>
    23     </aop:config>
    24 </beans>
    View Code

    1-2 :当方法执行完后,不报内部代码错误的情况下,再提交事物,比如购买操作的时候,执行减少库存,增加订单,只有当执行2条sql语句同时执行成功的情况下,才执行,否则有一条执行不成功的情况,都执行失败:

    spring主配置文件中的配置:

    代码:

     1  <!--aop事物-->
     2     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     3         <property name="dataSource" ref="dataSource"/>
     4     </bean>
     5     <!--  什么时候做 when -->
     6     <tx:advice id="txAdvice" transaction-manager="transactionManager">
     7         <tx:attributes>
     8             <tx:method name="get*" read-only="true"/>
     9             <tx:method name="query*" read-only="true"/>
    10             <tx:method name="list*" read-only="true"/>
    11             <tx:method name="list*" read-only="true"/>
    12             <tx:method name="*"/>
    13         </tx:attributes>
    14     </tx:advice>
    15 
    16     <!-- 什么地点做 where -->
    17     <aop:config>
    18         <aop:pointcut id="serviceOperation" expression="execution(* com.floor.shop.service.impl.*Service.*(..))"/>
    19         <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/>
    20     </aop:config>
    View Code
  • 相关阅读:
    [LeetCode] 26 Remove Duplicates from Sorted Array
    归并排序
    插入排序
    选择排序
    冒泡排序
    单链表排序
    如何实现单链表反转
    Linux基础——centos 跳过管理员密码进行登录(单用户模式、救援模式)
    response.sendRedirect()与request.getRequestDispatcher().forward()区别
    json-lib——JsonConfig详细使用说明
  • 原文地址:https://www.cnblogs.com/dw3306/p/9355729.html
Copyright © 2011-2022 走看看