[10] AOP的注解配置
1、关于配置文件
<?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: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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
...
</beans>
<?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: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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
...
</beans>
<!-- 开启bean组件扫描 -->
<context:component-scan base-package="dulk.learn"></context:component-scan>
<!-- 启用自动代理 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 开启bean组件扫描 -->
<context:component-scan base-package="dulk.learn"></context:component-scan>
<!-- 启用自动代理 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
2、AOP的注解配置
- 通过 @Component 声明该类为bean组件
- 通过 @Aspect 标记该类为切面
- 通过注解说明类中函数的通知类型
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class Section {
@Before("execution(* dulk.learn..*.*(..))")
public void doBefore(JoinPoint point) {
System.out.println("doBefore");
}
@AfterReturning(pointcut = "execution(* dulk.learn..*.*(..))", returning = "ret")
public void doAfterReturning(JoinPoint point, Object ret) {
System.out.println("doAfterReturning");
System.out.println("The returning obj is " + ret);
}
@AfterThrowing(pointcut = "execution(* dulk.learn..*.*(..))", throwing = "e")
public void doThrow(JoinPoint point, Throwable e) {
System.out.println("doThrow");
}
@After("execution(* dulk.learn..*.*(..))")
public void doAfter() {
System.out.println("doAfter");
}
@Around("execution(* dulk.learn..*.*(..))")
public void doAround(ProceedingJoinPoint point) {
System.out.println("doAround-dobefore");
try {
Object obj = point.proceed();
System.out.println("doAround-doAfterReturning");
} catch (Throwable throwable) {
System.out.println("doAround-doThrow");
}
System.out.println("doAround-doAfter");
}
}
x
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class Section {
@Before("execution(* dulk.learn..*.*(..))")
public void doBefore(JoinPoint point) {
System.out.println("doBefore");
}
@AfterReturning(pointcut = "execution(* dulk.learn..*.*(..))", returning = "ret")
public void doAfterReturning(JoinPoint point, Object ret) {
System.out.println("doAfterReturning");
System.out.println("The returning obj is " + ret);
}
@AfterThrowing(pointcut = "execution(* dulk.learn..*.*(..))", throwing = "e")
public void doThrow(JoinPoint point, Throwable e) {
System.out.println("doThrow");
}
@After("execution(* dulk.learn..*.*(..))")
public void doAfter() {
System.out.println("doAfter");
}
@Around("execution(* dulk.learn..*.*(..))")
public void doAround(ProceedingJoinPoint point) {
System.out.println("doAround-dobefore");
try {
Object obj = point.proceed();
System.out.println("doAround-doAfterReturning");
} catch (Throwable throwable) {
System.out.println("doAround-doThrow");
}
System.out.println("doAround-doAfter");
}
}
[10] AOP的注解配置的更多相关文章
- Spring之AOP的注解配置
配置过程可以简单的分为3步: 1,业务类配置 在业务类前加入,将业务类交由Spring管理 @Component("s") 这个表示,这个业务类的Bean名字为 s . 2,将切点 ...
- spring的aop的注解配置
一.使用注解的方式配置后置通知 第一步,创建通知类LogAdvice 第二步,要在spring主配置文件中声明以注解的方式配置spring aop 第三步,测试 二.其他异常配置 package c ...
- Spring 使用AOP——基于注解配置
首先,使用注解实现AOP是基于AspectJ方式的. 创建包含切点方法的类 package cn.ganlixin.test; import org.aspectj.lang.annotation.P ...
- Spring的AOP机制---- AOP的注解配置---- AOP的注解配置
3333隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢 ...
- 【Spring五】AOP之使用注解配置
AOP使用注解配置流程: 1.当spring容器启动时候. < context:component- scan base-package= "cn.itheima03.sprin ...
- Spring AOP的注解方式实现
spring也支持注解方式实现AOP,相对于配置文件方式,注解配置更加的轻量级,配置.修改更加方便. 1.开启AOP的注解配置方式 <!-- 开启aop属性注解 --> <aop:a ...
- Spring(十五):通过注解配置 Bean
在ClassPath中扫描组件 1)组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件: 2)特定组件包含: --- @C ...
- Spring AOP及事务配置三种模式详解
Spring AOP简述 Spring AOP的设计思想,就是通过动态代理,在运行期对需要使用的业务逻辑方法进行增强. 使用场景如:日志打印.权限.事务控制等. 默认情况下,Spring会根据被代理的 ...
- Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较
本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一 ...
随机推荐
- 根据需要扩展java中的ThreadPoolExecutor
经常被重写的三个方法 ThreadPoolExecutor是可扩展的,通过查看源码可以发现,它提供了几个可以在子类化中改写的方法:beforeExecute,afterExecute,terminat ...
- Python之随机梯度下降
实现:# -*- coding: UTF-8 -*-""" 练习使用随机梯度下降算法"""import numpy as npimport ...
- npm与yarn常用命令对比
最近在用yarn,但是命令老是记不住,在此记录,方便日后翻看 图片截取自:http://yuanhehe.cn/2017/06/11/npm-%E4%B8%8E-Yarn-%E5%B8%B8%E7%9 ...
- Arch Linux 更新源(以清华 arch 源为例)
Arch Linux 编辑/etc/pacman.d/mirrorlist,在文件最顶端添加: Server = https://mirrors.tuna.tsinghua.edu.cn/archl ...
- 【PAT】B1057 数零壹(20 分)
简单题,简单字符串处理加简单数学进制转换 #include<stdio.h> #include<string.h> #include<ctype.h> int ma ...
- C++基础算法学习——汉洛塔问题
汉诺塔问题古代有一个梵塔,塔内有三个座A.B.C,A座上有64个盘子,盘子大小不等,大的在下,小的在上(如图).有一个和尚想把这64个盘子从A座移到C座,但每次只能允许移动一个盘子,并且在移动过程中, ...
- linux运行apache出现403错误
1.文档权限问题,这是linux操作系统下经常会遇到的问题,需要使用chmod的指令把网站所在目录的权限提升到755.2.SElinux,开启它也会导致403错误的产生. 查看SELinux状态:1. ...
- row_number() over() 一句话概括,以及max()函数的一种查询分组中最大值的用法
row_number() over(partition by col1 order by col2) 根据COL1分组可能会有多个组,每组组内根据COL2进行排序.每组内都有自动生成的序号,从1开始, ...
- MyCat原理及分布式分库分表
1.什么是MyCat: MyCat是一个开源的分布式数据库系统,是一个实现了MySQL协议的服务器,前端用户可以把它看作是一个数据库代理,用MySQL客户端工具和命令行访问,而其后端可以用MySQL ...
- 字符串之StringBuffer 与 StringBuilder的对比
StringBuilder 和 StringBuffer是高度类似的两个类 StringBuilder是StringBuffer的版本改写,下面从几个方面简单的对比下他们的区别 原文地址:[十四]基础 ...