Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying Spring MVC architecture pattern

I'm working through a spring mvc video series and loving it!

  • https://www.youtube.com/channel/UCcawgWKCyddtpu9PP_Fz-tA/videos

I'd like to learn more about the specifics of the exact architecture being used and am having trouble identifying the proper name - so that I can read further.

For example, I understand that the presentation layer is MVC, but not really sure how you would more specifically describe the pattern to account for the use of service and resource objects - as opposed to choosing to use service, DAO and Domain objects.

Any clues to help me better focus my search on understanding the layout below?

application
  core
     models/entities
     services
  rest
     controllers
     resources
        resource_assemblers

Edit: Nathan Hughes comment clarified my confusion with the nomenclature and SirKometa connected the architectural dots that I was not grasping. Thanks guys.

like image 881
BranLakes Avatar asked Apr 16 '15 15:04

BranLakes


2 Answers

As far as I can tell the layout you have mentioned represents the application which communicates with the world through REST services.

  • core package represents all the classes (domain, services, repositories) which are not related to view.
  • model package - Assuming you are aiming for the typical application you do have a model/domain/entity package which represents your data For example: https://github.com/chrishenkel/spring-angularjs-tutorial-10/blob/master/src/main/java/tutorial/core/models/entities/Account.java.
  • repository package - Since you are using Spring you will most likely use also since spring-data or even spring-data-jpa with Hibernate as your ORM Library. It will most likely lead you to use Repository interfaces (author of videos you watch for some reason decided not to use it though). Anyway it will be your layer to access database, for example: https://github.com/chrishenkel/spring-angularjs-tutorial-10/blob/master/src/main/java/tutorial/core/repositories/jpa/JpaAccountRepo.java
  • service package will be your package to manipulate data. It's not the best example but this layer doesn't access your database directly, it will use Repositories to do it, but it might also do other things - it will be your API to manipulate data in you application. Let's say you want to have a fancy calculation on your wallet before you save it to DB, or like here https://github.com/chrishenkel/spring-angularjs-tutorial-10/blob/master/src/main/java/tutorial/core/services/impl/AccountServiceImpl.java you want to make sure that the Blog you try to create doesn't exist yet.
  • controllers package contain all classes which will be used by DispacherServlet to take care of the requests. You will read "input" from the request, process it (use your Services here) and send your responses.
  • resource_assemblers package in this case is framework specific (Hateoas). As far as I can tell it's just a DTO for your json responses (for example you might want to store password in your Account but exposing it through json won't be a good idea, and it would happen if you didn't use DTO).

Please let me know if that is the answer you were looking for.

like image 145
SirKometa Avatar answered Nov 15 '22 13:11

SirKometa


This question may be of interest to you as well as this explanation.

You are mostly talking about the same things in each case, Spring just uses annotations so that when it scans them it knows what type of object you are creating or instantiating.

Basically everything request flows through the controller annotated with @Controller. Each method process the request and (if needed) calls a specific service class to process the business logic. These classes are annotated with @Service. The controller can instantiate these classes by autowiring them in @Autowire or resourcing them @Resource.

@Controller
@RequestMapping("/")
public class MyController {

    @Resource private MyServiceLayer myServiceLayer;

    @RequestMapping("/retrieveMain")
    public String retrieveMain() {

        String listOfSomething = myServiceLayer.getListOfSomethings();
        return listOfSomething;
    }
}

The service classes then perform their business logic and if needed, retrieve data from a repository class annotated with @Repository. The service layer instantiate these classes the same way, either by autowiring them in @Autowire or resourcing them @Resource.

@Service
public class MyServiceLayer implements MyServiceLayerService {

    @Resource private MyDaoLayer myDaoLayer;

    public String getListOfSomethings() {

        List<String> listOfSomething = myDaoLayer.getListOfSomethings();
        // Business Logic
        return listOfSomething;
    }
}

The repository classes make up the DAO, Spring uses the @Repository annotation on them. The entities are the individual class objects that are received by the @Repository layer.

@Repository
public class MyDaoLayer implements MyDaoLayerInterface {

    @Resource private JdbcTemplate jdbcTemplate;

    public List<String> getListOfSomethings() {

        // retrieve list from database, process with row mapper, object mapper, etc.
        return listOfSomething;
    }
}

@Repository, @Service, and @Controller are specific instances of @Component. All of these layers could be annotated with @Component, it's just better to call it what it actually is.

So to answer your question, they mean the same thing, they are just annotated to let Spring know what type of object it is instantiating and/or how to include another class.

like image 22
Shaggy Avatar answered Nov 15 '22 12:11

Shaggy