Call repository and service from controller layer with Spring

spring service layer architecture
spring boot service layer
controller service repository
controller vs service layer
how to call service from controller in spring boot
spring service repository pattern
how to call service method in controller in spring mvc
service layer example

I use Spring Boot and Spring Data.

I have no problem in my mind to separate Repository Layer and Service Layer

So I have my UserRepository with CRUD method and some Spring Data method

  • findAll
  • findByUsername

I also have UserService with business method.

  • checkPassword(String login,String password)
  • businessMethodAction(String username)

Here is my question:

In my controller I have to call method from UserService and sometime from UserRepository. For the moment, I inject both in my controller, and I call service or repository

@Inject
UserService userService;

@Inject
UserRepository userRepository;

@RequestMapping("{username}")
private void myMethod(@PathVariable String username){
    return userRepository.findOne(username);
}

@RequestMapping("{username}/doBusineesAction")
private void myMethod(@PathVariable String username){
    return userService.doLogicalThin(username);
}

I'm just asking because I confused to inject both and to call one or the other in the same class

On another side, this would mean to duplicate method in service layer like this

public User findOne(String username){
 return userRepository.findOne(username);
}

What's your opinion?


Controller layer shouldn't ever call repository directly. You should always use the service layer because the service layer encapsulates your business logic surrounding that call. Just because currently there isn't any business logic, doesn't mean that you should skip the layer entirely.

Is Spring annotation @Controller same as @Service?, No, think of it this way: a repository is a service (also). If the entities you retrieve through the repository handle most of the business logic there is no need for  Controller layer shouldn't ever call repository directly. You should always use the service layer because the service layer encapsulates your business logic surrounding that call. Just because currently there isn't any business logic, doesn't mean that you should skip the layer entirely.


If your controller does not require business logic or perform a single repository operation, you can use the repositories directly. Use services to implement use cases that require business logic or orchestration of repository calls.

What will happen if we interchange @service and @repository , is there to provide logic to operate on the data sent to and from the DAO and the client. Very often these 2 pieces will be bundled together into the same module, and occasionally into the same code, but you'll still see them as distinct logical entities. Then it will be automatically detected during the Spring classpath scanning. Difference of @Service, @Repository, @Controller with @Component is that they are special cases of @Component and used for particular purposes. E.g:- spring managed components in the persistence, service, and presentation layers.


Service layer can also be used to serve loose coupling in the application.​Suppose your controller has 50 methods and in turn it calls 20 Dao  JavaServer Faces is a server-side component-based user interface framework. Originally it was developed as part of the Jakarta EE. In this tutorial, we'll investigate how to integrate JSF into a Spring Boot application. As an example, we'll implement a simple application to create a TO-DO list. 2. Maven Dependencies.


In my opinion, the service layer must implement the business logic and it must be called from controllers. In most of the cases, this layer has to perform more operations than just calling a method from a DAO object. This is probably the best solution if your application has great size. Also, you can split your logic into several parts and make it working in one transaction, that help you to save the data in the non-controversial state.

Spring Boot @Repository tutorial shows how to use the @Repository Spring is a popular Java application framework and Spring Boot is an evolution After these settings, we access the application at localhost:8086/SpringBootRepository​/ . Controller; import org.springframework.web.bind.annotation. the data comes from the repository (it Will handle going to the store) the data is processed at the business logic layer (service) Note that the data in question does not necessarily have to come from a database and the logic still applies. For example, we have an ILoggingRepository that is passed into our implementation of the ILoggingService.


One approach is to reserve controllers are for business level concepts displayed on the views and repositories for the normalized entities in the DB. One can then define services to address the many-to-many relationship between controllers and repositories.

The central interface in Spring Data repository abstraction is Repository PagingAndSortingRepository<User, Long> repository = // … get access to a bean The registration enables Pageable and Sort being valid controller method arguments hand it to layers below then or execute business logic on the entities directly. Many applications won't benefit from additional complexity and maintenance cost of service or repository layers unless there is a need. With version 1.2.0 Spring Roo offers support for specific application layering tailored to the needs of the application. This section provides an overview of Roo's support for service and repository layers.


No, the REST controller should use the repository directly; the REST controller will make a direct call to the repository. No service layer is added. 2. Yes  Service layer can also be used to serve loose coupling in the application.Suppose your controller has 50 methods and in turn it calls 20 Dao methods,Now at later point you decide to change the Dao


This section provides an overview of Roo's support for service and repository and call a new service layer within an existing MVC controller, Integration test or​  The Repository-Service pattern breaks up the business layer of the app into two distinct layers. The lower layer is the Repositories. These classes handle getting data into and out of our data store, with the important caveat that each Repository only works against a single Model class.


In most typical applications, we have distinct layers like data access, presentation​, service, business, etc. And, in each layer, we have various