为什么我的play框架控制器不能识别我的singleton类?

mqxuamgl  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(213)

我尝试发出http get请求,以反映应用程序仍在控制台中运行时所做的更改。例如:如果我添加两个cars并发送httpget请求,那么请求应该返回2作为cars列表的大小。我正在使用一个play框架应用程序来尝试实现这个功能,因为我计划最终实现一个基于js的前端。
我有下面的singelton课程。

package Model;

import java.util.ArrayList;

public class GarageManager {
    private static GarageManager gm;
    private static ArrayList<Car> carsList;

    private GarageManager(ArrayList<Car> carsList) {
        GarageManager.carsList = carsList;
    }

    public static GarageManager getInstance() {
        if (gm == null) {
            System.out.println("Empty");
            ArrayList<Car> carsList = new ArrayList<>();
            gm = new GarageManager(carsList);
        }
        return gm;

    }

    public static ArrayList<Car> getCarsList() {
        if (carsList == null) {
            carsList = new ArrayList<>();
        }
        return carsList;
    }

    public void addCar(Car car) {
        //add cars to the carsList ArrayList
    }

    public void deleteCar(Car car) {
        //delete cars from the carsList Array
    }
}

这是我的控制器:

package controllers;

import Model.GarageManager;
import com.fasterxml.jackson.databind.JsonNode;
import play.libs.Json;
import play.mvc.*;

public class DlController extends Controller {
    public Result garageSummary() {
        JsonNode jsonNode = Json.toJson(GarageManager.getCarsList().size());
        return ok(jsonNode).as("application/json");
    }
}

这是我的汽车级别:

package Model;

public class Car {
    private String make;
    private String model;

    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }
}

我正在测试控制器是否能够通过使用httpget请求识别在其他类中所做的更改,并检查在主类中所做更改时cars列表的大小是否被更新。无论控制台应用程序中添加了多少辆车,请求总是返回0。
我的应用程序中的所有其他类都能够识别一个对象是在另一个类中创建的,因此它们不会尝试创建第二个对象。我的dlcontroller类无法识别该类已在另一个类中示例化,因此它创建了一个新对象,该对象为空,没有数据,但实际上不是空的。
我通过在main方法中调用garageanger.getinstance()来运行我的应用程序。对对象所做的任何更改都可以在另一个测试类中看到。测试类能够正确识别主类中添加了多少辆车。
主要类别

package Model;

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        GarageManager gm = GarageManager.getInstance();
        System.out.println("Menu");
        System.out.println("Press A to add a car");
        System.out.println("Press D to Delete a car");
        Scanner scan = new Scanner(System.in);
        System.out.print("Please choose an option: ");
        String select = scan.nextLine();

        switch (select){
            case "A":
                // call the gm.addCar Method
                break;
            case "D":
                // call the gm.deleteCar Method
                break;
            default:
                System.out.println("Invalid option");
        }
        //Checking to so see if the test class can correctly recognize changes made in Main
        Test tst = new Test();
        tst.print();
    }
}

测试等级

package Model;

public class Test {
    PremierLeagueManager pm = PremierLeagueManager.getInstance();

    public void print() {
        System.out.println(PremierLeagueManager.getClubsList().size());
    }
}

以下是我的目录结构:

├── /app/                                 # The backend source (controllers, models, services)
│     └── /controllers/                   # Backend controllers
│           └── FrontendController.scala  # Asset controller wrapper serving frontend assets and artifacts
│           └── DlController.java         # This is the controller that's giving me issues
│     └── /Model/                         # My Java files
│           └── Main.java                 
│           └── GarageManager.java        
│           └── Test.java   
            └── Car.java                
├── /conf/                                # Configurations files and other non-compiled resources (on classpath)
│     ├── application.conf                # Play application configuratiion file.
│     ├── logback.xml                     # Logging configuration
│     └── routes                          # Routes definition file
├── /logs/                                # Log directory
│     └── application.log                 # Application log file
├── /project/                             # Contains project build configuration and plugins
│     ├── FrontendCommands.scala          # Frontend build command mapping configuration
│     ├── FrontendRunHook.scala           # Forntend build PlayRunHook (trigger frontend serve on sbt run)
│     ├── build.properties                # Marker for sbt project
│     └── plugins.sbt                     # SBT plugins declaration
├── /public/                              # Frontend build artifacts will be copied to this directory
├── /target/                              # Play project build artifact directory
│     ├── /universal/                     # Application packaging
│     └── /web/                           # Compiled web assets
├── /test/                                # Contains unit tests of backend sources
├── /ui/                                  # Contains the angular project
│     ├── /e2e/                           # End to end tests folder
│     ├── /node_modules/                  # 3rd-party frontend libraries and utilities
│     ├── /src/                           # The frontend source code (modules, componensts, models, directives, services etc.) of the application
│     |     ├── karma.conf.js             # Karma configuration file
│     |     └── proxy.conf.json           # UI proxy configuration      
│     ├── .angular.json                   # Angular CLI configuration
│     ├── .editorconfig                   # Define and maintain consistent coding styles between different editors and IDEs
│     ├── .gitignore                      # Contains ui files to be ignored when pushing to git
│     ├── package.json                    # NPM package configuration.
│     ├── README.md                       # Contains all user guide details for the ui
│     ├── tsconfig.json                   # Contains typescript compiler options
│     └── tslint.json                     # Lint rules for the ui
├── .gitignore                            # Contains files to be ignored when pushing to git
├── build.sbt                             # Play application SBT configuration
├── LICENSE                               # License Agreement file
├── README.md                             # Application user guide
└── ui-build.sbt

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题