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.
This commit is contained in:
Gabriel Magioli 2023-12-29 15:58:59 -03:00
parent 0aa3adb56f
commit d1b096d6ca
3 changed files with 12 additions and 4 deletions

View file

@ -44,9 +44,11 @@ import jakarta.validation.Valid;
@Controller @Controller
class OwnerController { 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) { public OwnerController(OwnerRepository clinicService) {
this.owners = clinicService; this.owners = clinicService;

View file

@ -41,9 +41,11 @@ import jakarta.validation.Valid;
@RequestMapping("/owners/{ownerId}") @RequestMapping("/owners/{ownerId}")
class PetController { 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) { public PetController(OwnerRepository owners) {
this.owners = owners; this.owners = owners;

View file

@ -23,3 +23,7 @@ logging.level.org.springframework=INFO
# Maximum time static resources should be cached # Maximum time static resources should be cached
spring.web.resources.cache.cachecontrol.max-age=12h spring.web.resources.cache.cachecontrol.max-age=12h
# Environment Variables
views.owner.createOrUpdateForm=owners/createOrUpdateOwnerForm
views.pets.createOrUpdateForm=pets/createOrUpdatePetForm