spring_(16) Spring_前置通知

  • AspectJ:Java社区里最完整最流行的AOP框架。
  • 在Spring2.0以上版本中,可以使用基于AspectJ注解或基于XML配置的AOP

在Spring中启用AspectJ注解支持

  • 要在Spring应用中使用AspectJ注解,必须在classpath下包含AspectrJ类库:aopalliance.jar、aspectj.weaver.jar、和spring-aspects.jar
  • 将aopSchema添加到< beans > 根元素中.
  • 要在SpringIOC容器中启用AspectJ注解支持,只要在Bean配置文件中定义一个空的XML元素< aop:aspectj-autoproxy >
  • 当Spring IOC容器侦测到Bean配置文件中的< aop:aspectj-autoproxy >元素时,会自动为与AspectJ切面匹配的Bean创建代理。

用AspectJ注解声明切面

  • 要在Spring中声明AspectJ切面,只需要在IOC容器中将切面声明为Bean实例。当在SpringIOC容器中初始化AspectJ切面之后,SpringIOC容器中初始化AspectJ切面之后,SpringIOC容器就会为那些于AspectJ切面相匹配的Bean创建代理.

  • 在AspectJ注解中,切面只是一个带有@Aspect注解的Java类.

  • 通知是标注有某种注解的简单的java方法.

  • AspectJ支持5中类型的通知注解:

    1. @Before:前置通知,在方法执行之前执行
    2. @After:后置通知,在方法执行之后执行
    3. @AfterRunning:返回通知,在方法返回结果之后执行
    4. @AfterThrowing:异常通知,在方法抛出异常之后
    5. @Around:环绕通知,围绕着方法执行

在这里插入图片描述

前置通知

  • 前置通知:在方法执行之前的通知
  • 前置通知使用@Before注解,并将切入点表达式的值作为注解值

在这里插入图片描述

例子程序

基本结构

在这里插入图片描述

ArithmeticCalculator.java

package com.spring.aop.impl;

public interface ArithmeticCalculator {

    int add(int i, int j);
    int sub(int i, int j);

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


}

ArithmeticCalculatorImpl.java

package com.spring.aop.impl;

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 com.spring.aop.impl;

import org.aspectj.lang.JoinPoint;
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;

//把这个类声明为一个切面:需要把该类放入到IOC容器中、再声明为一个切面
@Aspect
@Component
public class LoggingAspect {

    //声明该方法是一个前置通知:在目标方法开始之前执行
    @Before("execution(public int com.spring.aop.impl.ArithmeticCalculator.*(int ,int ))")
    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());

        System.out.println("The method "+methodName + " begins with " + 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: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.spring.aop.impl"></context:component-scan>

    <!--使AspjectJ 注解起作用: 自动为匹配的类生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>

Main.java

package com.spring.aop.impl;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args){

        //1.创建Spring的IOC容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

        //2.从IOC容器中获取bean的实例
        ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class);

        //3.使用bean
        int result = arithmeticCalculator.add(3,6);
        System.out.println("result:" + result);

        result = arithmeticCalculator.div(12,6);
        System.out.println("result:" + result);


    }

}

note.txt

1.SpringAOP

1).加入jar包:

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

commons-logging-1.1.1.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

2).在配置文件中加入aop的命名空间

3).基于注解的方式

 1.在配置文件中加入如下配置:

 <aop:aspectj-aitoproxy> </aop:aspectj-aitoproxy>

 2.把横切关注点的代码抽象到切面的类中。
    1))切面首先是一个IOC中的bean,即加入@Component注解
    2))切面还需要加入@Aspect注解
 3.在类中声明各种通知:
    1))声明一个方法
    2))在方法前加入@Before注解

 4.可以在通知方法中声明一个类型为JointPoint的参数。然后就能访问链接细节。如方法名称和参数值。

运行结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42036647/article/details/84557289