一、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. Struts2(接受表单参数)请求数据自动封装和数据类型转换

    Struts2请求数据自动封装: (1)实现原理:参数拦截器 (2)方式1:jsp表单数据填充到action中的属性:        普通的成员变量,必须给set,get可以不给的.    注意点,A ...

  2. jQuery插件实践之轮播练习(一)

    所有文章搬运自我的个人主页:sheilasun.me 因为从来没写过jQuery插件,所以本文要通过一个轮播的例子,练习jQuery插件的写法. 新建插件文件 在讨论细节之前,先新建插件文件(当然也可 ...

  3. Here We Go(relians) Again HDU2722

    处理完输入就是很简单的一题  但是输入好难 勉强找到一种能看懂的... #include<iostream> #include<stdio.h> #include<str ...

  4. 098实战 Job的调度

    一:介绍 1.job调度 容量调度:Apache Hadoop的默认方式 公平调度:CDH版本的Hadoop的默认方式 2.公平调度 是一种资源分配方式,在yarn的整个生命周期中,所有的applic ...

  5. with 重写enter exit 方法

  6. 算法竞赛入门经典-训练指南(10881-Piotr's Ants)

    题目大意: 一根长度为L的木棍一堆蚂蚁爬,向左或向右,速度都为1,若两蚂蚁碰撞则同时转头(转身时间忽略不计),问T时间之后每只蚂蚁的位置: 输入:t,(t个样例),每个样例输入 L,T,n,接下来是n ...

  7. anaconda虚拟环境管理,从此Python版本不用愁

    1 引言 在前几篇博文中介绍过virtualenv.virtualenvwrapper等几个虚拟环境管理工具,本篇要介绍的anaconda也有很强大的虚拟环境管理功能,甚至相比virtualenv.v ...

  8. 【RAY TRACING THE REST OF YOUR LIFE 超详解】 光线追踪 3-1 蒙特卡罗 (一)

    今天起,我们就开始学习第三本书了 这本书主要讲的是蒙特卡罗渲染,以及相关的数学.术语概念等 这本书相较于前面两本有着什么不同,承担着什么样的任务,尚涉书未深,姑妄言之: 第一本书,带领我们初探光线追踪 ...

  9. Android应用开发-网络编程(二)

    Apache HttpClient框架 GET方式请求提交数据 1. 创建一个HttpClient HttpClient hc = new DefaultHttpClient(); 2. 创建一个Ht ...

  10. ClassLoader如何加载class?

    ClassLoader一个经常出现又让很多人望而却步的词,本文将试图以最浅显易懂的方式来讲解 ClassLoader,希望能对不了解该机制的朋友起到一点点作用. 要深入了解ClassLoader,首先 ...