ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring AOP 环绕通知

2022-06-29 23:33:53  阅读:122  来源: 互联网

标签:String Spring highrisk 出发地 AOP 高风险 import 环绕 public


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>

 

标签:String,Spring,highrisk,出发地,AOP,高风险,import,环绕,public
来源: https://www.cnblogs.com/zengyu1234/p/16425291.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有