栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringMVC - 基于注解的AOP开发、注解配置AOP详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringMVC - 基于注解的AOP开发、注解配置AOP详解

文章目录
  • 基于注解的AOP开发
    • 快速入门
      • 新建接口、目标对象类、通知类
      • 将目标类和切面类的对象创建权交给Spring
      • 使用注解告知Spring切面类
      • 在切面类中使用注解织入关系
      • 开启组件扫描和aop自动代理
      • Spring测试类
      • 成功测试
  • 注解配置AOP详解
    • 注解通知的类型
      • 演示环绕通知
      • 成功测试
    • 切点表达式抽取
      • 演示
      • 成功测试

基于注解的AOP开发 快速入门

新建接口、目标对象类、通知类
package com.taotao.anno;


@SuppressWarnings({"all"})
public interface TargetInterface {
    public void save();
}

package com.taotao.anno;


@SuppressWarnings({"all"})
public class Target implements TargetInterface {

    @Override
    public void save() {
        System.out.println("正在存储数据....");
    }
}

package com.taotao.anno;

import org.aspectj.lang.ProceedingJoinPoint;


@SuppressWarnings({"all"})
public class MyAspect {
    public void before(){
        System.out.println("前置增强....");
    }

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

    //Proceeding JoinPoint:正在执行的连接点 === 切点
    public void around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强...");

        Object proceed = pjp.proceed(); //切点方法

        System.out.println("环绕后增强...");
    }

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

    public void after(){
        System.out.println("最终增强...");
    }
}
将目标类和切面类的对象创建权交给Spring

使用注解@Component(“myAspect”)

package com.taotao.anno;

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


@SuppressWarnings({"all"})
@Component("myAspect")
public class MyAspect {
    public void before(){
        System.out.println("前置增强....");
    }

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

    //Proceeding JoinPoint:正在执行的连接点 === 切点
    public void around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强...");

        Object proceed = pjp.proceed(); //切点方法

        System.out.println("环绕后增强...");
    }

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

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

@Component(“target”)

package com.taotao.anno;

import org.springframework.stereotype.Component;


@SuppressWarnings({"all"})
@Component("target")
public class Target implements TargetInterface {

    @Override
    public void save() {
        int i = 1 / 0;
        System.out.println("正在存储数据....");
    }
}
使用注解告知Spring切面类

@Aspect //标注当前MyAspect是一个切面类

注解配置通知方法,并写入切点表达式

package com.taotao.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;


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

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

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

    //Proceeding JoinPoint:正在执行的连接点 === 切点
    public void around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强...");

        Object proceed = pjp.proceed(); //切点方法

        System.out.println("环绕后增强...");
    }

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

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

在切面类中使用注解织入关系
    //配置前置通
    @Before("execution(* com.taotao.anno.*.*(..))")
开启组件扫描和aop自动代理




    


    



Spring测试类
package com.taotao;

import com.taotao.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;


@SuppressWarnings({"all"})

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

    @Autowired
    private TargetInterface target;

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

成功测试

注解配置AOP详解 注解通知的类型

演示环绕通知
package com.taotao.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;


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

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

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

    @Around("execution(* com.taotao.anno.*.*(..))")
    //Proceeding JoinPoint:正在执行的连接点 === 切点
    public void around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强...");

        Object proceed = pjp.proceed(); //切点方法

        System.out.println("环绕后增强...");
    }

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

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

切点表达式抽取

演示

package com.taotao.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


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

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

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

    @Around("pointcut()")
    //Proceeding JoinPoint:正在执行的连接点 === 切点
    public void around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强...");

        Object proceed = pjp.proceed(); //切点方法

        System.out.println("环绕后增强...");
    }

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

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

    @Pointcut("execution(* com.taotao.anno.*.*(..))")
    public void pointcut(){}
}

成功测试

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/838132.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号