zoukankan      html  css  js  c++  java
  • Spring AOP(3)使用AspectJ xml配置

    1.使用maven引入依赖:

    <dependency>
          <groupId>aopalliance</groupId>
          <artifactId>aopalliance</artifactId>
          <version>1.0</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>4.2.4.RELEASE</version>
        </dependency>
    
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjweaver</artifactId>
          <version>1.8.9</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aspects</artifactId>
          <version>4.2.4.RELEASE</version>
        </dependency>

    2.配置applicationContext.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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <!--XML的配置方式完成AOP的开发===============-->
        <!--配置目标类=================-->
        <bean id="customerDao" class="com.imooc.aspectJ.demo2.CustomerDaoImpl"/>
    
        <!--配置切面类-->
        <bean id="myAspectXml" class="com.imooc.aspectJ.demo2.MyAspectXml"/>
    
        <!--aop的相关配置=================-->
        <aop:config>
            <!--配置切入点-->
            <aop:pointcut id="pointcut1" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.save(..))"/>   <!-- * 表示所有的修饰符 如:public,private等-->
            <aop:pointcut id="pointcut2" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.update(..))"/>
            <aop:pointcut id="pointcut3" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.delete(..))"/>
            <aop:pointcut id="pointcut4" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.findOne(..))"/>
            <aop:pointcut id="pointcut5" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.findAll(..))"/>
            <!--配置AOP的切面-->
            <aop:aspect ref="myAspectXml">
                <!--配置前置通知-->
                <aop:before method="before" pointcut-ref="pointcut1"/>
                <!--配置后置通知-->
                <aop:after-returning method="afterReturing" pointcut-ref="pointcut2" returning="result"/>
                <!--配置环绕通知-->
                <aop:around method="around" pointcut-ref="pointcut3"/>
                <!--配置异常抛出通知-->
                <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut4" throwing="e"/>
                <!--配置最终通知-->
                <aop:after method="after" pointcut-ref="pointcut5"/>
            </aop:aspect>
    
        </aop:config>
    </beans>

    注:在AOP中有几个概念:
      — 方面(Aspect):一个关注点的模块化,这个关注点实现可能另外copy横切多个对象。事务管理是J2EE应用中一个很好的横切关注点例子。方面用Spring的Advisor或拦截器实现。

      — 连接点(Joinpoint):程序执行过百程中明确的点,如方法的调用或特定的异常被抛出。

      — 通知(Advice):在特定的连接点,AOP框架执行的动作。各种类型的通知包括“around”、“before”和“throws”通知。

      — 切入点(Pointcut):指定一个通知将被引发的一系列连接点的集合。AOP框架必须允许开发者度指定切入点,例如,使用正则表达式。

      所以“<aop:aspect>”实际上是定义横切逻辑,就是在连接点上做什么,“而<aop:advisor>”则定义了在哪些连接点应用什么<aop:aspect>。Spring这样做的好处就是可以让多个横切逻辑(即<aop:aspect>定义的)多次使用,提供可重用性。

      你后面的两个类实际上就是实现横切逻辑的不同方式,一种需要实现特定接口,一种以POJO + Annotation , 在功能上没道有太大差别,只是方式不同。

  • 相关阅读:
    管理员技术(三): 配置静态网络地址、 使用yum软件源 、 升级Linux内核、查找并处理文件、查找并提取文件内容
    管理员技术(二): 访问练习用虚拟机、 命令行基础技巧 、 挂载并访问光盘设备、ls列表及文档创建、复制删除移动
    管理员技术(一):装机预备技能、安装一台RHEL7虚拟机、使用RHEL7图形桌面、Linux命令行基本操作
    基础(三):yum(RedHat系列)和apt-get(Debian系列 )用法及区别
    基础(二):Linux系统/etc/init.d目录和/etc/rc.local脚本
    基础(一):SCSI硬盘与IDE硬盘有什么区别
    高级运维(六):源码安装Redis缓存服务、常用Redis数据库操作指令、配置Redis主从服务器
    错题
    count 【mysql】
    自连接和子查询
  • 原文地址:https://www.cnblogs.com/shouyaya/p/12561675.html
Copyright © 2011-2022 走看看