Spring基于XML配置的AOP简单使用

1.什么是AOP:

  

    简而言之, AOP是 我们把重复性的代码提取出来, 在需要的执行的时候, 使用动态代理的技术, 在不修改源码的情况下,进行功能增强
 

2.AOP术语
     
Joinpoint( 连接点):
                 所谓连接点是指那些被拦截到的点。在 spring 中,这些点指的是方法,因为 spring 只支持方法类型的连接点。
     Pointcut( 切入点):
                 所谓切入点是指我们要对哪些 Joinpoint 进行拦截的定义。

     Advice( 通知/ 增强):
                 所谓通知是指拦截到 Joinpoint 之后所要做的事情就是通知。

                 通知的类型:前置通知,后置通知,异常通知,最终通知,环绕通知。
     Introduction( 引介):
                 引介是一种特殊的通知在不修改类代码的前提下, Introduction 可以在运行期为类动态地添加一些方法或 Field。
     Target( 目标对象):
                 代理的目标对象。
     Weaving( 织入):
                 是指把增强应用到目标对象来创建新的代理对象的过程。
                 spring 采用动态代理织入,而 AspectJ 采用编译期织入和类装载期织入。
     Proxy (代理):
                 一个类被 AOP 织入增强后,就产生一个结果代理类。
     Aspect( 切面):
                是切入点和通知(引介)的结合。

3.jar包说明

  

2.案例

      Maven坐标

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.3.RELEASE</version>
        </dependency>

    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">
</beans>

实体类接口-AccountService.java:

public interface AccountService
{
    /*
     * @Author chensy
     * @Description //TODO 模拟保存账户
     * @Date 13:53 2019/4/22
     * @Param []
     * @return
     **/
    void saveAccount();

    /*
     * @Author chensy
     * @Description //TODO 模拟更新账户
     * @Date 13:54 2019/4/22
     * @Param [i]
     * @return void
     **/
    void updateAccount(int i);

    /*
     * @Author chensy
     * @Description //TODO 模拟删除账户
     * @Date 13:54 2019/4/22
     * @Param []
     * @return void
     **/
    int deleteAccount();
}

实体类实现类--AccountSeriveImp.java

import com.chensy.service.AccountService;

public class AccountSeriveImp implements AccountService
{
    public void saveAccount() {
        System.out.println("执行了保存");
    }

    public void updateAccount(int i) {
        System.out.println("执行了更新" + i);
    }

    public int deleteAccount() {
        System.out.println("执行了删除");
        return 0;
    }
}

通知类-- Logger:
 

public class Logger
{
    /*
     * @Author chensy
     * @Description //TODO 打印日志, 让其在切入点方法执行之前执行
     * @Date 13:57 2019/4/22
     * @Param []
     * @return void
     **/
    public void printLog(){
        System.out.println("Logger[printLog] is stating.....");
    }
}

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">

    <!--配置spring的ioc ,  把service对象配置进来-->
    <bean id="accountService" class="com.chensy.service.Imp.AccountSeriveImp"></bean>

<!--    spring中的基于xml的AOP  配置步骤
    1.把通知Bean也交给spring来管理
    2.使用aop:config标签表明开始AOP的配置
    3.使用aop:aspect标签表明配置切面
           id属性: 给切面提供一个唯一标识
           ref属性: 指定通知类bean的id
    4.在aop:aspect标签内部使用相应的标签来配置通知的类型
            aop:before 前置通知
                 method: 指定Logger类中的哪个方法是前置通知
                 pointcuit: 用于指定切入点表达式, 该表达式指的是哪些方法需要增强
            切入点表达式的写法:
                 execution
                 表达式:
                      访问符  返回值  包名.包名.......类名.方法名(参数列表)
                      如:
                      execution(public  void  com.chensy.service.Imp.AccountSeriveImp.saveAccount())
-->
<!--   配置 Logger类  -->
    <bean id="logger" class="com.chensy.Utils.Logger"></bean>
<!--    配置AOP   -->
    <aop:config>
        <aop:aspect id="logAdvice" ref="logger">
            <aop:before method="printLog" pointcut="execution(public  void  com.chensy.service.Imp.AccountSeriveImp.saveAccount())"></aop:before>
        </aop:aspect>
    </aop:config>
</beans>

运行结果:  
     

猜你喜欢

转载自blog.csdn.net/qq_38737845/article/details/89451143