zoukankan      html  css  js  c++  java
  • 【Spring】AOP以及实现实例

    AOP面向切面编程

    AOP在Spring中的使用

    提供声明式事务,允许用户自定义切面

    • 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等等…

    • 切面(Aspect):横切关注点 被模块化的特殊对象。即,它是一个类。(Log类)

    • 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。(Log类中的方法)

    • 目标(Target):被通知对象。(生成的代理类)

    • 代理(Proxy):向目标对象应用通知之后创建的对象。(生成的代理类)

    • 切入点(PointCut):切面通知执行的”地点”的定义。(最后两点:在哪个地方执行,比如:method.invoke())

    • 连接点(JointPoint):与切入点匹配的执行点。

     

    方式一:Spring原生接口

    (比如说前置通知的类 就实现MethodBeforeAdvice接口等等 需要实现spring原生接口)

     

     

     

     完整的配置文件如下:需要注意头文件的aop 以及aop的格式

    <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
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <bean id="userservice" class="com.lei.service.UserServiceImpl"></bean>
        <bean id="afterlog" class="com.lei.log.AfterLog"></bean>
        <bean id="beforelog" class="com.lei.log.BeforeLog"></bean>
    
        <aop:config>
            <aop:pointcut id="cut" expression="execution(* com.lei.service.UserServiceImpl.*(..))"/>
            <aop:advisor advice-ref="beforelog" pointcut-ref="cut"></aop:advisor>
            <aop:advisor advice-ref="afterlog" pointcut-ref="cut"></aop:advisor>
        </aop:config>
    </beans>

    因为aop的原理就是动态代理,所以拿到的bean的类型为代理角色。所以应用多态,使用接口UserService进行引用。

     关于执行期间 什么方法是前置方法 还是后置方法 取决于advisor(切面方法)实现了哪个接口。

    方式二、自定义类 作为切面 实现AOP

    首先我们来书写一个自定义类 里面声明两个方法(一会一个前置 一个后置)

     配置文件需要更改

      <!--自定义类实现切入-->
        <bean id="userservice" class="com.lei.service.UserServiceImpl"></bean>
        <bean id="log" class="com.lei.log.Log"></bean>
        <aop:config>
            <!--定义切面-->
            <aop:aspect id="log" ref="log">
    <!--            定义切入点-->
                <aop:pointcut id="cut" expression="execution(* com.lei.service.UserServiceImpl.*(..))"/>
                
                <!--切入方法-->
                <aop:before method="beforeMethod" pointcut-ref="cut"></aop:before>
                <aop:after method="afterMethod" pointcut-ref="cut"></aop:after>
            </aop:aspect>
        </aop:config>

    和上一个方式的不同 上一个方式是使用了不同的类实现了对应接口。然后注册bean,在aop进行调用他们的方法。aop:advisor

    但是这里我们的方式二是将类注册为bean 然后利用配置文件进行 前置还是后置的定义aop:before      aop:after.

    方式三、注解

     只注册bean就可以了

     

  • 相关阅读:
    一起学爬虫(Python) — 02
    模块(第1章)实验——编译问题(没有规则可以创建目标“modules”)
    linux 启动时,sendmail 长时间等待
    红帽发布首个RHEL 7测试版本
    AMD:引入ARM将是自64位以来的最大变革
    (OK) Windows XP 硬盘安装 RHEL7/CentOS7/Fedora19/Fedora20
    Windows 7 硬盘安装 RHEL7/CentOS7/Fedora19/Fedora20
    linux-0.11内核调试运行阅读环境的搭建及使用
    rhel 7—— /boot/grub2/grub.cfg
    Linux环境下网络编程杂谈
  • 原文地址:https://www.cnblogs.com/cckong/p/14384673.html
Copyright © 2011-2022 走看看