Spring Aop 入门

Spring AOP 入门

1. AOP思想介绍

横向重复,纵向抽取

  • Servlet
  • 动态代理
  • Action拦截器

    2. Spring中的AOP

  • Spring能够为我们生成动态代理对象
  • SpringAOP提高了代码的重用性(比如对某个模块提供权限)
  • AOP可以进行权限校验,日志记录,性能监控,事务控制。

    3. Spring实现AOP的原理

  1. 动态代理:被代理对象必须要实现接口,才能产生动态代理对象。
  2. cglib代理:第三方代理技术,可以对任何类生成代理对象。原理是对目标对象继续继承代理。如果目标对象被修饰成final,则无法被代理。

    4.SpringAOP名词

  • Joinpoint(连接点) : 目标对象中所有可以被增强的方法
  • Pointcut(切入点) : 目标对象中准备增强的方法
  • Advice(通知/增强) : 增强的代码
  • Targer(目标对象) : 被代理对象
  • Weaving(织入) : 将通知应用到切入点的过程
  • Proxy(代理) : 将通知织入到目标对象之后,形成代理对象
  • Aspect(切面) : 切入点 + 通知

    5.Spring的AOP操作

  1. 导包
  • 4个基础包
  • log4j和commons.logging
  • spring-aspets和sping-aop
  • org.aopalliance和org.aspectj.weaver
  1. 准备目标对象

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    package com.liweijian.service;

    /**
    * @Author:Liweijian
    * @Description: 目标对象
    * @Date:Create in 16:38 2018/1/20 0020
    */
    public class UserServiceImpl implements UserService {

    @Override
    public void add() {
    System.out.println("添加");
    }

    @Override
    public void delete() {
    System.out.println("删除");

    }

    @Override
    public void update() {
    System.out.println("更新");

    }

    @Override
    public void find() {
    System.out.println("查找 ");

    }
    }
  2. 准备通知

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    package com.liweijian.advice;

    import org.aspectj.lang.ProceedingJoinPoint;

    /**
    * @Author:Liweijian
    * @Description:
    * @Date:Create in 16:40 2018/1/20 0020
    */
    public class MyAdvice {

    public void before(){
    System.out.println("这是前置通知");
    }

    public void afterReturning(){
    System.out.println("这是后置通知(出现异常不会调用)");
    }

    public Object around(ProceedingJoinPoint pjp) throws Throwable {

    System.out.println("这是环绕通知之前的部分");
    Object proceed = pjp.proceed(); //调用目标之前的方法
    System.out.println("这是环绕通知单之后的部分");
    return proceed;
    }

    public void afterException(){
    System.out.println("这是异常通知");
    }

    public void after(){
    System.out.println("这是后置通知(出现异常一样会调用)");
    }

    }
  3. 将通知织入到目标对象

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">


    <!-- 开启注解模式 -->
    <!-- 扫描对应包下(包括子包)的类 -->
    <context:component-scan base-package="com.liweijian.bean"></context:component-scan>

    <!-- 1.准备工作:导入aop命名空间(约束) -->
    <!-- 2.配置目标对象(也可以使用注解配置) -->
    <bean name="userService" class="com.liweijian.service.UserServiceImpl"></bean>
    <!-- 3.配置通知对象 -->
    <bean name="myAdvice" class="com.liweijian.advice.MyAdvice"></bean>

    <!-- 4.将通知织入目标对象 -->
    <aop:config>
    <!-- 配置切入点
    public void cn.itcast.service.UserServiceImpl.save()
    void cn.itcast.service.UserServiceImpl.save()
    * cn.itcast.service.UserServiceImpl.save()
    * cn.itcast.service.UserServiceImpl.*()

    * cn.itcast.service.*ServiceImpl.*(..)
    * cn.itcast.service..*ServiceImpl.*(..)
    -->
    <aop:pointcut id="pc" expression="execution(* com.liweijian..service.*ServiceImpl.*(..))"></aop:pointcut>

    <aop:aspect ref="myAdvice">
    <!-- 前置通知 -->
    <aop:before method="before" pointcut-ref="pc"></aop:before>
    <!--后置通知-->
    <aop:after-returning method="afterReturning" pointcut-ref="pc"></aop:after-returning>
    <!--环绕通知-->
    <aop:around method="around" pointcut-ref="pc"></aop:around>
    <!--异常通知-->
    <aop:after-throwing method="afterException" pointcut-ref="pc"></aop:after-throwing>
    <!--后置通知-->
    <aop:after method="after" pointcut-ref="pc"></aop:after>
    </aop:aspect>

    </aop:config>

    </beans>
  4. 测试

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    package com.liweijian.test;

    import com.liweijian.service.UserService;
    import com.liweijian.service.UserServiceImpl;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    /**
    * @Author:Liweijian
    * @Description:
    * @Date:Create in 16:57 2018/1/20 0020
    */
    public class Demo2 {


    @Test
    public void fun1(){
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserService userService = (UserService) ac.getBean("userService");
    userService.add();
    }

    }
  5. 输出结果

    1
    2
    3
    4
    5
    6
    这是前置通知
    这是环绕通知之前的部分
    添加
    这是后置通知(出现异常一样会调用)
    这是环绕通知单之后的部分
    这是后置通知(出现异常不会调用)
  6. 总结

    • 使用Spring Aop 可以进行权限控制,事务管理等工作。
    • 出现的问题:在引入命名空间时,要记得在schemaLocation中添加上
    • 通过ApplicationContext容器获取对象时,需要获取接口。因为SpringAOP的实现原理之一是通过动态代理实现。

####感谢阅读本博客。

####欢迎关注我的博客:https://li-weijian.github.io/

####欢迎关注我的CSDN:https://blog.csdn.net/qq352642663

####需要联系请加QQ:352642663

####欢迎联系我共同交流