[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编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一 ...
随机推荐
- 【代码笔记】Web-HTML-颜色
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- rem与px之间的换算(移动端)
最近因为工作接触到rem与px之间的换算,之前知道一些,不过还是比较笼统模糊,用起来不是很明白,后来自己查了点资料,以及亲自测试总算明白它们之间是怎么换算的了. rem是一个相对值,它相对于根元素ht ...
- 利用python和shell脚本生成train.txt的标签文件
1. 用shell脚本生成带绝对路径的train.txt 例如我要生成如下形式的带标签的文件,如图:(如有两个标签:0 和 1) shell脚本如下: 这样标签0写入了train.txt # /usr ...
- Android View 绘制流程
Android 中 Activity 是作为应用程序的载体存在,代表着一个完整的用户界面,提供了一个窗口来绘制各种视图,当 Activity 启动时,我们会通过 setContentView 方法来设 ...
- mysql的高级特性-存储过程
定义: 存储例程是存储在数据库服务器中的一组sql语句,通过在查询中调用一个指定的名称来执行这些sql语句命令. 语法: DELIMITER // 声明语句结束符,用于区分; CEATE PROCED ...
- python的subprocess模块执行shell命令
subprocess模块可以允许我们执行shell命令 一般来说,使用run()方法就可以满足大部分情况 使用run执行shell命令 In [5]: subprocess.run('echo &qu ...
- SQLServer限制IP,限制用户,限制SSMS登录
SQL Server不像Mysql那样原生支持限制IP登录. 但可以使用Login触发器来实现. 以下为使用Login触发器实现限制用户u_user_r在指定IP192.168.1.205使用SSMS ...
- 第四章 Hyper-V 2012 R2 网络配置
尼玛的我不高兴写了,所以下面的文档我直接把原来的pdf给转换出来,加了点自己的注解,我写的话会写自己觉得终于的章节. 在搭建虚拟化平台时,网络的虚拟化是一个非常重要的环节,如何保障网络的持续可用并 ...
- [MapReduce_add_1] Windows 下开发 MapReduce 程序部署到集群
0. 说明 Windows 下开发 MapReduce 程序部署到集群 1. 前提 在本地开发的时候保证 resource 中包含以下配置文件,从集群的配置文件中拷贝 在 resource 中新建 ...
- 【PAT】B1072 开学寄语(20 分)
代码注释应该很清晰 先存下违禁品,放到数组中,未使用map #include<cstdio> #include<string.h> int wupin[10],N,M; boo ...