Understanding Spring Web Application Architecture: The Classic Way--转载
原文地址:http://www.petrikainulainen.net/software-development/design/understanding-spring-web-application-architecture-the-classic-way/
Every developer must understand two things:
- Architecture design is necessary.
- Fancy architecture diagrams don’t describe the real architecture of an application.
The real architecture is found from the code that is written by developers, and if we don’t design the architecture of our application, we will end up with an application that has more than one architecture.
Does this mean that developers should be ruled by architects?
No. Architecture design is far too important to be left to the architects, and that is why every developer, who wants to be more than just a type writer, must be good at it.
Let’s start our journey by taking a look at the two principles that will help us to design a better and a simpler architecture for our Spring powered web application.
The Two Pillars of a Good Architecture
Architecture design can feel like an overwhelming task. The reason for this is that many developers are taught to believe that architecture design must be done by people who are guardians of a mystical wisdom. These people are called software architects.
However, the task itself isn’t so complicated than it sounds:
Software architecture is the high level structure of a software system, the discipline of creating such a high level structure, and the documentation of this structure.
Although it is true that experience helps us to create better architectures, the basic tools of an architecture design are actually quite simple. All we have to do is to follow these two principles:
1. The Separation of Concerns (SoC) Principle
The Separation of Concerns (SoC) principle is specified as follows:
Separation of concerns (SoC) is a design principle for separating a computer program into distinct sections, such that each section addresses a separate concern.
This means that we should
- Identify the “concerns” that we need to take care of.
- Decide where we want to handle them.
In other words, this principle will help us the identify the required layers and the responsibilities of each layer.
2. The Keep It Simple Stupid (KISS) principle
The Keep It Simple Stupid (KISS) principle states that:
Most systems work best if they are kept simple rather than made complicated; therefore simplicity should be a key goal in design and unnecessary complexity should be avoided.
This principle is the voice of reason. It reminds us that every layer has a price, and if we create a complex architecture that has too many layers, that price will be too high.
In other words, we should not design an architecture like this:
I think that John, Judy, Marc, and David are guilty of mental masturbation. They followed the separation of concerns principle, but they forgot to minimize the complexity of their architecture. Sadly, this is a common mistake, and its price is high:
- Adding new features takes a lot longer than it should because we have to transfer information through every layer.
- Maintaining the application is pain in the ass impossible because no one really understands the architecture, and the ad-hoc decisions, that are made every, will pile up until our code base looks like a big pile of shit that has ten layers.
This raises an obvious question:
What kind of an architecture could serve us well?
Three Layers Should Be Enough for Everybody
If think about the responsibilities of a web application, we notice that a web application has the following “concerns”:
- It needs to process the user’s input and return the correct response back to the user.
- It needs an exception handling mechanism that provides reasonable error messages to the user.
- It needs a transaction management strategy.
- It needs to handle both authentication and authorization.
- It needs to implement the business logic of the application.
- It needs to communicate with the used data storage and other external resources.
We can fulfil all these concerns by using “only” three layers. These layers are:
- The web layer is the uppermost layer of a web application. It is responsible of processing user’s input and returning the correct response back to the user. The web layer must also handle the exceptions thrown by the other layers. Because the web layer is the entry point of our application, it must take care of authentication and act as a first line of defense against unauthorized users.
- The service layer resides below the web layer. It acts as a transaction boundary and contains both application and infrastructure services. The application services provides the public API of the service layer. They also act as a transaction boundary and are responsible of authorization. Theinfrastructure services contain the “plumbing code” that communicates with external resources such as file systems, databases, or email servers. Often these methods are used by more than a one application service.
- The repository layer is the lowest layer of a web application. It is responsible of communicating with the used data storage.
The high level architecture of a classic Spring web application looks as follows:
The next thing that we have to do is to design the interface of each layer, and this is the phase where we run into terms like data transfer object (DTO) and domain model. These terms are described in the following:
- A data transfer object is an object that is just a simple data container, and these objects are used to carry data between different processes and between the layers of our application.
- A domain model consists of three different objects:
- A domain service is a stateless class that provides operations which are related to a domain concept but aren’t a “natural” part of an entity or a value object.
- An entity is an object that is defined by its identity which stays unchanged through its entire lifecycle.
- A value object describes a property or a thing, and these objects don’t have their own identity or lifecycle. The lifecycle of a value object is bound to the lifecycle of an entity.
Now that we know what these terms mean, we can move on and design the interface of each layer. Let’s go through our layers one by one:
- The web layer should handle only data transfer objects.
- The service layer takes data transfer objects (and basic types) as method parameters. It can handle domain model objects but it can return only data transfer objects back to the web layer.
- The repository layer takes entities (and basic types) as method parameters and returns entities (and basic types).
This raises one very important question:
Do we really need data transfer objects? Why cannot we just return entities and value objects back to the web layer?
There are two reasons why this is a bad idea:
- The domain model specifies the internal model of our application. If we expose this model to the outside world, the clients would have to know how to use it. In other words, the clients of our application would have to take care of things that don’t belong to them. If we use DTOs, we can hide this model from the clients of our application, and provide an easier and cleaner API.
- If we expose our domain model to the outside world, we cannot change it without breaking the other stuff that depends from it. If we use DTOs, we can change our domain model as long as we don’t make any changes to the DTOs.
The “final” architecture of a classic Spring web application looks as follows:
There Are Many Unanswered Questions Left
This blog post described the classic architecture of a Spring web application, but it doesn’t provide any answers to the really interesting questions such as:
- Why the layer X is responsible of the concern Y?
- Should our application have more than three or less than three layers?
- How should we design the internal structure of each layer?
- Do we really need layers?
The reason for this is simple:
We must learn to walk before we can run.
The next blog posts of this tutorial will answer to these questions.
Understanding Spring Web Application Architecture: The Classic Way--转载的更多相关文章
- interface21 - web - ContextLoaderListener(Spring Web Application Context加载流程)
前言 最近打算花点时间好好看看spring的源码,然而现在Spring的源码经过迭代的版本太多了,比较庞大,看起来比较累,所以准备从最初的版本(interface21)开始入手,仅用于学习,理解其设计 ...
- What is Web Application Architecture? How It Works, Trends, Best Practices and More
At Stackify, we understand the amount of effort that goes into creating great applications. That’s w ...
- 在eclipse中运行spring web application时的异常: java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis ...
- Web application architecture overview
- Spring Security(三十七):Part IV. Web Application Security
Most Spring Security users will be using the framework in applications which make user of HTTP and t ...
- Spring REST实践之Spring Web MVC
Spring概要 Spring Framework提供了依赖注入模型和面向切面编程,简化了基础型代码的编写工作以及更好的能够与其它框架和技术整合起来.Spring Framework由data acc ...
- SPRING IN ACTION 第4版笔记-第五章Building Spring web applications-001-SpringMVC介绍
一. 二.用Java文件配置web application 1. package spittr.config; import org.springframework.web.servlet.suppo ...
- Docker---(4)Docker 部署spring web项目
原文:Docker---(4)Docker 部署spring web项目 版权声明:欢迎转载,请标明出处,如有问题,欢迎指正!谢谢!微信:w1186355422 https://blog.csdn.n ...
- Spring Security(二十八):9.4 Authentication in a Web Application
Now let’s explore the situation where you are using Spring Security in a web application (without we ...
随机推荐
- C++调用Matlab引擎及Eigen配置
这个周开始要着手实现网格水印的代码了,虽然还什么都不会,但也只能一步步摸索着往前走了. 我要实现的论文题目是<<Watermarking 3D Polygonal Meshes in th ...
- 关于ShareSDK接入的各种问题,以及解决方案
随着社交网络的流行,游戏接入分享已经是必然.毕竟这是非常好的一种推广方式.ShareSDK是一个非常好的内分享提供商!但是接入后发生的各种问题,下面给大家提供几个本人遇到的问题,以及解决方法: 1)微 ...
- 安装 RabbitMQ C#使用-摘自网络(包括RabbitMQ的配置)
1.什么是RabbitMQ.详见 http://www.rabbitmq.com/ . 作用就是提高系统的并发性,将一些不需要及时响应客户端且占用较多资源的操作,放入队列,再由另外一个线程,去异步处理 ...
- jdk+jira配置
1.JDK.JIRA.MySQL安装完毕,停止JIRA服务 创建数据库:mysqlcreate database jiradb character set ‘UTF8′; 创建用户并赋与权限:crea ...
- mysql performance_schema 初探
mysql performance_schema 初探: mysql 5.5 版本 新增了一个性能优化的引擎: PERFORMANCE_SCHEMA 这个功能默认是关闭的: 需要设置参数: perf ...
- hashCode()和equals()的用法
使用hashCode()和equals() hashCode()和equals()定义在Object类中,这个类是所有java类的基类,所以所有的java类都继承这两个方法. hashCode()方法 ...
- c# 如何使用DLL的config文件中的信息
我知道用c#编写的exe程序可以读取config文件中的配置信息,比如Test.exe,可以在与Test.exe相同目录下放置一个config文件:Test.exe.config,用System.Co ...
- Nginx NLB 及Redis学习
负载均衡: ARR: 微软的应用级别的负载均衡方案 NLB:服务器级别的负载均衡方案 Nginx:反向代理 达到负载均衡. Redis:用作缓存(Redis 主从配置和参数详解 http://www. ...
- heidsoft logo
- java中的定时器
所有类型的 Java 应用程序一般都需要计划重复执行的任务.企业应用程序需要计划每日的日志或者晚间批处理过程.一个 J2SE或者 J2ME 日历应用程序需要根据用户的约定计划闹铃时间.不过,标准的调度 ...