At present, the Internet technology is popular, and Java Internet technology dominated by Spring framework has become the mainstream. Spring Boot, derived from Spring technology, adopts the idea of “convention is superior to configuration”, which greatly simplifies the development of Spring framework.

With the popularity of micro-service technology in recent years, Spring Boot has become a hot technology. In September 2017, there was a major release upgrade to the Spring Framework, from 4.x to 5.x. With this upgrade, the Spring Boot version was also upgraded from 1.x to 2.x in March 2018.

                                                

Spring Boot 2.x

Author: Yang Kaizhen

The updated version focuses on Spring Boot 2.x technology, and first introduces the reader into the development of Spring Boot by comparing it with traditional Spring MVC development, and then introduces the full annotation of Spring IoC and AOP respectively.

Combined with MyBatis framework detailed database programming and its transaction development, popular NoSQL (Redis and MongoDB) technology, Spring MVC technology, REST style website construction, Spring Security technology, Spring 5.x newly released WebFlux Technology, high concurrency and locking, packaging, testing, monitoring and getting started with Spring Cloud to help readers master the key points of enterprise-level Spring Boot development. The book is presented with part of the accompanying video courses explained by the author.

The origins of Spring Boot 2.x

In order to further simplify Spring development, Spring Boot was born in 2014. It is a new framework provided by the Pivotal team. It is designed to simplify the construction and development of Spring applications and cater to the popular micro-services thinking. More and more enterprises choose Spring Boot. With the launch of Spring 5.x in September 2017, Spring Boot 2018 has also launched a 2.x release, ushering in the 2.x release era.

Based on this trend, I cooperated with my friends to create and complete the integrated development of Java EE Internet Lightweight Framework: After SSM framework (Spring MVC+Spring+MyBatis) and Redis implementation, I received many suggestions from readers, former colleagues and friends in the industry. They hoped that I could write a book about Spring Boot to provide a reference for practitioners who need to learn Spring Boot. That was the genesis of this book.

Spring Boot adopts the rule of “convention is better than configuration”. In most cases, after relying on the starter provided by Spring Boot, you can use the default convention and property files to do a lot of custom configuration, which makes development easier. For deployment, Spring Boot provides an embedded server, packaged with Maven (or Grandle) to further ease enterprise deployment. For testing, it provides a rapid testing environment, further improve the efficiency of development, so it has gradually become a mainstream choice for small and medium-sized enterprises and even some large enterprises to develop.

In addition, in the Internet world, distributed has become an inevitable trend, and distributed governance and component research and development costs are not affordable by ordinary companies. Therefore, the Spring community also provides Spring Cloud distributed development components based on Spring Boot, which further simplifies enterprise-level distributed development. This makes Spring Boot and Spring Cloud stand in the mainstream direction of Internet back-end development, and are more and more favored by enterprises.

This book uses the Spring Boot version

Spring Boot is a highly concerned microservice development framework in the market, and version iteration is very frequent, which brings me great challenges to write this book. An interesting interlude prior to the publication of this book is that the latest official version of Spring Boot was 1.5.4 at the time of the book’s creation and was updated to 1.5.9 by the time OF my initial finalization, both based on the 1.x version of Spring Boot. In early March 2018, Spring Boot underwent a major release change to the official (GA) version of 2.x before the draft was reviewed. In order to keep up with The Times and make this book more referable, I decided to update the Spring Boot version of this book from the original draft 1.5.9 to 2.0.0. Therefore, this book uses version 2.0.0.release to explain. Spring Boot 2.x and Spring Boot 1.x have many differences in usage and only support JDK 8 or above, which should be noted in reading this book and practice.

Before we start the Spring Boot tutorial, let’s review the history of the Spring Framework.

The history of the Spring

Before the Spring framework was developed, Java EE was named after Sun, which was acquired by Oracle and no longer exists, but retains its name in honor of its enormous influence on Java development. EJB (Enterprise Java Beans) as a standard.

