I'm trying to make HTTP GET requests that reflects changes made while the application is still running in the console. For example: If I add two cars and send the HTTP GET request then the request is supposed to return 2 as the size of the cars list. I'm using a Play Framework application to try and acheive this functionality since I plan to implement a JS based front-end eventually.
I have the following Singelton class.
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 } }
This is my controller:
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"); } }
This is my Car Class:
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; } }
I'm testing whether the controller is able to recognize changes made in other classes by using a HTTP GET request and checking whether the size of the cars list is updated when changes are made in the Main class. The request always returns 0 regardless of how many cars are added in the console application.
All the other classes in my application are able to recognize that an object has been created in another class and so they don't try to create a second object. My DlController class is unable to recognize that the class has been instantiated in another class so it creates a new object that's blank and has no data but isn't actually null.
I'm running my application by calling GarageManger.getInstance() in my main method. Any changes made to the object are visible in another Test class. The test class is able to correctly identify how many cars have been added in the Main Class.
Main Class
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(); } }
Test Class
package Model; public class Test { PremierLeagueManager pm = PremierLeagueManager.getInstance(); public void print() { System.out.println(PremierLeagueManager.getClubsList().size()); } }
Here's my directory Structure:
├── /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
https://stackoverflow.com/questions/65386085/why-isnt-my-play-framework-controller-able-to-recognize-my-singleton-class December 21, 2020 at 08:09AM
没有评论:
发表评论