例子下载

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. Java的动手动脑(四)

    日期:2018.10.18 星期四 博客期:019 Part1:回答为啥会报错 答案:当然会报错啦!因为平常的编程过程中,系统会对我们写的类自动生成一个默认无参形式的构造方法,类似于C++中的体制!这 ...

  2. eclipse maven .jar中没有主清单属性

    报错环境: windows系统eclipse maven 打包jar包后, 运行 java -jar 报错 E:\My_java\mysql\target>java -jar original- ...

  3. 安装lrzsz 实现windows与linux之间文件互传

    环境:CentOS7.4 执行命令安装: [root@linuxhg01 www]# yum install lrzsz rz // Windows 上传到 linux [root@linuxhg01 ...

  4. HTML&javaSkcript&CSS&jQuery&ajax-XSS

    一.CSS  标题隐藏 1. <sytle>h1.hidden {visibility: hidden;} </style> <body> <h1>这是 ...

  5. C语言访问一个链接

    示例代码1: # include <Windows.h> int main(){ system("start http://""www.baidu.com&q ...

  6. AI-序列化-查-做接口

    序列化最终代码(下边的可以不看) from rest_framework.views import APIView from rest_framework import serializers fro ...

  7. java数据

    因为曾经干了啥事儿,才印象特别深刻. 将byte存入String的后果 String res = ""; res += (char) 0xc3; byte[] bytes = re ...

  8. ConfigurationManager 类的使用

    一.引用 命名空间:   System.Configuration程序集:  System.Configuration(位于 System.Configuration.dll) 二.示例 1.读取.增 ...

  9. Ajax增删改查-----------删 改

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Mac OS X10.8.3-bash基本命令失效后的修复

    -bash基本命令都失败了. 比如: -bash: ls :command not found   顿时心都凉了. 想要找到.bash_profile文件也不是那么容易的.     step1. 在t ...