Spring17_基于注解的AOP开发11

一、快速入门

 基于注解的aop开发步骤:
  1. 创建目标接口和目标类(内部有切点)
  2. 创建切面类(内部有增强方法)
  3. 将目标类和切面类的对象创建权交给 spring
  4. 在切面类中使用注解配置织入关系
  5. 在配置文件中开启组件扫描和 AOP 的自动代理
  6. 测试

 代码实现:

  1. 创建目标接口和目标类(内部有切点) 

   新建一个包:com.itheima.anno,拷贝上一节中的TargetInterface、Target到anno包下

  2. 创建切面类(内部有增强方法) 

   拷贝上一节中的MyAspect到anno包下

    3. 将目标类和切面类的对象创建权交给spring

package com.itheima.anno;

import org.springframework.stereotype.Component;

@Component("target")
public class Target implements TargetInterface {
    public void save() {
        System.out.println("save running ...");
    }
}
package com.itheima.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

@Component("myAspect")
public class MyAspect {

    public void before(){
        System.out.println("前置增强......");
    }

    public void afterReturning(){
        System.out.println("后置增强......");
    }

    //ProceedingJoinPoint 正在执行的连接点=切点
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强");
        //切点方法
        Object proceed = pjp.proceed();
        System.out.println("环绕后增强");
        return proceed;
    }

    public void afterThrowing(){
        System.out.println("异常抛出异常...");
    }

    public void after(){
        System.out.println("最终增强...");
    }
}

  4. 在切面类中使用注解配置织入关系

package com.itheima.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; @Component("myAspect") @Aspect //标注当前MyAspect是一个切面类 public class MyAspect { @Before("execution(public void com.itheima.anno.*.*(..))") public void before(){ System.out.println("前置增强......"); } public void afterReturning(){ System.out.println("后置增强......"); } //ProceedingJoinPoint 正在执行的连接点=切点 public Object around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("环绕前增强......"); //切点方法 Object proceed = pjp.proceed(); System.out.println("环绕后增强......"); return proceed; } public void afterThrowing(){ System.out.println("异常抛出异常......"); } public void after(){ System.out.println("最终增强......"); } }

  5. 在配置文件中开启组件扫描和 AOP 的自动代理

   新建一个applicationContext-anno.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"
       xmlns:context="http://www.springframework.org/schema/context"
       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
                            http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--组件扫描-->
    <context:component-scan base-package="com.itheima.anno"/>
    <!--aop自动代理-->
    <aop:aspectj-autoproxy />
</beans>

  6. 测试

package com.itheima.test;

import com.itheima.anno.TargetInterface;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-anno.xml")
public class AnnoTest {

    @Autowired
    private TargetInterface target;

    @Test
    public void test1(){
        target.save();
    }
}

   执行test1,检查控制台输出

    

二、注解配置AOP详解

 1. 注解通知的类型

  通知的配置语法:@通知注解("切点表达式")

  

 2. 切点表达式的抽取

  同 xml 配置 aop 一样,我们可以将切点表达式抽取。抽取方式是在切面内定义方法,在该方法上使用@Pointcut注解定义切点表达式,然后在在增强注解中进行引用。具体如下:

package com.itheima.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component("myAspect")
@Aspect //标注当前MyAspect是一个切面类
public class MyAspect {

//    @Before("execution(public void com.itheima.anno.*.*(..))")
    @Before("MyAspect.myPointCut()")
    public void before(){
        System.out.println("前置增强......");
    }

    @Pointcut("execution(public void com.itheima.anno.*.*(..))")
    public void myPointCut(){
    }
}

三、知识要点

  • 注解aop开发步骤

   1. 使用@Aspect标注切面类
   2. 使用@通知注解标注通知方法
   3. 在配置文件中配置aop自动代理<aop:aspectj-autoproxy/>

  • 通知注解类型

    

 

posted on 2023-05-06 14:57  花溪月影  阅读(24)  评论(0编辑  收藏  举报