博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring中AOP实例详解
阅读量:2492 次
发布时间:2019-05-11

本文共 2495 字,大约阅读时间需要 8 分钟。

需要增强的服务

假如有以下service,他的功能很简单,打印输入的参数并返回参数。

@Servicepublic class SimpleService {    public String getName(String name) {        System.out.println("get name is: " + name);        return name;    }}

定义切面和切点

@Component@Aspectpublic class LogAspect {
// 定义切点 @Pointcut("within(com.ydoing.service..*)") // @Pointcut("execution(* com.ydoing.service.*.*(..))") public void pointCut() { } }

Before增强处理

// 定义Before增强处理    // 在目标方法调用之前执行增强处理    @Before("pointCut()")    public void before(JoinPoint jp) {        // 获取连接点传入参数        // Object args = jp.getArgs();        System.out.println("Before增强处理--execute before target method call");    }

测试输出:

Before增强处理--execute before target method callget name is: Bob

AfterReturning增强

// 在目标方法调用之后执行增强处理    @AfterReturning(pointcut = "pointCut()", returning = "ret")    public void afterReturning(JoinPoint jp, Object ret) {        System.out.println("AfterReturnin增强处理--execute after target method call, return value is :" + ret);    }

测试输出:

get name is: BobAfterReturnin增强处理--execute after target method call, return value is :Bob

Around增强

@Around("pointCut()")    public void around(ProceedingJoinPoint jp) {        System.out.println("Around增强--around start...");        Object[] args = jp.getArgs();        // 修改目标方法传入的参数        args[0] = "around_add_" + args[0];        try {            System.out.println("修改传入参数后执行输出:");            jp.proceed(args);        } catch (Throwable e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        System.out.println("Around增强--around end");    }

输出:

Around增强--around start...修改传入参数后执行输出:get name is: around_add_BobAround增强--around end

After增强

// 无论是否发生异常都会 处理    @After("pointCut()")    public void after() {        System.out.println("After增强--always do no matter what happen");    }

输出:

get name is: BobAfter增强--always do no matter what happen

AfterThrowing增强

@AfterThrowing(pointcut = "pointCut()", throwing = "ex")    public void afterThrowing(JoinPoint jp, Throwable ex) {        System.out.println("error is: " + ex);    }

这里没抛异常,就没有输出了

测试代码如下

@Configuration@EnableAspectJAutoProxy@ComponentScan(basePackages = "com.ydoing.service,com.ydoing.aspect")public class AppConfig {
public static void main(String[] args) { @SuppressWarnings("resource") ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class); SimpleService service = ctx.getBean(SimpleService.class); service.getName("Bob"); }}

转载地址:http://uxhrb.baihongyu.com/

你可能感兴趣的文章
laravel 定时任务秒级执行
查看>>
浅析 Laravel 官方文档推荐的 Nginx 配置
查看>>
Swagger在Laravel项目中的使用
查看>>
Laravel 的生命周期
查看>>
CentOS Docker 安装
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
Mysql出现Table 'performance_schema.session_status' doesn't exist
查看>>
MySQL innert join、left join、right join等理解
查看>>
vivado模块封装ip/edf
查看>>
sdc时序约束
查看>>
Xilinx Jtag Access/svf文件/BSCANE2
查看>>
NoC片上网络
查看>>
开源SoC整理
查看>>
【2020-3-21】Mac安装Homebrew慢,解决办法
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>
已知子网掩码,确定ip地址范围
查看>>
判断时间或者数字是否连续
查看>>
docker-daemon.json各配置详解
查看>>