zoukankan      html  css  js  c++  java
  • Spring学习02:面向切面编程AOP

    基于XML的AOP配置

    1. 把通知类bean交给spring管理

    2. 使用<aop:config>标签表示开始AOP的配置

    3. 使用<aop:aspect>标签表示配置切面

      • id属性:切面的唯一标识
      • ref属性:指定通知类bean的id
    4. <aop:aspect>标签的内部使用相应标签来配置通知的类型

      • <aop:before>:配置前置通知,在切入点方法执行之前执行
        • method属性:用于指定哪个方法是前置通知
        • pointcut属性:用于指定切入点表达式,该表达式的含义指的是对业务层中哪些方法增强
      • <aop:after-returning>:配置后置通知,在切入点方法正常执行之后执行,它和异常通知永远只能执行一个
      • <aop:after-throwing>:配置异常通知,在切入点方法执行产生异常之后执行,它和后置通知永远只能执行一个
      • <aop-after>:无论切入点方法是否正常执行它都会在其后面执行
      • <aop:around>:配置环绕通知
        • spring提供了一个接口:ProceedingJoinPoint,该接口有一个方法proceed(),此方法就相当于明确调用切入点方法,该接口可以作为环绕通知的方法参数,在程序执行时,spring框架会提供该接口的实现类给我们使用
        • spring中的环绕通知:是spring框架提供的一种可以在代码中手动控制增强方法何时执行的方式
      • <aop:pointcut>:配置切入点表达式
        • id属性:用于指定表达式的唯一标识
        • expression属性:用于指定表达式内容

      切入点表达式

      • 关键字:execution(表达式)
      • 表达式写法:访问修饰符 返回值 包名 .包名.包名...类名.方法名(参数列表)(要导aspectj的jar包才能识别)
        • 访问修饰符可以省略
        • 返回值可以使用通配符*,表示任意返回值
        • 包名可以使用通配符*,表示任意包,但是有几级包,就需要些几个*.
        • 包名可以使用..表示当前包及其子包
        • 类名和方法名都可以使用通配符*
        • 参数列表:可以直接写数据类型,可以使用通配符表示任意类型,但是必须有参数,可以使用..表示有无参数均可,有参数可以是任意类型
          • 基本类型直接写名称,如int
          • 引用类型写包名.类名的方式,如java.lang.String
        • 实际开发中切入点表达式的通常写法:
          • 切到业务层实现类下的所用方法,如* com.chenpeng.service.impl.*.*(..)

      使用xml配置

      <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"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context.xsd
              http://www.springframework.org/schema/aop
              http://www.springframework.org/schema/aop/spring-aop.xsd">
          
          </bean>-->
          <!--配置service-->
          <bean id="accountService" class="com.chen.service.impl.AccountServiceImpl">
              <!--注入Dao-->
              <property name="accountDao" ref="accountDao"></property>
          </bean>
          <!--配置dao-->
          <bean id="accountDao" class="com.chen.dao.impl.AccountDaoImpl">
              <!--注入QueryRunner-->
              <property name="runner" ref="runner"></property>
              <property name="connectionUtils" ref="connectionUtils"></property>
          </bean>
          <context:component-scan base-package="com.chen"></context:component-scan>
          <!--配置QueryRunner-->
          <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
      
          </bean>
          <!--配置dataSource-->
          <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
              <!--连接数据库的必备信息-->
              <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
              <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/account1?characterEncoding=utf-8"></property>
              <property name="user" value="root"></property>
              <property name="password" value="991105"></property>
          </bean>
      
          <!--配置ConnectionUtils-->
          <bean id="connectionUtils" class="com.chen.utils.ConnectionUtils">
              <property name="dataSource" ref="dataSource"></property>
          </bean>
      
          <!--配置TransactionManager 通知类-->
          <bean id="transactionManager" class="com.chen.utils.TransactionManager">
              <property name="connectionUtils" ref="connectionUtils"></property>
          </bean>
      
          <!--配置AOP-->
          <aop:config>
              <aop:pointcut id="pt" expression="execution(* com.chen.service.impl.*.*(..))"/>
              <aop:aspect id="taAdvice" ref="transactionManager">
                  <aop:before method="beginTransaction" pointcut-ref="pt"></aop:before>
                  <aop:after-returning method="commit" pointcut-ref="pt"></aop:after-returning>
                  <aop:after-throwing method="rollback" pointcut-ref="pt"></aop:after-throwing>
                  <aop:after method="release" pointcut-ref="pt"></aop:after>
              </aop:aspect>
          </aop:config>
      </beans>
      

    基于注解的AOP配置

    使用的注解

    • @Aspect:表示当前类是一个切面类
    • @Before:前置通知
    • @AfterReturning:后置通知
    • @AfterThrowing:异常通知
    • @After:最终通知(使用注解配置AOP时最终通知会在后置/异常通知之前)
    • @Around:环绕通知(使用环绕通知没有问题)
    • @EnableAspectJAutoProxy:表示开启注解AOP的支持(xml中可以使用aop:aspectj-autoproxy标签)
    • @Pointcut:指定切入表达式的内容

    使用注解配置AOP

    @Component("logger")
    @Aspect	//表示当前类是一个通知类
    public class Logger {
    
        //配置切入点表达式,通过调用被注解的方法获取切入点表达式
        @Pointcut("execution(* com.chenpeng.service.impl.*.*(..))")
        private void pt(){} 
        
    	@Before("pt()")
        public void printLogBefore(){
            System.out.println("前置通知Logger类中的printLogBefore方法开始记录日志了。。。");
        }
    
        @AfterReturning("pt()")
        public void printLogAfterReturning(){
            System.out.println("后置通知Logger类中的printLogAfterReturning方法开始记录日志了。。。");
        }
        
    	@AfterThrowing("pt()")
        public void printLogAfterThrowing(){
            System.out.println("异常通知Logger类中的printLogAfterThrowing方法开始记录日志了。。。");
        }
    }
    
  • 相关阅读:
    BZOJ 3754 Tree之最小方差树
    【CSS】318- CSS实现宽高等比自适应容器
    【Vuejs】317- 提升90%加载速度——Vuecli下的首屏性能优化
    【H5】316- 移动端H5跳坑指南
    【每周小回顾】2- 一起回顾上周精彩内容
    【Webpack】315- 手把手教你搭建基于 webpack4 的 vue2 多页应用
    【Web技术】314- 前端组件设计原则
    【JS】313- 复习 回流和重绘
    【JS】312- 复习 JavaScript 严格模式(Strict Mode)
    【Canvas】311- 解决 canvas 在高清屏中绘制模糊的问题
  • 原文地址:https://www.cnblogs.com/codeDD/p/12696999.html
Copyright © 2011-2022 走看看