例子下载

beans.xml

  首先,在配置文件配置好下面的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
>
<context:annotation-config />
<context:component-scan base-package="com.bjsxt"/>
<aop:aspectj-autoproxy />
</beans>

  其中<context:annotation-config />为声明使用annotation部分,<context:component-scan base-package="com.bjsxt"/>会使得程序运行时进行对com.bjsxt包及子包的扫描操作,<aop:aspectj-autoproxy />对于AOP的annotation来说最为主要,标志着可以使用AOP的annotion来进行操作。

Aspect

package com.bjsxt.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Aspect
@Component
public class LogInterceptor {
@Pointcut("execution(public * com.bjsxt.service..*.add(..))")
public void myMethod(){}; @Around("myMethod()")
public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("method around start");
pjp.proceed();
System.out.println("method around end");
} @Before("myMethod()")
public void before() {
System.out.println("method before");
}
}

@Aspect注释标志着此类为一个切面类。

@Pointcut作为切入点,@Pointcut("execution(public * com.bjsxt.service..*.add(..))")说明在调用com.bjsxt.service下面的包或其子包的add方法(任意参数)时调用下面的方法。

@Around("myMethod()")说明在myMethod()方法调用前运行一次下面的方法,在myMethod()方法调用结束后再调用一次下面的方法。

@Before("myMethod()")说明在myMethod()方法调用之前调用下面的方法。

spring_AOP_annotation的更多相关文章

  1. Spring AOP—注解配置方法的使用

    Spring除了支持Schema方式配置AOP,还支持注解方式:使用@AspectJ风格的切面声明. 1 启用对@AspectJ的支持 Spring默认不支持@AspectJ风格的切面声明,为了支持需 ...

随机推荐

  1. css 选择器/table属性/type 属性

    css   style样式---要写单位px style=" width: 200px; height :300px;" ;是结束符              

  2. 关于npm 淘宝镜像 以及package.json里包的更新

    1.淘宝镜像的设置 npm config set registry https://registry.npm.taobao.org npm config set disturl https://npm ...

  3. WampServer & XAMPP Configure with MariaDB and MySQL

    第一部分补上次的一个问题 1.WampServer 3不支持的硬件格式 FAT3和 exFAT 他只能工作在NTFS的格式硬盘上. 不能在Windows XP上运行. 安装 WampServer 必须 ...

  4. Winhex数据恢复学习笔记(三)

    上次对文件系统进行简单的分析,这次就文件的镜像功能做一介绍 1.首先镜像的概念:镜像就是数据的副本,是原来数据在相同位置上以相同的排列模式生成的拷贝,所以镜像可以用来还原原始数据,代替原始数据工作,镜 ...

  5. Distance

    1191: Distance 时间限制: 1 Sec  内存限制: 32 MB 题目描述 There is a battle field. It is a square with the side l ...

  6. 论文阅读笔记二十六:Fast R-CNN (ICCV2015)

    论文源址:https://arxiv.org/abs/1504.08083 参考博客:https://blog.csdn.net/shenxiaolu1984/article/details/5103 ...

  7. 基于Redis的分布式锁到底安全吗

    http://zhangtielei.com/posts/blog-redlock-reasoning.html

  8. javascript 正则表达式(十)

    一.什么是正则 在常见的字符串检索和替换中,我们需要提供一种模式表示检索或替换的规则.正则表达式使用单个字符串来描述.匹配一系列符合某个句法规则的字符串. abc [a-z]{4} \d\d\d 二. ...

  9. tjoi2018

    1.[TJOI2018]数学计算 傻逼题 会发现符合线段树分治的特点 每个数的操作范围都是连续的 然后就等于区间修改了 #include <bits/stdc++.h> using nam ...

  10. 【BZOJ2298】[HAOI2011]problem a

    题解: 虽然也是个可以过得做法...但又没有挖掘到最简单的做法... 正解是发现这个东西等价于求不相交区间个数 直接按照右端点排序,然后贪心就可以O(n)过了 而我的做法是按照a排序(其实我是在模拟这 ...