码迷,mamicode.com
首页 > 编程语言 > 详细

Spring AOP 前置通知和后置通知

时间:2016-01-30 13:47:52      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

  • 加入JAR包:                      

技术分享

 

  • 在配置文件中加入AOP 的命名空间

技术分享

 

  • 基于注解的注解的方式,配置文件如下:

beans-aop-helloworld.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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    <!-- 配置自动扫描包 -->
    <context:component-scan base-package="spring.aop.helloworld"></context:component-scan>
    
    <!-- 是AspectJ注解起作用: 自动为匹配的类生成代理对象 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

 

  • 把横切关注点的代码抽象到切面的类中
    • 切面首先是一个IOC中的bean, 即加入@Component注解
    • 切面还需要加入@Aspect注解:
  • 在类中声明各种通知:
    • 申明一个方法
    • 在方法前加入@Before注解
  • 可以在通知方法中申明一个类型为JointPoint 的参数,然后就鞥访问链接细节。如方法名称和参数值。
  • 具体代码如下:
    • ArithmeticCalculator.java
package spring.aop.helloworld;

public interface ArithmeticCalculator {
	public int add(int i, int j);

	public int sub(int i, int j);

	public int mul(int i, int j);

	public int div(int i, int j);
}

 

    • ArithmeticCalculatorImpl.java
package spring.aop.helloworld;

import org.springframework.stereotype.Component;

@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

	@Override
	public int add(int i, int j) {
		int result = i + j;
		return result;
	}

	@Override
	public int sub(int i, int j) {
		int result = i - j;
		return result;
	}

	@Override
	public int mul(int i, int j) {
		int result = i * j;
		return result;
	}

	@Override
	public int div(int i, int j) {
		int result = i/j;
		return result;
	}
}

 

    • LoggingAspect.java

package spring.aop.helloworld;

import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

//把这个类声明为一个切面:需要把该类放入到IOC容器中, 声明为一个切面
@Aspect
@Component
public class LoggingAspect {
//声明该方法是一个前置通知:在目标方法开始之前执行
@Before("execution(public int spring.aop.helloworld.ArithmeticCalculator.*(int, int))")
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The " + methodName + " begins " + " args is: " + args);
}

//后置通知:在目标方法发生之后执行(不论这个方法是否发生了异常)
//在后置通知中海不能访问目标方法执行的结果
@After("execution(public int spring.aop.helloworld.ArithmeticCalculator.*(int, int))")
public void afterMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The " + methodName + " ends " + methodName + " args is: " + args);
}
}

 

  • 注释:关于execute函数的匹配方法有多种写法,具体如下: 
//表示任意返回值
execution(public * spring.aop.helloworld.ArithmeticCalculator.*(int, int))

//表示包里面的所有类
execution(public * spring.aop.helloworld.*.*(int, int))

//匹配ArithmetricCalculator接口的所有公有方法
execution(public * ArithmeticCalculator.*(...))

//匹配ArithmetricCalculator接口中返回double类型数值的方法
execute public double ArithmetricClaculator.*(...)

//配第一个参数为double类型的方法, ...匹配任意数量任意类型的参数
execution public double ArithmetricCalculator.*(double, ...)

//匹配参数类型为double, double的方法
execution public double ArithmetricCalculator.*(double, double)

  

 

 

Spring AOP 前置通知和后置通知

标签:

原文地址:http://www.cnblogs.com/shi-yi-ge/p/5170788.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!