BranLakes
BranLakes

Reputation: 358

Identifying Spring MVC architecture pattern

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.

Upvotes: 5

Views: 1147

Answers (3)

Shaggy
Shaggy

Reputation: 1464

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.

Upvotes: 2

SirKometa
SirKometa

Reputation: 1887

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.

Upvotes: 4

Fritz Duchardt
Fritz Duchardt

Reputation: 11950

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.

Upvotes: 0

Related Questions