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 ...
随机推荐
- python 全栈开发,Day17(初识面向对象)
一.引子 第一次参加工作,进入了一家游戏公司,公司需要开发一款游戏<人狗大战>一款游戏,首先得把角色和属性定下来. 角色有2个,分别是人和狗属性如下:人 :昵称.性别.血.攻击力狗 :名字 ...
- mysql的基本演示
数据库需要配置 cmd打开doc窗口 net start mysql:启动数据库 net stop mysql :停止数据库 表的定义:列 行 主键
- Emmet Cheat Sheet(Sublime编辑)
快捷创建html标签 官网的Emmet Cheat Sheet :http://docs.emmet.io/cheat-sheet/ https://files.cnblogs.com/files/t ...
- JS高级 - 面向对象3(面向过程改写面向对象)
改写: 1.前提:所有东西都在 onload 里 2.改写:不能有函数嵌套,可以有全局变量 onload --> 构造函数 全局变量 --> 属性 函数 --> 方法 4.改错: t ...
- Hibernate的CRUD以及junit测试
Hibernate的CRUD以及junit测试 1:第一步创建动态工程引包,省略. 2:第二步,创建数据库和数据表,省略. 3:第三步,创建实体类,如User.java,源码如下所示: 对于实体类,一 ...
- [JSOI2009]等差数列
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1558 题解: 考虑这么用线段树进行维护,由于他有区间修改等差数列 很容易想到可以用差分数组来维 ...
- Python debug 调试;
F9:执行跳到下一个断点 F8:执行下一步 F7:进入函数
- prometheus简介
一.prometheus简介 1.1 什么是prometheus? Prometheus是一个开源监控系统,它前身是SoundCloud的警告工具包.从2012年开始,许多公司和组织开始使用Prome ...
- 《Android进阶之光》--Dagger2
No1: Project的build.gradle文件添加 buildscript{ dependencies{ ...classpath 'com.neenbedankt.gradle.plugin ...
- poj2184 Cow Exhibition【01背包】+【负数处理】+(求两个变量的和最大)
题目链接:https://vjudge.net/contest/103424#problem/G 题目大意: 给出N头牛,每头牛都有智力值和幽默感,然后,这个题目最奇葩的地方是,它们居然可以是负数!! ...