java - 使用 AspectJ 保存到文件

标签 java aop aspectj

我是一名正在学习 Java 的缺乏经验的开发人员。 我正在开发此 AddressBook,其中我正在将 AspectJ 实现到我的功能之一(更新联系人)。 在更新联系人之前,显然用户需要添加一个联系人,我的 saveContact 代码如下所示:

public class Contacts { //class to manage the contacts information

    public String name;
    public String street;
    public String city;
    public String state;
    public int zip;
    public long phoneNumber;
    public Scanner input;

    public void saveContact()
    {//code to add and save a contact to the book
        input = new Scanner(System.in);

        System.out.println("Plase enter contact Name and Lastname: ");
        name = input.nextLine();

        System.out.println("\nPlase enter Street of contact: ");
        street = input.nextLine();

        System.out.println("\nPlase enter City of the contact: ");
        city = input.nextLine();

        System.out.println("\nPlase enter State of the contact: ");
        state = input.nextLine();

        System.out.println("\nPlase enter the Zipcode of the contact: ");
        zip = input.nextInt();

        System.out.println("\nPlase enter the contact Phone number (Ex 1115550000): ");
        phoneNumber = input.nextLong();

        System.out.println("Done! Contact Saved");
}

我有更多选项,例如更新联系人、查找联系人和删除联系人。在执行更新联系人函数之前,我想运行一个方面,将用户输入的值保存到 file.txt 中的变量(名称、城市、州等)中,然后再在更新联系人代码中分配新值。 我的问题是,当执行方面建议时,我的 .txt 文件带有 null,而不是用户在添加联系人代码中分配的值。

我的方面看起来像这样:

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public aspect AspectUpdateContact { //code to save the old information of the contact to the file before updating the contact

    pointcut aspectCallUpdateFile() : call (void updateContact()) && within (Menu);
    before() : aspectCallUpdateFile() 
        {
/*******Code is running and saving to the file but not bringing the values of the variables, saving null values*/
        Contacts ct = new Contacts();
        try {

        PrintWriter pwrite = new PrintWriter(new FileWriter("UpdatesLog.txt", true));//to append the write to the end of the file

        pwrite.println("Record Updated->" + ct.name +":"+ ct.street +":"+ ct.city +":"+ ct.state +":"+ ct.zip +":"+ ct.phoneNumber);

        pwrite.close();//close the Print Writer

            }
            catch (IOException e) {
                System.out.println(e.getMessage());
            }

    }// end of code for pointcut

}//end of aspect

我在 txt 文件中的最终输出是:Record Updated:null:null:null:null:0:0

最佳答案

我对您的代码进行了一些重构(类名不再复数,重命名的方法,私有(private)字段,toString() 方法,便捷构造函数,尝试使用方面中的资源),并且还通过使用 target() 与方面中的参数绑定(bind)来修复方面:

联系人数据类:

package de.scrum_master.app;

import java.util.Scanner;

public class Contact {
  private String name;
  private String street;
  private String city;
  private String state;
  private int zip;
  private long phoneNumber;

  private Scanner input;

  public Contact() {}

  public Contact(String name, String street, String city, String state, int zip, long phoneNumber) {
    this.name = name;
    this.street = street;
    this.city = city;
    this.state = state;
    this.zip = zip;
    this.phoneNumber = phoneNumber;
  }

  @Override
  public String toString() {
    return "Contact[name=" + name + ", street=" + street + ", city=" + city + ", state=" + state +
      ", zip=" + zip + ", phoneNumber=" + phoneNumber + "]";
  }

  public void updateContact() {
    input = new Scanner(System.in);

    System.out.println("Please enter contact Name and Lastname: ");
    name = input.nextLine();
    System.out.println("\nPlease enter Street of contact: ");
    street = input.nextLine();
    System.out.println("\nPlease enter City of the contact: ");
    city = input.nextLine();
    System.out.println("\nPlease enter State of the contact: ");
    state = input.nextLine();
    System.out.println("\nPlease enter the Zipcode of the contact: ");
    zip = input.nextInt();
    System.out.println("\nPlease enter the contact Phone number (Ex 1115550000): ");
    phoneNumber = input.nextLong();

    System.out.println("Done! Contact updated.\n");
  }
}

带有主方法的虚拟菜单类:

package de.scrum_master.app;

public class Menu {
  public void updateContact(Contact contact) {
    contact.updateContact();
  }

  public static void main(String[] args) {
    Menu menu = new Menu();
    Contact contact = new Contact("Albert Einstein", "101 Main St", "Middle of Nowhere", "Utah", 12345, 11223344);
    menu.updateContact(contact);
    menu.updateContact(contact);
  }
}

如您所见,我正在创建一个初始联系对象,然后更新它两次以创建两行日志输出。

日志记录方面:

package de.scrum_master.aspect;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import de.scrum_master.app.Contact;
import de.scrum_master.app.Menu;

public aspect UpdateContactAspect {
  pointcut callUpdateContact(Contact contact) :
    call(void updateContact()) && within(Menu) && target(contact);

  before(Contact contact) : callUpdateContact(contact) {
    try (PrintWriter writer = new PrintWriter(new FileWriter("UpdatesLog.txt", true))) {
      writer.println("Record updated -> " + contact);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

示例运行的控制台日志:

Please enter contact Name and Lastname: 
John Doe

Please enter Street of contact: 
321 Broadway

Please enter City of the contact: 
New York City

Please enter State of the contact: 
New York

Please enter the Zipcode of the contact: 
54321

Please enter the contact Phone number (Ex 1115550000): 
0123456789
Done! Contact updated.

Please enter contact Name and Lastname: 
Donald Duck

Please enter Street of contact: 
33 Wherever

Please enter City of the contact: 
Quacktown

Please enter State of the contact: 
Duckstate

Please enter the Zipcode of the contact: 
88099

Please enter the contact Phone number (Ex 1115550000): 
999333111
Done! Contact updated.

示例运行后的日志文件内容:

Record updated -> Contact[name=Albert Einstein, street=101 Main St, city=Middle of Nowhere, state=Utah, zip=12345, phoneNumber=11223344]
Record updated -> Contact[name=John Doe, street=321 Broadway, city=New York City, state=New York, zip=54321, phoneNumber=123456789]

还有更多的东西需要重构,但我就到此为止。

关于java - 使用 AspectJ 保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58649729/

相关文章:

java - 将 iajc 与 java 1.8 JDK 一起使用 - 比较器无法解析为类型

java - Keycloak RC4-带 Hmac

java - Java中的Anagram算法

Ninject 拦截具有特定属性的任何方法?

java - 将方面添加到实现类而不是接口(interface)

java - AspectJ:如何获取切入点以通知位于其他项目中的类

java - 在 Java 代理中重定位 AspectJ 包

java - 球从鼠标拖动绘制的线上弹起

java - jcifs copyTo 从一个网络到另一网络路径不起作用

java - 获取解析异常