N-tier architecture with Spring

One of the ways of working with the Spring framework is by using an form of the N-tier architecture pattern. We can conceptualize it like this:

n-tiers layers

When our frontend framework sends a request to the server it reaches our controller. The controller is annotated with @RestController. This separtes the UI from the backend logic. This makes it possible to sue the same controller for a webfrontend, a desktop frontend or a CLI (command line interface).

The controller has a service component autowired which can help him return the request.

A service component is annotated with @Service. It it interacts with data through an autowired repository and contains the businesses logic. Another name for this layer is the business layer.

A repository component is annotated with @Repository. It interacts with the chosen way to persist data. Another name for this layer is the data access layer.

We can replace classes in any of the layers with new classes that confirm to the same interface. This makes managing an application a lot easier, because you are dealing with the same level of abstraction all the time. Imagine dealing with database interaction in the same class as your UI. It’s a great way to create spaghetti code.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s