[10] AOP的注解配置


1、关于配置文件

首先在因为要使用到扫描功能,所以xml的头文件中除了引入bean和aop之外,还要引入context才行:
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">...</beans>    
x
11
1
<?xml version="1.0" encoding="UTF-8"?>
2
<beans xmlns="http://www.springframework.org/schema/beans"
3
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
       xmlns:context="http://www.springframework.org/schema/context"
5
       xmlns:aop="http://www.springframework.org/schema/aop"
6
       xsi:schemaLocation="http://www.springframework.org/schema/beans
7
       http://www.springframework.org/schema/beans/spring-beans.xsd
8
       http://www.springframework.org/schema/aop
9
       http://www.springframework.org/schema/aop/spring-aop.xsd
10
       http://www.springframework.org/schema/context
11
       http://www.springframework.org/schema/context/spring-context.xsd">
12
    
13
    ...
14
    
15
</beans>    

既然使用注解,那么在配置文件中需要开启扫描配置以注册bean组件;同时Spring中使用了aspectj包的@Aspect注解标注当前组件为切面,所以同时还需要在配置文件中配置实用aspectj的自动代理模式。如下:
<!-- 开启bean组件扫描 -->
<context:component-scan base-package="dulk.learn"></context:component-scan>
<!-- 启用自动代理 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
1
1
<!-- 开启bean组件扫描 -->
2
<context:component-scan base-package="dulk.learn"></context:component-scan>
3
<!-- 启用自动代理 -->
4
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

2、AOP的注解配置

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
1
49
1
import org.aspectj.lang.JoinPoint;
2
import org.aspectj.lang.ProceedingJoinPoint;
3
import org.aspectj.lang.annotation.After;
4
import org.aspectj.lang.annotation.AfterReturning;
5
import org.aspectj.lang.annotation.AfterThrowing;
6
import org.aspectj.lang.annotation.Around;
7
import org.aspectj.lang.annotation.Aspect;
8
import org.aspectj.lang.annotation.Before;
9
import org.springframework.stereotype.Component;
10
11
12
@Component
13
@Aspect
14
public class Section {
15
16
    @Before("execution(* dulk.learn..*.*(..))")
17
    public void doBefore(JoinPoint point) {
18
        System.out.println("doBefore");
19
    }
20
21
    @AfterReturning(pointcut = "execution(* dulk.learn..*.*(..))", returning = "ret")
22
    public void doAfterReturning(JoinPoint point, Object ret) {
23
        System.out.println("doAfterReturning");
24
        System.out.println("The returning obj is " + ret);
25
    }
26
27
    @AfterThrowing(pointcut = "execution(* dulk.learn..*.*(..))", throwing = "e")
28
    public void doThrow(JoinPoint point, Throwable e) {
29
        System.out.println("doThrow");
30
    }
31
32
    @After("execution(* dulk.learn..*.*(..))")
33
    public void doAfter() {
34
        System.out.println("doAfter");
35
    }
36
37
    @Around("execution(* dulk.learn..*.*(..))")
38
    public void doAround(ProceedingJoinPoint point) {
39
        System.out.println("doAround-dobefore");
40
        try {
41
            Object obj = point.proceed();
42
            System.out.println("doAround-doAfterReturning");
43
        } catch (Throwable throwable) {
44
            System.out.println("doAround-doThrow");
45
        }
46
        System.out.println("doAround-doAfter");
47
    }
48
49
}


转载于:https://www.cnblogs.com/deng-cc/p/9225047.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/266806.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

惠新宸php教程_惠新宸:PHP在百度的应用现状及展望

惠新宸&#xff0c;百度PHP高级顾问,年二十有八&#xff0c;好追根究底&#xff0c;有不良嗜好, 幸性本善。乙酉年识互联网&#xff0c;丁亥年入雅虎&#xff0c;翌年入百度。虽性好安稳&#xff0c;然经变无数&#xff0c;唯常叹"人生&#xff0c;菠菜汤尔"。大家好…

html5结合css3,html5八大技术特征,与CSS3完美结合

html5已经问世一段时间&#xff0c;比起之前的版本又添加了不少标签和属性&#xff0c;这让开发者以后的开发会越来越轻松&#xff0c;代码越来越规范&#xff0c;下面来说说它的八大技术特征。一、本地存储在应用缓存&#xff0c;本地存储&#xff0c;索引数据库和文件应用程序…

Java程序通过批处理文件定时执行

