使用 AspectJ 保存到文件

Saving to a File using a AspectJ

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

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文件中的最终输出是:记录Updated:null:null:null:null:0:0

我稍微重构了你的代码(在 class 名称、重命名的方法、私有字段、toString() 方法、便捷构造函数、方面的资源尝试中不再使用复数形式)并修复了方面通过在方面使用 target() 和参数绑定:

联系方式class:

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");
  }
}

虚拟菜单 class 主要方法:

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]

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