5

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

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.

BranLakes
  • 338
  • 2
  • 15
  • Can you expand a little on what you mean by "describe the pattern to account for the use of service and resource objects - as opposed to choosing to use service, DAO and Domain objects"? In spring you still use services, DAO, and domain objects. – Shaggy Apr 16 '15 at 16:25
  • I guess i'm trying to better understand the different naming schemes. I see some folks referring to entities and DAOs while others use names like repositories or resources. It appears that there are different ways to describe the various implementations of the DAL and different names for the 'middle' layer - ie business layer, service layer. – BranLakes Apr 16 '15 at 16:38
  • Spring has some adoption of Domain Driven Design patterns terminology. Some people use the terms from the J2EE patterns book and some use the terms from Evans' DDD book. – Nathan Hughes Apr 16 '15 at 16:53
  • Nathan, that was *exactly* the direction I was looking for. A quick look at the DDD and Core J2EE patterns books you mentioned seem to indicate that the implementations I've seen have been based in one of these two camps. I take it that these are considered to be two of the more influential pattern books that would relate to spring mvc? – BranLakes Apr 16 '15 at 17:38
  • Pretty much, along with GoF, of course. thankfully most of Core J2EE Patterns is obsolete. btw you can prepend @ to the username (or just the first part of the username) in order to notify someone you're directing a comment to. – Nathan Hughes Apr 20 '15 at 19:43

3 Answers3

4

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

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

SirKometa
  • 1,857
  • 3
  • 16
  • 26
  • Thanks SirKometa. That clarifies much. You may be interested to know that you were on point - the author of the series does indeed circle back and use a repository package. – BranLakes Apr 17 '15 at 18:00
2

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.

Community
  • 1
  • 1
Shaggy
  • 1,444
  • 1
  • 23
  • 34
  • Thanks Shaggy. I appreciate the time and effort that went into writing up this explanation. It clarifies for me how the generic architectural objects tie into the spring framework. – BranLakes Apr 17 '15 at 18:04
0

I guess the architectural pattern you are looking for is Representational State Transfer (REST). You can read up on it here:

http://en.wikipedia.org/wiki/Representational_state_transfer

Within REST the data passed around is referred to as resources:

Identification of resources: Individual resources are identified in requests, for example using URIs in web-based REST systems. The resources themselves are conceptually separate from the representations that are returned to the client. For example, the server may send data from its database as HTML, XML or JSON, none of which are the server's internal representation, and it is the same one resource regardless.

Fritz Duchardt
  • 11,026
  • 4
  • 41
  • 60