http://zywang.iteye.com/blog/974226

http://www.cnblogs.com/garinzhang/p/java_spring_aop_aspect.html

第一种配置方法:使用@AspectJ标签

  1. 在配置文件中添加<aop:aspectj-autoproxy/>注解
  2. 创建一个Java文件,使用@Aspect注解修饰该类
  3. 创建一个方法,使用@Before、@After、@Around等进行修饰,在注解中写上切入点的表达式

说明:上述Java文件创建好后,需要将其在Spring的容器中进行声明,可以在配置文件中定义<bean/>节点,也可以使用@Component组件进行修饰

示例:

 1 import org.aspectj.lang.ProceedingJoinPoint;
2 import org.aspectj.lang.annotation.After;
3 import org.aspectj.lang.annotation.AfterThrowing;
4 import org.aspectj.lang.annotation.Around;
5 import org.aspectj.lang.annotation.Aspect;
6 import org.aspectj.lang.annotation.Before;
7 import org.springframework.stereotype.Component;
8
9 /**
10 * 基于注解的AOP日志示例
11 * @author ZYWANG 2011-3-24
12 */
13 @Component
14 @Aspect
15 public class AopLog {
16
17 //方法执行前调用
18 @Before("execution (* com.zywang.services.impl.*.*(..))")
19 public void before() {
20 System.out.println("before");
21 }
22
23 //方法执行后调用
24 @After("execution (* com.zywang.services.impl.*.*(..))")
25 public void after() {
26 System.out.println("after");
27 }
28
29 //方法执行的前后调用
30 @Around("execution (* com.zywang.services.impl.*.*(..))")
31 public Object around(ProceedingJoinPoint point) throws Throwable{
32 System.out.println("begin around");
33 Object object = point.proceed();
34 System.out.println("end around");
35 return object;
36 }
37
38 //方法运行出现异常时调用
39 @AfterThrowing(pointcut = "execution (* com.zywang.services.impl.*.*(..))",throwing = "ex")
40 public void afterThrowing(Exception ex){
41 System.out.println("afterThrowing");
42 System.out.println(ex);
43 }
44 }

上面这段代码中多次使用了重复的切入点,这种情况下,可以使用@Pointcut标注,来修改一个切入点方法(这个方法不需要参数和方法体),然后就可以在@Before等标注中引用该方法作为切入点,示例如下:

 1 import org.aspectj.lang.ProceedingJoinPoint;
2 import org.aspectj.lang.annotation.Around;
3 import org.aspectj.lang.annotation.Aspect;
4 import org.aspectj.lang.annotation.Before;
5 import org.aspectj.lang.annotation.Pointcut;
6 import org.springframework.stereotype.Component;
7
8 /**
9 * 基于注解的AOP日志示例
10 * @author ZYWANG 2011-3-24
11 */
12 @Component
13 @Aspect
14 public class AopLog {
15
16 @Pointcut("execution (* com.iflysse.school.services.impl.*.*(..))")
17 public void pointcut(){}
18
19 //方法执行前调用
20 @Before("pointcut()")
21 public void before() {
22 System.out.println("before");
23 }
24
25 //方法执行的前后调用
26 @Around("pointcut()")
27 public Object around(ProceedingJoinPoint point) throws Throwable{
28 System.out.println("begin around");
29 Object object = point.proceed();
30 System.out.println("end around");
31 return object;
32 }
33 }

第二种配置方法:基于配置文件的配置

  1. 创建一个Java文件,并指定一个用于执行拦截的方法,该方法可以有0个或多个参数
  2. 在Spring配置文件中注册该Java类为一个Bean
  3. 使用<aop:config/>、<aop:aspect/>等标签进行配置

示例:

Java文件

 1 import org.aspectj.lang.ProceedingJoinPoint;
2
3 /**
4 * 基于配置文件的AOP日志示例
5 * @author ZYWANG 2011-3-24
6 */
7 public class AopLog {
8
9 //方法执行的前后调用
10 public Object runOnAround(ProceedingJoinPoint point) throws Throwable{
11 System.out.println("begin around");
12 Object object = point.proceed();
13 System.out.println("end around");
14 return object;
15 }
16
17 }

使用第二种方式的AOP配置,在Eclipse(有SpringIDE插件)中被拦截到的方法中有标识显示

以上配置基于Spring 3.0.5 进行设置,参考其《Reference Documentation》

