Spring-AOP-春天的故事4

使用Spring4 AOP步骤

  • 1、导包
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-aspects</artifactId>  
    <version>4.0.2.RELEASE</version>  
</dependency> 
  • 2、配置Bean文件

    配置AOP有两种方法:注解和配置文件,项目中建议使用配置文件

    • 注解方式
<!--扫描包装备-->
   <context:component-scan base-package="com.test.aop"></context:component-scan>

    <!--使Aspectj起作用-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

定义业务接口

package com.test.aop;


public interface Calculation {
    int add(int x,int y);
    int multiply(int x,int y);
    int div(int x,int y);
}

实现类

package com.test.aop;

import org.springframework.stereotype.Component;

@Component
public class CalculationImpl implements Calculation {
    @Override
    public int add(int x, int y) {
        return x+y;
    }

    @Override
    public int multiply(int x, int y) {
        return x*y;
    }

    @Override
    public int div(int x, int y) {
        return x/y;
    }
}

切面

package com.test.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

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


@Aspect
@Component
public class LoggingAspect {

    @Before("execution(public int com.test.aop.Calculation.*(..))")
    public void beforeMethod(JoinPoint joinpoint){
        String methodName=joinpoint.getSignature().getName();
        List<Object> params= Arrays.asList(joinpoint.getArgs());

        System.out.println("The method is "+methodName+" params are "+params);
    }

    @AfterReturning(pointcut = "execution(public int com.test.aop.Calculation.*(..))",returning ="result" )
    public void afterReturning(Object result){
        System.out.println("result="+result);
    }
    @After("execution(public int com.test.aop.Calculation.*(..))")
    public void afterMethod(){
        System.out.println("@After executing... ");
    }
    @AfterThrowing(pointcut ="execution(public int com.test.aop.Calculation.*(..))",throwing = "e")
    public void afterThrowing(Exception e){
        System.out.println("@AfterThrowing executing..");
        System.out.println(e);
    }

}

@Before: 前置通知, 在方法执行之前执行
@After: 后置通知, 在方法执行之后执行 ,即使是方法在执行时发生异常也要通知
@AfterRunning: 返回通知, 在方法返回结果之后执行,方法必须有返回值。需要添加returning = “result”,其中result就是返回结构。
@AfterThrowing: 异常通知, 在方法抛出异常之后,有异常的时候才执行

execution(public void com.spring.aop.AopService.* (..)) : 切入点签名表达式,用*号表示所有,也可以指定,括号内两点表示多个变量,也可以指定,还可以用 && , || , !;以每个execution为一个单位。

切面优先级:@order(n)配置在切面类的上面,n越小优先级别越高。

  • 通过配置文件配置

切面类代码

package com.test.aop2;

import org.aspectj.lang.JoinPoint;

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


public class LogAspect {
    public void before(JoinPoint joinpoint){
        String methodName=joinpoint.getSignature().getName();
        List<Object> params= Arrays.asList(joinpoint.getArgs());
        System.out.println("The method is "+methodName+" params are "+params);
    }

}

spring配置文件applicationContext2.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--扫描包装备-->
    <context:component-scan base-package="com.test.aop"></context:component-scan>

    <!--配置切面类-->
    <bean id="logAspect" class="com.test.aop2.LogAspect"></bean>
    <!--aop配置-->
    <aop:config>
        <!--切点-->
        <aop:pointcut id="aop" expression="execution(public int com.test.aop.Calculation.*(..))"></aop:pointcut>
        <!-- 切面 : ref 的值是 切面类的id-->
        <aop:aspect id="aspect" ref="logAspect">
            <aop:before method="before" pointcut-ref="aop"></aop:before>
        </aop:aspect>
    </aop:config>
</beans>

测试运行类

package com.test.aop2;

import com.test.aop.Calculation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Main2 {

    public static void main(String[] args){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext2.xml");
        Calculation calculation=applicationContext.getBean(Calculation.class);
        int result=calculation.div(6,3);
    }
}

运行结果:
The method is div params are [6, 3]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值