Spring的第四天AOP之注解版
Spring的第四天AOP之注解版
在上一篇博客中,介绍了Spring的AOP的xml版本的使用,在这篇博客中,我将介绍一下,注解版的使用。
常用注解
注解 | 通知 |
---|---|
@After | 通知方法会在目标方法返回或抛出异常后调用 |
@AfterRetruening | 通常方法会在目标方法返回后调用 |
@AfterThrowing | 通知方法会在目标方法抛出异常后调用 |
@Around | 通知方法将目标方法封装起来 |
@Before | 通知方法会在目标方法执行之前执行 |
xml文件配置
<!-- 声明激活自动扫描组件功能,spring会去自动扫描com.weno下面或则自包下面的java文件,如果扫描到了@Component,@Controller等这些注解的类,就会把他们注册为bean,名字为类名但是首字母小写 -->
<context:component-scan base-package="com.weno.*"/>
<!-- Spring默认不支持使用@AspectJ的切面声明
使用如下配置可以使Spring发现那些配置@AspectJ的bean,并且创建代理,织入切面 -->
<aop:aspectj-autoproxy/>
通知类
//声明这是一个组件,使其能够被扫描到
@Component
//声明这是一个切面Bean
@Aspect
public class Advices {
//减少重复性工作
@Pointcut(value = "execution(* com.weno.pojo.User.msg())")
public void msg(){
}
@Before(value = "msg()")
public void beforeMethod(){
System.out.println("我正在切前置");
}
@AfterReturning(returning = "rvt",value = "msg()")
public void returnMethod(String rvt){
System.out.println("我正在切返回值"+rvt);
}
@After( value = "msg()")
public void afterMethod(){
System.out.println("我正在切后置");
}
@AfterThrowing(value = "execution(* com.weno.pojo.User.msg2())")
public void errorMethod(){
System.out.println("我正在切错误");
}
}
被切的类
import org.springframework.stereotype.Component;
@Component
public class User {
public String msg(){
System.out.println("我叫msg");
return "你是谁?";
}
public void msg2(){
System.out.println("被错误通知切");
throw new RuntimeException("我故意的错");
}
}
啊啊啊啊!恭喜IG
Spring的第四天AOP之注解版的更多相关文章
- Spring的第三天AOP之xml版
Spring的第三天AOP之xml版 ssm框架 spring AOP介绍 AOP(Aspect Oriented Programming),面向切面编程.它出来的目的并不是去取代oop,而是对它的 ...
- Spring 学习(四)--- AOP
问题 : AOP 解决的问题是什么 Spring AOP 的底层实现是什么 Spring AOP 和 AspectJ 的区别是什么 概述 在软件业,AOP为Aspect Oriented Progra ...
- 【Spring】每个程序员都使用Spring(四)——Aop+自定义注解做日志拦截
一.前言 上一篇博客向大家介绍了Aop的概念,对切面=切点+通知 .连接点.织入.目标对象.代理(jdk动态代理和CGLIB代理)有所了解了.理论很强,实用就在这篇博客介绍. 这篇博客中,小编向大家介 ...
- Spring 笔记(四)AOP
前言 横切关注点 使用 @AspectJ 定义切面. 同时还需要在配置类上应用 @EnableAspectJAutoProxy 注解,启用 AOP 自动代理.(不添加它的话,@AspectJ 注解的类 ...
- Spring深入浅出(四)AOP面向切面
面向切面编程--AOP AOP(Aspect Oriented Programming),程序员称之为面向切面编程,是Spring框架除了IOC之外的另一个核心概念. AOP是对OOP(面向对象编程) ...
- Spring框架(四)AOP面向切面编程
一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...
- Spring框架第四篇之基于注解的DI注入
一.说明 与@Component注解功能相同,但意义不同的注解还有三个: 1)@Repository:注解在Dao实现类上 2)@Service:注解在Service实现类上 3)@Controlle ...
- spring学习笔记四:AOP
AOP(Aspect Orient Programming),面向切面编程,是对面向对象编程OOP的一种补充 面向对象编程使用静态角度考虑程序的结构,而面向切面编程是从动态角度考虑程序运行过程 AOP ...
- SpringBoot系列(十三)统一日志处理,logback+slf4j AOP+自定义注解,走起!
往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件详解 SpringBoot系列(四)we ...
随机推荐
- Java-Selenium,获取下拉框中的每个选项的值,并随机选择某个选项
今天逛51testing,看见有人问这个问题.现在以Select标签为例. 1.首先看页面中的下拉框,如图: 2.F12查看页面源代码,如下 <select class="form-c ...
- Laravel-nestedset that base left and right values tree package
This is a Laravel 4-5 package for working with trees in relational databases. Laravel 5.5, 5.6, 5.7, ...
- struts2遇到的一个问题。
2018-09-12 好几年没配过struts2了,今天想用最新版的配一下,一直不成功,后来才知道,一堆红色输出里面有这样一句 ERROR StatusLogger No log4j2 configu ...
- 创建.NET core的守护进程
http://www.cnblogs.com/savorboard/p/dotnetcore-supervisor.html
- git stash错误小记
git出错小记 想要push代码,我们经常这样做. 1.查看状态 git status 2.隐藏本地编辑的新内容 git stash 3.拉远程的代码 git pull 这一步操作有的时候会报错,没有 ...
- 数据库-mysql语句-查
复习: 列类型: 数值类型: 20 '20' tinyint / smallint / int / bigint float / double / decimal(m,d) bool (TRU ...
- Linux学习---GCC编译过程
(一)GCC编译过程 预处理 cpp -o a.i a.c //生成预处理文件 等同于[gcc -E] //预处理为将宏定义(#define)等进行替换. 编译 /user/lib/gcc/i ...
- Hibernate 映射及查询
实体类和实体之间的关系:一对多,多对多 数据库设计:e_r 一个实体对象就是一个表格, 如果是1对多的关系,将多方的主键拿到1方做外键. 多对多:重新建立一张新的表格,将双方的主键拿到这里做外键 ...
- Merge Parts of Rar/7z Package in Linux
When file is too large, we will compress it and split it into several parts. Now Let me show you how ...
- Python之旅Day2 元组 字符串 字典 集合
元组(tuple) 元组其实跟列表差不多,也是存一组数,与列表相比,元组一旦创建,便不能再修改,所以又叫只读列表. 语法: names = ("Wuchunwei","Y ...