Spring之AOP(二)---前置通知

一、首先我要加入jar包:

com.springsource.org.aopaaliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar

commmons-logging-1.1.3.jar

spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar

二、在配置文件中加入aop的命名空间

<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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

三、基于注解的方式
在配置文件中加入如下代码,给目标自动生成代理对象。

<!-- 使before方法起作用,即要使AspectJ注解起作用 ,自动为匹配的类生成代理对象,这里匹配的类是AtithmeticCaculator-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

把横切关注点的代码抽象到切面的类中:
1、切面首先是ioc中的一个bean,即加入@Component注解
2、切面还要加入@Aspect注解,因为它是一个特殊的bean

在切面类中5种类型的通知:(通知实际上就是方法)
@Before:前置通知,方法执行前执行
@After:后置通知,方法执行后通知
@AfterReturning:方法返回结果后执行
@AfterThrowing:方法抛出异常后执行
@Around:环绕通知,围绕方法执行
下面就是一个切面类:

//把这个类声明为一个切面
@Aspect
@Component
public class LoggingAspect {
    @Before("execution(public int spring.impl.AtithmeticCaculator.*(int, int))")
public void beforeMethod(JoinPoint joinPoint){
        String methodName=joinPoint.getSignature().getName();
        List<Object> args=(List) Arrays.asList(joinPoint.getArgs());
    System.out.println("======================================="+methodName+"方法开始"+"参数为 "+args);
}
}

如上代码,@Before里面是AspectJ表达式,用来锁定目标。用JointPoint可以让你获取连接点的信息。
下面是所有的代码:
Calculator.java

public interface AtithmeticCaculator {
    int add(int i,int j);
    int sub(int i,int j);


    int mul(int i,int j);
    int div(int i,int j);

}

CalculatorImpl.java


@Component
public class AtithmeticCaculatorImpl implements AtithmeticCaculator {
@Override
public int add(int i, int j) {
//  System.out.println("加----------------------"+"起始参数["+i+","+j+"]");
    int result=i+j;
    //System.out.println("加的结果:"+result);

    return result;
}
@Override
    public int div(int i, int j) {
//  System.out.println("除----------------------"+"起始参数["+i+","+j+"]");

    int result=i/j;
    //System.out.println("除的结果:"+result);

    return result;  }
@Override
    public int mul(int i, int j) {
//  System.out.println("乘----------------------"+"起始参数["+i+","+j+"]");

    int result=i*j;
    //System.out.println("乘的结果:"+result);

    return result;
    }
@Override
    public int sub(int i, int j) {
//  System.out.println("减----------------------"+"起始参数["+i+","+j+"]");

    int result=i-j;
//  System.out.println("减的结果:"+result);

    return result;
    }
}

LoggingAspect.java


//把这个类声明为一个切面
@Aspect
@Component
public class LoggingAspect {
    @Before("execution(public int spring.impl.AtithmeticCaculator.*(int, int))")
public void beforeMethod(JoinPoint joinPoint){
        String methodName=joinPoint.getSignature().getName();
        List<Object> args=(List) Arrays.asList(joinPoint.getArgs());
    System.out.println("======================================="+methodName+"方法开始"+"参数为 "+args);
}
}

applicationContext.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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

<context:component-scan base-package="spring.impl"></context:component-scan>

<!-- 使before方法起作用,即要使AspectJ注解起作用 ,自动为匹配的类生成代理对象,这里匹配的类是AtithmeticCaculator-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

上面的代码主要举的是一个前置通知的例子。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值