Spring AOP 环绕通知

2022/6/30 6:19:55

本文主要是介绍Spring AOP 环绕通知,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1、使用AOP环绕通知完成如下业务。

   在一个properties文件中,配置多个疫情高风险区域地址。

   制作一个用户出行校验系统。系统出行方法中显示如下信息“您好,XXX,欢迎您乘坐从XX地到XX地航班”。

   要求使用环绕通知,对用户出发地和目的地进行是否高风险排查。如果用户出发地为高风险区域,则不允许用户出行,给出高风险提示。如果用户目的地为高风险区域,则要生成日志系统,记录用户姓名、出发地、目的地及出发时间。

 

package com.xzit.test;

import com.xzit.model.Passenger;
import com.xzit.model.Waiter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

/**
 * 2、使用AOP环绕通知完成如下业务。
 *    在一个properties文件中,配置多个疫情高风险区域地址。
 *    制作一个用户出行校验系统。系统出行方法中显示如下信息“您好,XXX,欢迎您乘坐从XX地到XX地航班”。
 *    要求使用环绕通知,对用户出发地和目的地进行是否高风险排查。
 *    如果用户出发地为高风险区域,则不允许用户出行,给出高风险提示。
 *    如果用户目的地为高风险区域,则要生成日志系统,记录用户姓名、出发地、目的地及出发时间。
 */
public class TestAround {
    public static void main(String[] args) throws IOException {
        /* 搞一个文件读取的类来读取db.properties文件的高风险地区 */
        Properties properties = new Properties();
        properties.load(new FileInputStream("C:\\JavaProject\\idea_Workspace\\springFramework\\day_05_job\\src\\main\\resources\\db.properties"));
        String highrisk = properties.getProperty("highrisk");

        /* 定义旅客的信息 */
        String passengerName = "曾誉";
        String origin = "Zhuhai";
        String destination = "Beijing";

        /* 获取并执行旅游的方法,把旅客信息和高风险地区作为参数传过去 */
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        Passenger passenger = context.getBean(Passenger.class);
        passenger.travel(passengerName,origin,destination,highrisk);
    }
}

 

package com.xzit.util;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 环绕通知(增强)
 * 2、使用AOP环绕通知完成如下业务。
 *    在一个properties文件中,配置多个疫情高风险区域地址。
 *    制作一个用户出行校验系统。系统出行方法中显示如下信息“您好,XXX,欢迎您乘坐从XX地到XX地航班”。
 *    要求使用环绕通知,对用户出发地和目的地进行是否高风险排查。
 *    如果用户出发地为高风险区域,则不允许用户出行,给出高风险提示。
 *    如果用户目的地为高风险区域,则要生成日志系统,记录用户姓名、出发地、目的地及出发时间。
 */
//让当前的通知类作为spring容器中的组件
@Component
//使用AspectJ来实现AOP
@Aspect
public class AroundUtil {
    @Around(value = "execution(* travel(..))")
    public void transaction(ProceedingJoinPoint point){
        System.out.println("开启事务");
        try {
            //获取到Passenger对象,然后把旅客的属性和出发地,目的地等信息都存在String变量里方便判断和对比
            Object[] args = point.getArgs();
            String passengerName= (String) args[0];
            String origin = (String) args[1];
            String destination = (String) args[2];
            String highrisk = (String) args[3];
            System.out.println(origin);
            System.out.println(destination);
            System.out.println(highrisk);

            //如果用户出发地为高风险区域,则不允许用户出行,给出高风险提示。
            if (highrisk.contains(origin)){
                System.out.println("提示:您的出发地是高风险地区,不允许出行!");
            }else if (highrisk.contains(destination)){//如果用户目的地为高风险区域,则要生成日志系统,记录用户姓名、出发地、目的地及出发时间。
                System.out.println("提示:您的目的地是高风险地区!系统将记录本次行程进入日志。");
                point.proceed();//调用目标方法,允许出行
                System.out.println("正在生成日志...");
                try {
                    //打印日志
                    BufferedWriter bw = new BufferedWriter(new FileWriter("d:/log.log",true));
                    bw.write("出行的旅客:"+passengerName);
                    bw.newLine();
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
                    LocalDateTime dateTime=LocalDateTime.now();
                    String time = formatter.format(dateTime);
                    bw.write("出行的时间:"+ time);
                    bw.newLine();
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("日志生成成功!");
            }else{
                System.out.println("提示:您的出发地和目的地没有高风险地区。");
                point.proceed();//调用目标方法,允许出行
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("提交事务");
    }
}
package com.xzit.model;

import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class Passenger {
    private String passengerName;   //旅客姓名
    private String origin;          //出发地
    private String destination;     //目的地
    private String highrisk;        //高风险地区

    public void travel(String passengerName,String origin,String destination,String highrisk){
        System.out.println("您好,"+passengerName+"欢迎您乘坐从"+origin+"到"+destination+"的航班。");
    }

    public String getPassengerName() {
        return passengerName;
    }

    public void setPassengerName(String passengerName) {
        this.passengerName = passengerName;
    }

    public String getOrigin() {
        return origin;
    }

    public void setOrigin(String origin) {
        this.origin = origin;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public String getHighrisk() {
        return highrisk;
    }

    public void setHighrisk(String highrisk) {
        this.highrisk = highrisk;
    }

    @Override
    public String toString() {
        return "Passenger{" +
                "passengerName='" + passengerName + '\'' +
                ", origin='" + origin + '\'' +
                ", destination='" + destination + '\'' +
                ", highrisk='" + highrisk + '\'' +
                '}';
    }
}
//dp.properties
highrisk=Beijing Guangzhou Shanghai Shenzhen
<?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
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd">
    <context:component-scan base-package="com.xzit"></context:component-scan>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springFramework</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>day_05_job</artifactId>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.3.16</version>
        </dependency>
    </dependencies>
</project>

 



这篇关于Spring AOP 环绕通知的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程