Spring3.0中的AOP配置方法的更多相关文章

  1. WCF学习之旅—WCF4.0中的简化配置功能(十五)

    六 WCF4.0中的简化配置功能 WCF4.0为了简化服务配置,提供了默认的终结点.绑定和服务行为.也就是说,在开发WCF服务程序的时候,即使我们不提供显示的 服务终结点,WCF框架也能为我们的服务提 ...

  2. AntiXSS v4.0中Sanitizer.GetSafeHtmlFragment等方法将部分汉字编码为乱码的解决方案

    AntiXSS v4.0中Sanitizer.GetSafeHtmlFragment等方法将部分汉字编码为乱码的解决方案 以下代码为asp.net环境下,c#语言编写的解决方案.数据用Dictiona ...

  3. CentOS-7.0.中安装与配置Tomcat-7的方法

    安装说明 安装环境:CentOS-7.0.1406安装方式:源码安装 软件:apache-tomcat-7.0.29.tar.gz 下载地址:http://tomcat.apache.org/down ...

  4. spring3.0.5的aop使用

    spring3.0.5开始支持jpa2.0了,但是最近笔者在使用他的的时候发现了3.0.5的包与2.5.5相比,有所精简.其他外部的包,我们需要自己下载. AOP必须的spring包 org.spri ...

  5. django 2.0 中URL的include方法使用分析

    一.问题出现: 在使用Django2.0,配置全局URL时,希望指向某个APP的URL,配置如下: from django.contrib import admin from django.conf. ...

  6. AFNetworking 3.0中调用[AFHTTPSessionManager manager]方法导致内存泄漏的解决办法

    在使用AFNetworking3.0框架,使用Instruments检查Leaks时,检测到1000多个内存泄漏的地方,定位到 [AFHTTPSessionManager manager] 语句中,几 ...

  7. Vue2.0中的路由配置

    Vue2.0较Vue1.0,路由有了较大改变.看一下Vue2.0中的路由如何配置: 步骤一: 在main.js文件中引入相关模块以及组件及实例化vue对象配置选项路由及渲染App组件 默认设置如下: ...

  8. vue3.0中的双向数据绑定方法

    熟悉vue的人都知道在vue2.x之前都是使用object.defineProperty来实现双向数据绑定的 而在vue3.0中这个方法被取代了 1. 为什么要替换Object.definePrope ...

  9. VS2015ASP.NET MVC5项目中Spring.NET配置方法(超详细)

    首先,在ASP.NET MVC5项目右键,如下图所示,选择“管理Nuget程序包...” 然后,在弹出的页面的搜索框中输入“spring.web”,在返回结果中选择Spring.Web和Spring. ...

  10. Spring3数据源的6种配置方法

    在Spring3中,配置DataSource的方法有五种. 第一种:beans.xml <bean id="dataSource" class="org.apach ...

随机推荐

  1. 面试官:一个 SpringBoot 项目能处理多少请求?(小心有坑)

    你好呀,我是歪歪. 这篇文章带大家盘一个读者遇到的面试题哈. 根据读者转述,面试官的原问题就是:一个 SpringBoot 项目能同时处理多少请求? 不知道你听到这个问题之后的第一反应是什么. 我大概 ...

  2. 私网部署DNS(BIND)笔记

    准备工作 下载 yum install -y bind bind-utils包含dig.nslookup等调试命令,非必须. yum install -y bind-utils 防火墙 firewal ...

  3. 解决:ValueError: Cannot mask with non-boolean array containing NA / NaN values

    错误原因:这里就是说,分组这一列里面,包含了非字符串的内容,比如数字.因为 .str.contains 的使用就要求这个字段必须是字符串,不能掺杂数字的. 解决方案: # 包含对应关系的所有行 dat ...

  4. 【教程】青少年CTF机器人使用教程

    前言 本期教程适用于版本号为2.0.1-Beta的青少年CTF机器人,其他版本可能与当前版本不同. 由于之前版本的机器人重构,所以我们细化了本次的机器人逻辑,并且对机器人的功能进行了一些升级. 机器人 ...

  5. WorkManager的用法

    一.WorkManager的作用 绝大部分应用程序都有后台执行任务的需求,根据需求的不同,Android为后台任务提供了多种解决方案,如JobShedule,Loader,Service等.如果这些a ...

  6. quarkus数据库篇之三:单应用同时操作多个数据库

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 一个应用同时连接多个数据库进行操作,这是常见 ...

  7. 使用C#创建安装Windows服务程序(最全教程)

    开发语言:C# 开发环境: Visual Studio 2022 微软官方文档:https://learn.microsoft.com/zh-cn/dotnet/framework/windows-s ...

  8. 使用 Rancher 安装 K8s 集群

    舞台环境 Ubuntu 22.04.2 LTS Docker 24.0.2 2GB RAM或者更多 CPU 2核心或者更多 Rancher 2.6.9 测试环境中,我准备了两台 Ubuntu 服务器, ...

  9. 对称加密 vs 非对称加密

    计算机网络在给我们带来便利的同时,也存在很多安全隐患,比如信息伪造,病毒入侵,端点监听,SQL 注入等,给我们日常生活造成很严重的影响. 那么这篇文章我就跟大家聊聊常见的网络安全隐患,只作为科普,不能 ...

  10. Microsoft Build 2021第二天

    C++20 Ranges are complete in Visual Studio 2019 version 16.10 https://devblogs.microsoft.com/cppblog ...