Spring中通过Annotation来实现AOP
一、Spring配置文件
<!--通过@AspectJ注解的形式来使用Spring AOP,强制使用CGLIB代理-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
Spring默认不支持@AspectJ风格的切面声明,通过aop命名空间的<aop:aspectj-autoproxy/>声明自动为spring容器中那些配置@Aspect切面的bean创建代理,织入切面。proxy-target-class="true",强制使用CGLIB代理(没有接口也可以生成代理类,JDK代理是必须有接口的)。
二、定义方面类(aspect)
package aop; import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; /**
* 定义一个方面(Aspect),Maven需要引用spring-aspects
* 这个方面(Aspect)包括了多个增加处理(通知,比如:前置、后置、后置返回、异常等)
*
* @author xfyou
* @date 2018/9/29
*/
@Aspect
@Component
public class Advice { @Before(value = "execution(* bean.*.*(..)) && args(arg0)", argNames = "arg0")
public void beforeAdvice(String arg0) {
System.out.println("arg0=" + arg0);
} @AfterReturning(pointcut = "execution(* bean.*.*(..))", returning = "retVal")
public void afterReturnAdvice(String retVal) {
System.out.println("fristName=" + retVal);
} @AfterThrowing(pointcut = "execution(* bean.*.*(..))", throwing = "ex")
public void afterThrowingAdvice(Exception ex) {
System.err.println(ex.getMessage());
} }
上面分别定义了三个“前置”、“后置返回”、“异常”增强处理(或者叫着:通知)方法。通过@Aspect标注的Advice类必须注册到Spring容器中才能使用,所以我们这里使用到了一个@Component注解让Spring能够自动扫描并注册到Spring容器中。
三、测试
package bean; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author xfyou
* @date 2018/9/6
*/
public class Test { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml");
Person person = context.getBean(Person.class);
person.setFirstName("frank");
person.getFirstName();
person.test();
} }
输出如下:
beforeAdvice,arg0=frank
afterReturnAdvice,retVal=frank
afterThrowingAdvice,exception:RuntimeException
Exception in thread "main" java.lang.RuntimeException: RuntimeException
at bean.Person.test(Person.java:18)
at bean.Person$$FastClassBySpringCGLIB$$40dc9373.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
java动态代理:
参考:https://blog.csdn.net/luanlouis/article/details/24589193
Spring中通过Annotation来实现AOP的更多相关文章
- 菜鸟学习Spring——60s使用annotation实现简单AOP
一.概述. AOP大家都知道切面编程,在Spring中annotation可以实现简单的AOP列子.下面还未大家介绍几个概念: Aspect 对横切性关注点的模块化. Advice 对横切性关注点的具 ...
- JavaEE开发之Spring中的依赖注入与AOP
上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...
- JavaEE开发之Spring中的依赖注入与AOP编程
上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...
- Spring中的面向切面编程(AOP)简介
一.什么是AOP AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面 ...
- Spring中,关于IOC和AOP的那些事
一.spring 的优点? 1.降低了组件之间的耦合性 ,实现了软件各层之间的解耦 2.可以使用容易提供的众多服务,如事务管理,消息服务等 3.容器提供单例模式支持 4.容器提供了AOP技术,利用它很 ...
- Spring AOP——Spring 中面向切面编程
前面两篇文章记录了 Spring IOC 的相关知识,本文记录 Spring 中的另一特性 AOP 相关知识. 部分参考资料: <Spring实战(第4版)> <轻量级 JavaEE ...
- 详谈 Spring 中的 IOC 和 AOP
这篇文章主要讲 Spring 中的几个点,Spring 中的 IOC,AOP,下一篇说说 Spring 中的事务操作,注解和 XML 配置. Spring 简介 Spring 是一个开源的轻量级的企业 ...
- 【转】Spring中事务与aop的先后顺序问题
[原文链接] http://my.oschina.net/HuifengWang/blog/304188 [正文] Spring中的事务是通过aop来实现的,当我们自己写aop拦截的时候,会遇到跟sp ...
- Spring框架系列(4) - 深入浅出Spring核心之面向切面编程(AOP)
在Spring基础 - Spring简单例子引入Spring的核心中向你展示了AOP的基础含义,同时以此发散了一些AOP相关知识点; 本节将在此基础上进一步解读AOP的含义以及AOP的使用方式.@pd ...
随机推荐
- 步步为营-42-通过DataAdapter实现增删查改
说明:通过DataAdapter或者dataset连接数据库,实现对数据增删改查操作. 以前写过一篇步步为营-23-通过GridView实现增删改 1:SqlDataAdapter DataTabl ...
- 如何用命令将本地项目上传到git,git基本使用
1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库 git init 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点 ...
- ORACLE 计算时间相减间隔
在Oralce中我发现有add_months函数,加天数N可以用如下方法实现,select sysdate+N from dual 在Oralce中我发现有add_months函数,加天数N可以用如下 ...
- JQuery中的Ajax(六)
一:Ajax请求jQuery.ajax(options) load(url,[data],[callback])jQuery.get(url,[data],[callback]) jQuery.get ...
- 浅析C语言的变量
参考资料 寄存器变量 用register声明的变量是寄存器变量,是存放在CPU的寄存器里的.而我们平时声明的变量是存放在内存中的.虽说内存的速度已经很快了,不过跟寄存器比起来还是差得远. 寄存器变量和 ...
- Oracle回收站的清理方法
http://blog.itpub.net/18841027/viewspace-1057765/
- bzoj1026
题意: windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间,包括A和B,总共有多少个windy数? 数据范围 A,B≤ ...
- mybatis的sqlSessionFactory的加载过程
使用过SSM的框架的都知道mybatis这个持久层框架,今天小编就来简单说说这个框架的核心工厂类sqlSessionFactory的加载过程,一般的SSM框架我们都会在spring的applicati ...
- 初探和实现websocket心跳重连(npm: websocket-heartbeat-js)
提示:文章最下方有仓库地址 心跳重连缘由 websocket是前后端交互的长连接,前后端也都可能因为一些情况导致连接失效并且相互之间没有反馈提醒.因此为了保证连接的可持续性和稳定性,websocket ...
- jackson实现java对象转支付宝/微信模板消息
一.支付宝消息模板大致长这样 { "to_user_id": "", "telephone": "xxxxx", &qu ...