Spring Boot简介
Spring Boot won’t replace the Spring Framework. That’s because Spring Bootisthe Spring Framework! You
can view Spring Boot as a new way to create Spring applications with ease.
Spring Boot simplifies the way you develop, because it makes it easy to create production-ready Spring-based applications that you canjust run. You will find out that, with Spring Boot, you can create standalone applications that use an embedded server, making them 100% runnable applications. I will talk about this in several chapters of the book. One of its best features is that Spring Boot is an “opinionated” technology in that it will help you follow the best practices for creating robust, extensible, and scalable Spring applications.
can view Spring Boot as a new way to create Spring applications with ease.
Spring Boot simplifies the way you develop, because it makes it easy to create production-ready Spring-based applications that you canjust run. You will find out that, with Spring Boot, you can create standalone applications that use an embedded server, making them 100% runnable applications. I will talk about this in several chapters of the book. One of its best features is that Spring Boot is an “opinionated” technology in that it will help you follow the best practices for creating robust, extensible, and scalable Spring applications.
spring boot 基于spring框架,是一种新的创建spring应用的新方式,这种方式主要是基于maven的设计哲学--约定优于配置。一行代码,一个注解就可以运行一个spring boot应用,其底层的配置,spring boot默认基于规则帮我们自动化处理了,避免了繁琐的xml或java 方式的配置。
Spring Applications
If you are a Spring developer like me, you already know that in order to create a simple Spring webapplication, you must follow certain rules of the J2EE stack and some of the Spring Framework. Those rules
include the following:
过去,我们创建一个spring web应用,要按照一系列步骤:
Create a folder structure that contains your WAR (Web ARchive):
• It must contain aWEB-INFfolder withlib and classessubfolders that contain the third-party libraries and
Create a folder structure that contains your WAR (Web ARchive):
• It must contain aWEB-INFfolder withlib and classessubfolders that contain the third-party libraries and
your web application classes, respectively.
• Some JSP (if needed), HTML, CSS, images, and JavaScript (if needed) files.
• A file namedweb.xmlthat will contain the Springorg.springframework.web.servlet.DispatcherServletclass.
• Your Spring beans in the form<servlet-name>-servlet.xml(of course, you can
override this and add the complete location and name of your Spring beans).
• Some JSP (if needed), HTML, CSS, images, and JavaScript (if needed) files.
• A file namedweb.xmlthat will contain the Springorg.springframework.web.servlet.DispatcherServletclass.
• Your Spring beans in the form<servlet-name>-servlet.xml(of course, you can
override this and add the complete location and name of your Spring beans).
Spring Boot to the Rescue
现在我们创建一个spring boot 应用:package com.sdcuike.spring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SimpleWebApp {
public static void main(String[] args) {
SpringApplication.run(SimpleWebApp.class, args);
}
}
基本上一行代码,一个注解搞定。
Why Spring Boot?
Spring Boot has many features that make it suitable for:• Cloud Native Applications that follow the 12 factor patterns (developed by the Netflix
engineering team at http://12factor.net/)
• Productivity increases by reducing time of development and deployment
• Enterprise-production-ready Spring applications
• Non-functional requirements, such as the Spring Boot Actuator (a module that
brings metrics, health checks, and management easily) and embedded containers
for running web applications (such as Tomcat, Undertow, Jetty, etc.)
The term “microservices” is getting attention for creating scalable, highly available, and robust
applications, and Spring Boot fits there perfectly by allowing developers to focus only on the business logic
and to leave the heavy lifting to the Spring Framework.
Spring Boot Features
Spring Boot has a lot of features that you’ll learn about in the following chapters, and here is just a taste:
• The SpringApplicationclass. I showed you that in a Java Spring Boot application, the main method executes this singleton class. This particular class provides a convenient way to initiate a Spring application.
启动很方便。
• Spring Boot allows you to create applications without requiring any XML configuration. Spring Boot doesn’t generate code.
无需xml配置,利用java默认的配置或定制配置即可。
• Spring Boot provides a fluent builder API through theSpringApplicationBuilder singleton class that allows you to
create hierarchies with multiple application
contexts. This particular feature is related to the Spring Framework and how it works
internally. If you are a Spring developer already, you’ll learn more about this feature
in the following chapters. If you are new to Spring and Spring Boot, you just need to
know that you can extend Spring Boot to get more control over your applications.
• Spring Boot offers you more ways to configure the Spring application events and listeners. This will be explained
in more detail in the following chapters.
• I mentioned that Spring Boot is an “opinionated” technology, which means that Spring Boot will attempt to create the right type of application, either a web application (by embedding a Tomcat or Jetty container) or a single application.
• The ApplicationArgumentsinterface. Spring Boot allows you to access any application arguments. This is useful when you want to run your application with some parameters. For example, you can use--debug mylog.txtor --audit=true and have access to those values.
• Spring Boot allows you to execute code after the application has started. The only thing you need to do is implement theCommandLineRunnerinterface and provide the implementation of the run(String ...args)method. A particular example is to initialize some records in a database as it starts or check on some services and see if
they are running before your application starts.
• Spring Boot allows you to externalize configurations by using an application.propertiesorapplication.ymlfile.
More about this in the following chapters.
• You can add administration-related features, normally through JMX. You do this simply by enabling thespring.application.admin.enabledproperty in the application.propertiesorapplication.ymlfiles.
• Spring Boot allows you to have profiles that will help your application run in different environments.
• Spring Boot allows you to configure and use logging very simply.
• Spring Boot provides a simple way to configure and manage your dependencies by using starter poms. In other words, if you are going to create a web application, you only need to include thespring-boot-start-webdependency in your Maven pom or Gradle build file.
• Spring Boot provides out-of-the-box non-functional requirements by using the Spring Boot Actuator, so you can see the health, memory, and so on, of your application.
• Spring Boot provides@Enable<feature>annotations that help you to include, configure, and use technologies like databases (SQL and NoSQL), caching, scheduling, messaging, Spring integration, batching, and more.
As you can see, Spring Boot has all these features and more, and you’ll learn more about these features in
the following chapters. Now it’s time to start learning more about Spring Boot by seeing how it works internally
the following chapters. Now it’s time to start learning more about Spring Boot by seeing how it works internally