In the “distant” EJB era, developing an EJB required a large number of interfaces and configuration files, and in the EJB 2.0 era, developing an EJB required two configuration files, with the result that configuration was more work than development.

Second, EJBs run in the EJB container, whereas Sun defined JSPS and servlets run in the Web container, so you can imagine that you need to use the Web container to invoke the SERVICES of the EJB container.

This means the following disadvantages: you need to increase the configuration file of the call to get the Web container to call the EJB container; At the same time, it was necessary to develop two containers, and the development efficiency was very low due to a lot of configuration content and cumbersome specifications, which was criticized by the developers at that time. The pattern of the Web container calling the SERVICES of the EJB container is destined to be passed over the network, resulting in poor performance. It is also difficult for testers to configure and test ejBs that require a lot of detail.

In 2002, Australian engineer Rod Johnson (who should have been a musician, Because he has a PhD in music) introduced the concept of Spring in his famous book Expert One-To-one J2EE Design and Development.

Then in 2004, the Rod Johnson-led Spring project came out with version 1.0, which completely changed the World of Java EE development, quickly abandoning the heavy EJB standards in favor of the Spring framework, Spring became the standard for Java EE development in the real world.

Spring manages Java resources with a powerful inversion of control (IoC) that reduces the coupling of resources; It also provides very low invassibility, meaning that code developed using the Spring framework can continue to be used without the Spring API.

Spring’s section-oriented programming (AOP) enhances beans by allowing configuration programming by convention through dynamic proxy techniques, erasing a lot of repetitive code, such as database programming that requires a lot of try… The catch… Finally… Statements and database transactions control the code logic, allowing developers to focus more on business development rather than resource functionality.

Spring also provides many templates that integrate popular frameworks at that time, such as HibernateTemplate template for persistence layer Hibernate, SqlMapClientTemplate template for iBATIS, etc., which greatly integrates and simplifies the use of mainstream technologies at that time. Make it show a strong vitality, and continue to this day.

It is worth mentioning that the EJB 3.0 specification also introduced the ideas of Spring and integrated the ideas of Hibernate framework, but failed to reverse its decline, mainly because its specification is still relatively rigid and it is difficult to integrate other open source frameworks. Second, it runs in an EJB container, which is still difficult to use and does not perform well.

Annotations are XM again

In the early 1.x versions of Spring, however, XML was the only option because the JDK at the time did not support annotations. Soon, as the JDK was upgraded to JDK5 and annotations were added, annotations became widely used, and Spring was divided internally between proponents of XML and proponents of annotations.

In order to simplify development, annotations have been introduced in versions after Spring 2.x, but only a small number of annotations (@Component, @service, etc.) are not powerful enough, so most Spring development is based on XML, and annotations are secondary.

With The introduction of more annotation capabilities in Spring 3.0, there was a big split in Spring over whether to use annotations or XML. Some people find the introduction of XML too cumbersome, while the use of annotations can lead to annotations being scattered all over the place and out of control, sometimes requiring a lot of understanding of the internal implementation of the framework to accurately use the functionality needed for annotation development.

At this time, there was an unwritten consensus that annotations should be used for business classes. For example, for MVC development, Controller should use @Controller, business layer should use @Service, persistence layer should use @repository. For some common beans, for example, for databases (such as Redis), third-party resources, etc., XML is used for configuration, which is still widely used in enterprises today.

Perhaps using annotations versus XML is a perennial debate, but it makes sense anyway.

With the enhancement of annotation capabilities, especially the Servlet 3.0 specification, The Web container can be removed from the deployment of Web.xml, so that the Web container can be completely developed based on annotations. With Spring 3.x and Spring 4.x versions, annotations are becoming more powerful and less dependent on XML. With the 4.x release, you can even move away from XML altogether, so using annotations for development in Spring has become a dominant feature.

