zoukankan      html  css  js  c++  java
  • 课时6::AOP、execution表达式、前置通知

    .1)AOP:面向方面编程

      1.aop的一些名词

      .2)AOP的通知

        1.一些通知的示意图

       2.以前置通知为例子 其他的通知写法都差不多

        2.1 导入相关jar包

      <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
        <dependency>
          <groupId>aopalliance</groupId>
          <artifactId>aopalliance</artifactId>
          <version>1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/aspectj/aspectjweaver -->
        <dependency>
        <groupId>aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.5.3</version>
        </dependency>

        2.2 配置

          2.2.0 编写ioc容器头部

    xmlns:aop="http://www.springframework.org/schema/aop"
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd"

          2.2.1 注入业务类

    <!--    配置切入点的方法在哪个类-->
        <bean id="studentService" class="net.bdqn.hbz.service.impl.IStudentServiceImpl">
            <property name="iStudentDao" ref="studentDao"></property>
        </bean>

          2.2.2 注入通知类

            <!--    注入”前置通知“类-->
            <!--    连接线的一方 切面-->
        <bean id="before" class="net.bdqn.hbz.aop.Before"/>

          2.2.3 将addStudent 和 通知进行关联

            <aop:config>
    <!--        配置切入点 (在哪里执行通知)  连接线的另一方-->
            <aop:pointcut id="add" expression="execution(public void addStudent(net.bdqn.hbz.pojo.Student))"/>
    <!--        advisor相当于连接切入点和切面的线-->
            <aop:advisor advice-ref="before" pointcut-ref="add"/>
    </aop:config>

            2.2.3.1 aop:config:将 将addStudent 和 通知进行关联

            2.2.3.2 aop:pointcut :切入点的位置

              id:切入点唯一标识符 expression:切入点的位置

            2.2.3.3 aop:advisor:相当于连接切入点和切面的线

              advice-ref:通知类(通知类注入的id) pointcut-ref:切入点的id

        2.3 编写代码

          2.3.1 aop:每当之前add()之前(IStudentService.java addStudent()) 自动执行一个方法

          2.3.2 add():业务方法 log()l:自动执行的通知,即aop前置通知

          2.3.3 编写前置通知的一个类实现MethodBeforeAdvice 将这个类变成前置通知类

    package net.bdqn.hbz.aop;
    
    import org.springframework.aop.MethodBeforeAdvice;
    
    import java.lang.reflect.Method;
    
    /**
     * 前置通知类
     */
    public class Before implements MethodBeforeAdvice {
        @Override
        public void before(Method method, Object[] objects, Object o) throws Throwable {
            System.out.println("执行了前置通知");
        }
    }

          2.3.4 编写业务类

    package net.bdqn.hbz.service.impl;
    
    import net.bdqn.hbz.dao.impl.IStudentDaoImpl;
    import net.bdqn.hbz.pojo.Student;
    import net.bdqn.hbz.service.IStudentService;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    
    public class IStudentServiceImpl implements IStudentService {
        private IStudentDaoImpl iStudentDao;
    
        public void setiStudentDao(IStudentDaoImpl iStudentDao) {
            this.iStudentDao = iStudentDao;
        }
    
        /**
         *
         * @param student
         */
        @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
        @Override
        public void addStudent(Student student) {
            iStudentDao.addStudent(new Student());
        }
    }

      3.如果想要添加多个切入点 通过execution来配置多个切入点

      <aop:pointcut id="add"
    expression="execution( public void deleteStudent(java.lang.Integer)) or execution(public void addStudent(net.bdqn.hbz.pojo.Student))"
    />

        3.1 execution表达式的一些使用说明

  • 相关阅读:
    EXCEL某列长度超过255个字符导入SQL SERVER2005的处理方法
    正则同时获取a标签里的href,text 2项
    火柴棍
    特殊数
    骨牌铺方格
    拼图
    挑战题之排列生成
    模拟 找规律
    sort
    贪心 简单背包问题
  • 原文地址:https://www.cnblogs.com/thisHBZ/p/12511582.html
Copyright © 2011-2022 走看看