一、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的更多相关文章

  1. 菜鸟学习Spring——60s使用annotation实现简单AOP

    一.概述. AOP大家都知道切面编程,在Spring中annotation可以实现简单的AOP列子.下面还未大家介绍几个概念: Aspect 对横切性关注点的模块化. Advice 对横切性关注点的具 ...

  2. JavaEE开发之Spring中的依赖注入与AOP

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

  3. JavaEE开发之Spring中的依赖注入与AOP编程

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

  4. Spring中的面向切面编程(AOP)简介

    一.什么是AOP AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面 ...

  5. Spring中,关于IOC和AOP的那些事

    一.spring 的优点? 1.降低了组件之间的耦合性 ,实现了软件各层之间的解耦 2.可以使用容易提供的众多服务,如事务管理,消息服务等 3.容器提供单例模式支持 4.容器提供了AOP技术,利用它很 ...

  6. Spring AOP——Spring 中面向切面编程

    前面两篇文章记录了 Spring IOC 的相关知识,本文记录 Spring 中的另一特性 AOP 相关知识. 部分参考资料: <Spring实战(第4版)> <轻量级 JavaEE ...

  7. 详谈 Spring 中的 IOC 和 AOP

    这篇文章主要讲 Spring 中的几个点,Spring 中的 IOC,AOP,下一篇说说 Spring 中的事务操作,注解和 XML 配置. Spring 简介 Spring 是一个开源的轻量级的企业 ...

  8. 【转】Spring中事务与aop的先后顺序问题

    [原文链接] http://my.oschina.net/HuifengWang/blog/304188 [正文] Spring中的事务是通过aop来实现的,当我们自己写aop拦截的时候,会遇到跟sp ...

  9. Spring框架系列(4) - 深入浅出Spring核心之面向切面编程(AOP)

    在Spring基础 - Spring简单例子引入Spring的核心中向你展示了AOP的基础含义,同时以此发散了一些AOP相关知识点; 本节将在此基础上进一步解读AOP的含义以及AOP的使用方式.@pd ...

随机推荐

  1. jeecg Export导出图片到excel

    import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; impo ...

  2. 《JavaScript 高级程序设计》第三章:基本概念

    目录 语法 标识符 严格模式 关键字 保留字 变量 数据类型 运算符 表达式与语句 语法 "语法"指的是一门语言的书写风格,JavaScript 的语法风格很类似于 C 以及 Ja ...

  3. python访问百度地图接口并返回信息

    import urllib.parse import urllib.request data = urllib.parse.urlencode({'address': '广东省湛江市霞山区', 'ou ...

  4. HDU 3032 (SG打表找规律)

    题意: 有n堆石子,alice先取,每次可以选择拿走一堆石子中的1~x(该堆石子总数) ,也可以选择将这堆石子分成任意的两堆.alice与bob轮流取,取走最后一个石子的人胜利. 思路: 因为数的范围 ...

  5. POJ 1995 Raising Modulo Numbers (快速幂)

    题意: 思路: 对于每个幂次方,将幂指数的二进制形式表示,从右到左移位,每次底数自乘,循环内每步取模. #include <cstdio> typedef long long LL; LL ...

  6. asp.net core日志组件

    日志介绍 Logging的使用 1. appsettings.json中Logging的介绍 Logging的配置信息是保存在appsettings.json配置文件中的.因为之前介绍配置文件的时候我 ...

  7. 【noip模拟赛5】水流

    描述 全球气候变暖,小镇A面临水灾.于是你必须买一些泵把水抽走.泵的抽水能力可以认为是无穷大,但你必须把泵放在合适的位置,从而能使所有的水能流到泵里.小镇可以认为是N * M的矩阵.矩阵里的每个单元格 ...

  8. nowcoder提高组2题解

    T1 化一下试子就ok code #include<cstdio> #include<algorithm> inline long long read() { long lon ...

  9. BZOJ.1927.[SDOI2010]星际竞速(无源汇上下界费用流SPFA /最小路径覆盖)

    题目链接 上下界费用流: /* 每个点i恰好(最少+最多)经过一次->拆点(最多)+限制流量下界(i,i',[1,1],0)(最少) 然后无源汇可行流 不需要源汇. 注: SS只会连i',求SS ...

  10. 【11.8校内测试】【倒计时2天】【状压DP】【随机化?/暴力小模拟】

    Solution 数据范围疯狂暗示状压,可是一开始发现状态特别难受. 将每一层的奇偶性状压,预处理所有状态的奇偶性.每一层的输入代表的其实可以是下一层某个点可以被从这一层哪些点转移到. 所以枚举每个状 ...