At the same time, the Pivotal team continued to simplify the development of the Spring framework, mainly through annotations, by developing Spring Boot on top of the original Spring framework, so that Spring Boot was not a replacement for the Spring framework, It’s about making the Spring framework easier to use quickly.

The Pivotal team released version 1.0 of Spring Boot in 2014, which uses a specific way to configure it so that developers don’t have to define boilerplate configurations. In March 2018, Spring Boot released the 2.0.0 GA release, which is based on Spring 5 and introduces its latest features to effectively support Java 9 development.

Spring Boot is committed to becoming an industry leader in the booming field of Rapid Application development, leveraging Java EE’s strong position on the enterprise Internet, and is one of the most surprising projects in Java development in recent years.

With the popularity of microservices in recent years, more and more enterprises need rapid development, and Spring Boot has other bindings besides annotation-oriented development. For example, the server is bound and the default Spring maximization configuration, so developers can develop code, publish and test their projects as quickly as possible.

This is in line with today’s rapid development, testing and deployment of microservices, so more and more enterprises are choosing Spring Boot as the development model, which makes Spring Boot even more prosperous. The focus of this book is on Spring Boot, an exciting development tool.

Advantages of Spring Boot

Speaking of Spring Boot, let’s first understand its advantages. According to the official documentation, Spring Boot has the following advantages:

Create a separate Spring application;

Embedded Tomcat, Jetty, or Undertow without deploying a WAR file;

Allows you to get a starter as needed via Maven.

Automatically configure Spring whenever possible;

Provide production-ready functions such as metrics, health checks, and external configuration;

There is absolutely no code generation and no configuration required for XML.

Spring Boot is an application based on the Spring framework. It is embedded in servers such as Tomcat, Jetty or Undertow, and does not require a traditional WAR file for deployment. This means that you don’t need to download a traditional server like Tomcat to set up a Spring Boot project.

It also provides a Maven (or Grandle) dependent starter, which can directly obtain the relevant packages required for development. Through these starter projects, Spring Boot projects can be run as Java Applications without additional server configuration.

For configuration, Spring Boot provides the maximum automatic configuration of the Spring framework, and makes extensive use of automatic configuration to minimize the configuration of Spring by developers.

Monitoring, automatic detection, and external configuration are also provided without additional code and XML configuration requirements.

Convention over configuration is the dominant philosophy of Spring Boot. For Spring Boot, there is a default configuration in most cases, and you can use the Spring framework without any definition at all. If you need to customize it, you just need to configure some properties in the configuration file, which is very convenient.

Spring Boot provides a starter dependency for the functionality necessary to deploy these projects. For example, spring-boot-starter-Web bundles the packages that Spring MVC relies on, Spring-boot-starter-tomcat is bundled with embedded Tomcat, which enables developers to set up a development environment as quickly as possible and develop and deploy it quickly. This is the characteristic of Spring Boot. Maybe you, as a traditional developer, don’t get it yet, but that doesn’t matter.

To demonstrate Spring Boot’s features, the next section presents and compares a traditional Spring MVC project with a simple Spring Boot starter example.

Traditional Spring MVC vs. Spring Boot

In traditional Spring MVC project development, to develop a simple Spring MVC project, you need to configure the DispatcherServlet, and also need to configure the Spring IoC container. You can choose to use the configuration of web.xml, of course, if you are using the Servlet 3.1 specification, Can also be inherited by Spring MVC AbstractAnnotationConfigDispatcherServletInitializer to configure Spring MVC project.

Here are some examples of code that will work, even if you are not familiar with Spring MVC. This is just to show that development is a bit of a hassle.

Assuming you have imported the required Spring and Spring MVC dependencies into your project, you are ready to configure the DispatcherServlet. For example, listing 1-1 is the way through inheritance AbstractAnnotationConfigDispatcherServletInitializer to configure Spring MVC DispatcherServlet.

