Spring Rest - Use of various Services and Repositories

3

I have a question about the best way to implement some services with Spring Rest , but I could not find material for this case:

Let's say I have a service for making a release ( LancamentoService ). This service launches, updates the balance and charges the fee. So, I've injected the repositories in Repository , BalanceRepository and RateRepository .

In addition to this service, it would also have services to make loot, transfer, etc., and all these would need to make a launch, plus other things. To do this I'm injecting the LancamentoService into each of these sevices.

However, even though I call a service in SaqueService , for example, that I would not need to do postings, L ancamentoService is "loaded" along with all its repositories.

Is that so? If anyone has a better practice or some reference material, thank you very much.

    
asked by anonymous 17.10.2018 / 18:36

1 answer

3

Early in the application, Spring already scans its classes for beans to be managed by it. This means that even in classes that your program has not even passed yet, the beans of it are already, in theory, instantiated and managed, ready to use. In other words, any bean injected into your class, even if not in use, will be preinstalled. It is a small price to pay for the huge facility that the control inversion provides in development.

As for your architecture, I would create a service for each action of the release ( TarifaService , SaldoService etc.), would inject within each of them their respective Repository and finally, in class LancamentoService would inject these created services. This makes it easier for you to centralize business rules that eventually need to be applied to the tariff, for example, and also conform to best practices, which say business rules should be handled at the service layer rather than at the DAO layer (repositories) .

    
17.10.2018 / 21:11