echo off :begin echo Wscript.Sleep Wscript.Arguments(0) * 1000>Delay.vbs echo 廷时60秒 Delay.vbs 60 setlocal enabledelayedexpansion set classpath.;./bin for %%f in (./lib/*.jar) do ( set onefile%%f set classpath!classpath!;./lib/!onefile! ) ec…

安装secureCRT提示sorry的解决办法

摘自&#xff1a;你的secureCRT还在sorry吗&#xff1f; 作者&#xff1a;丶PURSUING 发布时间&#xff1a; 2021-03-12 08:21:37 网址&#xff1a;https://blog.csdn.net/weixin_44742824/article/details/114684721?spm1001.2014.3001.5502 此篇专门针对于学习群中部分小伙伴…

华南农业大学计算机专业学硕20,2020年华南农业大学计算机应用技术考研经验分享...

2020年华南农业大学计算机应用技术考研经验分享大家好&#xff0c;我是育明考研小赵老师关于华南农业大学计算机应用技术考研经验信息汇总&#xff0c;请参考华南农业大学是全国重点大学,广东省和农业部共建的“211工程”大学,广东省高水平大学重点建设高校。校园坐落在素有“花…

手游无限多开器安卓版_无限多开版!使用叶子猪手游模拟器玩大话西游手游教程...

无限多开版&#xff01;使用叶子猪手游模拟器玩大话手游教程&#xff0c;本期小编给大家分享了&#xff0c;很多玩家都知道本次首测主要面向的对象是安卓客户端&#xff0c;但是对于果粉的朋友来说&#xff0c;ios版本手游是玩不了的&#xff0c;但是不用担心&#xff0c;我们还…

第三十三讲:tapestry表单组件详解之Label

2019独角兽企业重金招聘Python工程师标准>>> http://flywind.org/newtechnologydetail/168 转载于:https://my.oschina.net/flywind/blog/82633

c语言基础回顾 —— 其他知识点

参考&#xff1a;c语言基础回顾 —— 其他知识点 作者&#xff1a;丶PURSUING 发布时间&#xff1a; 2021-03-11 14:48:59 网址&#xff1a;https://blog.csdn.net/weixin_44742824/article/details/114666007 本文为学习笔记&#xff0c;结合课程内容以及下列文章 在 printf 里…

WebFlux02 SpringBoot WebFlux项目骨架搭建

1 环境搭建 1.1 版本说明 jdk -> 1.8 maven -3.5 springboot -> 2.0.3 开发工具 -> IDEA 1.2 创建项目 利用 IDEA 或者 start.spring.io 创建一个SpringBoot项目 1.2.1 选择依赖 1.2.2 pom.xml 依赖 <?xml version"1.0" encoding"UTF-8"?&g…

计算机专业 拒绝清华,高分拒绝清华北大怎么回事 为什么会拒绝

原标题&#xff1a;高分拒绝清华北大怎么回事 为什么会拒绝今年有一位来自云南的考生在高考的时候考了712分&#xff0c;成功超过清华北大的投档线&#xff0c;要是换做别人都应该会很高兴&#xff0c;终于能够上万人瞩目的名牌大学了&#xff0c;但是这位学生却拒绝了北大清华…

linux top 命令可视化_Linux top命令使用详解:显示或管理执行中的程序

top命令可以实时动态地查看系统的整体运行情况&#xff0c;是一个综合了多方信息监测系统性能和运行信息的实用工具。通过top命令所提供的互动式界面&#xff0c;用热键可以管理。语法top(选项)选项-b&#xff1a;以批处理模式操作&#xff1b;-c&#xff1a;显示完整的治命令&…

新浪微博2-找到用户的accessToken

要想使用新浪微博的API&#xff0c;首先就必须获得用户的accessToken&#xff0c;否则什么都干不了。 在AuthorizeActivity中 String token values.getString("access_token"); String expires_in values.getString("expires_in"); …

五大板块(1)—— 数组的定义,赋值与应用

参考&#xff1a;五大板块&#xff08;1&#xff09;—— 数组的定义&#xff0c;赋值与应用 作者&#xff1a;丶PURSUING 发布时间&#xff1a; 2021-03-18 16:00:05 网址&#xff1a;https://blog.csdn.net/weixin_44742824/article/details/114981166 目录数组的定义及访问完…

计算机 用户创建失败,电脑用户加载错误,无法创建新的配置文件的解决方法...

这种无法创建新的配置文件&#xff0c;所以本地计算机中没有配置文件的用户都无法登录&#xff0c;而有配置文件的用户就可以登录。是因为创建新的用户配置文件时是把系统分区 Documents and Settings文件夹下的Default User文件夹(默认隐藏&#xff0c;需要设置显示隐藏文件夹…

css3-6 表格如何设置样式和定位样式是什么

css3-6 表格如何设置样式和定位样式是什么 一、总结 一句话总结&#xff1a;css可以解决所有属性设置的样式。 1、表格如何设置样式&#xff1f; css样式可以解决一切问题&#xff0c;没必要在表格上面加属性来设置样式。 7 table{8 width:1000px; 9 bor…

python flask 分页前后端分离_flask展示pyecharts图表前后端分离的问题

我按照pyecharts的官网案例学习flask&#xff0c;其中有段flask前后端分离的示例。官方教程&#xff1a;https://pyecharts.org/#/zh-cn...我用前后端分离展示柱状图、饼图都没问题&#xff0c;但把pyecharts的图形函数换成地理图表&#xff0c;就展示不出来&#xff0c;请问是…

Django学习笔记2:一个简单的开发实例

Technorati 标签: Python,Django目标&#xff1a;通过开发一个简单的Todo管理应用&#xff0c;熟悉Django的基本概念、和使用。 运行环境 Windows Vista Python 2.7 Django 1.5 准备工作 * 创建工程 python C:\Python27\Lib\site-packages\django\bin\django-admin.py startp…

五大板块(2)—— 指针

参考&#xff1a;五大板块&#xff08;2&#xff09;—— 指针 作者&#xff1a;丶PURSUING 发布时间&#xff1a; 2021-03-18 16:01:22 网址&#xff1a;https://blog.csdn.net/weixin_44742824/article/details/114981482 本文为学习笔记&#xff0c;整合课程内容及文章如下&…

基于word API 创建的可以打开word的自定义控件

代码基本框架来源&#xff1a; CSDN 博主&#xff1a;zjlovety的专栏 地址&#xff1a;https://blog.csdn.net/zjlovety/article/details/24463117 public partial class WinWordViewer : UserControl { public WinWordViewer() { Initializ…

台式计算机机箱都一样吗,别以为组装台式机很简单,机箱选择大有学问

原标题&#xff1a;别以为组装台式机很简单&#xff0c;机箱选择大有学问曾经组装台式机的时候对于机箱完全没有在意&#xff0c;总觉得随便选择一款就可以了&#xff0c; 而且是挑便宜的选择&#xff0c;而那些机箱有便宜也有贵&#xff0c;其实里面的设计完全不一样&#xff…