Listing 1-1 Configuring Spring MVC Note the bold areas in the code. A Java configuration file, webconfig.java, is introduced to configure the context of Spring MVC’s core DispatcherServlet class, as shown in Listing 1-2.

Listing 1-2 configures the context of the DispatcherServlet

                                              

After configuring the Spring MVC development environment, you can develop the Spring MVC Controller Controller, as shown in Listing 1-3.

Listing 1-3 develops the Spring MVC controller

                                                 

This completes the development of a traditional Spring MVC, but you will need a third party server such as Tomcat, WebLogic, etc to deploy your project. After the restart the server, and then open a browser and enter the corresponding URL, such as the project name is enter http://localhost:8080/SpringMVC/test.do for SpringMVC, can get the page shown in figure 1-1.

                                 

Figure 1-1 Testing a traditional Spring MVC project

From this point of view, traditional Spring MVC development requires a lot of configuration and requires a lot of designers. After the development, developers need to find the corresponding server to run, such as Tomcat or Jetty, which requires not only development, but also configuration and deployment, and the workload is quite a lot.

With Spring Boot, you’ll see how easy it can be. We don’t need to go into too much detail at the beginning of the process, but that’s for the future, so I’ll just show you how simple it is. Start by creating a Maven project in the IDE and defining its name as Chapter1, so you can see a Maven configuration file pom.xml and modify its contents to the code shown in Listing 1-4.

Listing 1-4 configures the Spring Boot dependency environment

                                             

As you can see from the code in boldface, Maven’s configuration file introduces multiple Spring Boot starters. Spring Boot will find the corresponding dependency based on Maven’s configured starter, and load the corresponding JAR package into the project. It will also load the bound server into the project. You don’t have to deal with that anymore. Bind the server, as Spring Boot promises, and implement as much configuration as Spring can, convention over configuration. Here we only need to develop a single class to run the Spring Boot application, creating a new class — Chapter1Main, as shown in Listing 1-5.

Listing 1-5 developing a Spring Boot application

                                              

Well, this introductory example is done. If you haven’t been exposed to Spring Boot, you’ll be surprised. Is that how Spring MVC is configured? I can answer you with: “Yes, that’s done, now you can run the Chapter1Main class as Java Application.” Here is the Spring Boot run log:

                                               

As you can see from the log, Tomcat has started and loaded our Chapter1Main as the Spring MVC controller, and mapped the corresponding path (/test) to the developed test method. So, it’s time to test. Open a browser, type http://localhost:8080/test in the address bar, you can see the results as shown in figure 1-2.

                                               

Figure 1-2Spring Boot running results

Is it very different from traditional Spring MVC? As you can see from the comparison above, Spring Boot has the advantage of allowing direct development. Where traditional configuration is required, Spring Boot has conventions, meaning that you can develop and run your projects in the same way as Spring Boot conventions do.

It also provides some quick configuration conventions when you need to change the configuration, as it promises to do as much as possible to configure the Spring project and bind to the corresponding server, allowing developers to work with less configuration and more directly on the project.

For microservices, projects like this are preferred for quick setup, and Spring Boot offers this possibility, along with monitoring capabilities.

With the advent of Cloud technology, microservice has become a hot spot in the market, so the era of Spring Boot microservice development, which represents the Era of Java microservice, has arrived. Combined with Spring Cloud, it can also easily build distributed system development, meeting the needs of most enterprises that are unable to independently develop distributed architecture. So it’s definitely exciting.

                                                      

Spring Boot 2.x

Author: Yang Kaizhen

The Spring framework is the de facto standard for Java EE development, and Spring Boot is the industry’s most popular microservices development framework.

This book starts with a simple project to explain Spring Boot enterprise development, Its content includes Fully annotated Spring IoC and AOP, database programming, database transactions, NoSQL technology, Spring MVC, Spring 5 new generation of responsive framework WebFlux, REST style and Spring Cloud distributed development, etc.