Spring基于注解配置AOP

时间:2021-05-27 06:11:47


D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\aop.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--配置自动扫描的包-->
    <context:component-scan base-package="com.xiya.spring.aop"/>
    <!--使AspectJ注释起作用:自动为匹配的类生成代理对象-->
    <aop:aspectj-autoproxy/>
</beans>

<context:component-scan base-package="com.xiya.spring.aop"/>
在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的Java文件,

如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean

com/xiya/spring/aop/advice/LoggingAspect.java

package com.xiya.spring.aop.advice;

/**
 * Created by N3verL4nd on 2017/3/24.
 */

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;

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

/**
 * 如何把这个类声明为一个切面:
 * 1、把该类放入IOC容器中
 * 2、再声明为一个切面
 */
@Aspect
@Component
public class LoggingAspect {
    //声明该方法是一个前置通知:在目标方法执行之前执行
    @Before("execution(public int com.xiya.spring.aop.service.impl.ArithmeticCalculatorImpl.*(int,int))")
    public void doBefore(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The method " + methodName + " begins with " + args);

    }

    //后置通知:在目标方法执行后(无论是否发生异常)执行的通知
    //在后置通知中还不能访问目标方法执行的结果
    @After("execution(* com.xiya.spring.aop.service.impl.ArithmeticCalculatorImpl.*(int,int))")
    public void doAfter(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("the method " + methodName + " ends!");
    }
}
com/xiya/spring/aop/service/ArithmeticCalculator.java
package com.xiya.spring.aop.service;

/**
 * Created by N3verL4nd on 2017/3/24.
 */
public interface ArithmeticCalculator {
    int add(int x, int y);
    int sub(int x, int y);
    int mul(int x, int y);
    int div(int x, int y);
}
com/xiya/spring/aop/service/impl/ArithmeticCalculatorImpl.java
package com.xiya.spring.aop.service.impl;

import com.xiya.spring.aop.service.ArithmeticCalculator;
import org.springframework.stereotype.Component;

/**
 * Created by N3verL4nd on 2017/3/24.
 */
@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
    @Override
    public int add(int x, int y) {
        int result = x + y;
        //int z = 1 / 0;
        return result;
    }

    @Override
    public int sub(int x, int y) {
        int result = x - y;
        return result;
    }

    @Override
    public int mul(int x, int y) {
        int result = x * y;
        return result;
    }

    @Override
    public int div(int x, int y) {
        int result = x / y;
        return result;
    }
}
com/xiya/spring/aop/test/Main.java
package com.xiya.spring.aop.test;

import com.xiya.spring.aop.service.ArithmeticCalculator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by N3verL4nd on 2017/3/24.
 *
 */
public class Main {
    public static void main(String[] args) {
        //1、创建Spring的IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("aop.xml");

        //2、从IOC容器中获取bean实例
        ArithmeticCalculator calculator = context.getBean(ArithmeticCalculator.class);

        //3、使用bean
        int result = calculator.add(10, 20);
        System.out.println("result = " + result);

        result = calculator.div(10, 2);
        System.out.println("result = " + result);
    }
}