标签:


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>
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);
}
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;
}
}
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);
}
}
//表示任意返回值 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)
标签:
原文地址:http://www.cnblogs.com/shi-yi-ge/p/5170788.html