1. 程式人生 > >spring_(17)Spring_後置通知與前置通知

spring_(17)Spring_後置通知與前置通知

後置通知

  • 後置通知是在連線點完成之後執行的,即連線點返回結果或者丟擲異常的時候,下面的後置通知記錄了方法的終止。
  • 一個切面可以包括一個或者多個通知

例子程式

基本結構

在這裡插入圖片描述

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.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;

//把這個類宣告為一個切面:需要把該類放入到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);

    }

    //後置通知:在目標方法執行後(無論是否發生異常),執行的通知
    //在後置通知中還不能訪問目標方法執行的結果。
    @After("execution(* com.spring.aop.impl.*.*(int,int))")
    public void afterMethod(JoinPoint joinPoint){

        String methodName = joinPoint.getSignature().getName();

        System.out.println("The method "+methodName + " ends ");

    }

}

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,2);
        System.out.println("result:" + result);


    }

}

執行結果:就算會有異常,後置通知也會產生作用

在這裡插入圖片描述