From d1b096d6cab72dd82b5b419cd423944be121ecf6 Mon Sep 17 00:00:00 2001 From: Gabriel Magioli Date: Fri, 29 Dec 2023 15:58:59 -0300 Subject: [PATCH] Refactor: Use environment variables for URLs and @Autowired for injection Instead of hardcoding URLs for owners/createOrUpdateOwnerForm and pets/createOrUpdatePetForm in the code, introduce environment variables for these values. Removed the static final type to allow injection. Also, replaced constructor injection with @Autowired annotation for OwnerRepository to reduce lines of code. --- .../samples/petclinic/owner/OwnerController.java | 6 ++++-- .../samples/petclinic/owner/PetController.java | 6 ++++-- src/main/resources/application.properties | 4 ++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java index 329c82395..25417e968 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java @@ -44,9 +44,11 @@ import jakarta.validation.Valid; @Controller class OwnerController { - private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm"; + @Value("${views.owner.createOrUpdateForm}") + private String VIEWS_OWNER_CREATE_OR_UPDATE_FORM; - private final OwnerRepository owners; + @Autowired + private OwnerRepository owners; public OwnerController(OwnerRepository clinicService) { this.owners = clinicService; diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java index fae63b8fb..4334ece14 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java @@ -41,9 +41,11 @@ import jakarta.validation.Valid; @RequestMapping("/owners/{ownerId}") class PetController { - private static final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm"; + @Value("${views.pets.createOrUpdateForm}") + private String VIEWS_PETS_CREATE_OR_UPDATE_FORM; - private final OwnerRepository owners; + @Autowired + private OwnerRepository owners; public PetController(OwnerRepository owners) { this.owners = owners; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 5d3eeed32..df874a090 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -23,3 +23,7 @@ logging.level.org.springframework=INFO # Maximum time static resources should be cached spring.web.resources.cache.cachecontrol.max-age=12h + +# Environment Variables +views.owner.createOrUpdateForm=owners/createOrUpdateOwnerForm +views.pets.createOrUpdateForm=pets/createOrUpdatePetForm \ No newline at end of file