Spring 框架对于不少 Java 开发人员来讲都不陌生。Spring 框架包含几十个不一样的子项目,涵盖应用开发的不一样方面。如此多的子项目和组件,一方面方便了开发人员的使用,另一个方面也带来了使用方面的问题。每一个子项目都有必定的学习曲线。开发人员须要了解这些子项目和组件的具体细节,才能知道如何把这些子项目整合起来造成一个完整的解决方案。在如何使用这些组件上,并无相关的最佳实践提供指导。对于新接触 Spring 框架的开发人员来讲,并不知道如何更好的使用这些组件。Spring 框架的另一个常见问题是要快速建立一个能够运行的应用比较麻烦。Spring Boot 是 Spring 框架的一个新的子项目,用于建立 Spring 4.0 项目。它能够自动配置 Spring 的各类组件,并不依赖代码生成和 XML 配置文件。Spring Boot 也提供了对于常见场景的推荐组件配置。Spring Boot 能够大大提高使用 Spring 框架时的开发效率,对于快速开发一个可运行的非大型的项目很是合适。java
从 Spring Boot 项目名称中的 Boot 能够看出来,Spring Boot 的做用在于建立和启动新的基于 Spring 框架的项目。它的目的是帮助开发人员很容易的建立出独立运行和产品级别的基于 Spring 框架的应用。Spring Boot 会选择最适合的 Spring 子项目和第三方开源库进行整合。大部分 Spring Boot 应用只须要很是少的配置就能够快速运行起来。git
Spring Boot 包含的特性以下:github
* 建立能够独立运行的 Spring 应用。 * 直接嵌入 Tomcat 或 Jetty 服务器,不须要部署 WAR 文件。 * 尽量的根据项目依赖来自动配置 Spring 框架。 * 不须要传统的Sring项目繁多的 XML 配置文件。
经过 Spring Boot,建立新的 Spring 应用变得很是容易,并且建立出的 Spring 应用符合通用的最佳实践。web
官方文档上有这样一段介绍:spring
Learn what you can do with Spring Boot ?
Spring Boot offers a fast way to build applications. It looks at your classpath and at beans you have configured, makes reasonable assumptions about what you’re missing, and adds it. With Spring Boot you can focus more on business features and less on infrastructure.
For example:数组
- Got Spring MVC? There are several specific beans you almost always need, and Spring Boot adds them automatically. A Spring MVC app also needs a servlet container, so Spring Boot automatically configures embedded Tomcat.
- Got Jetty? If so, you probably do NOT want Tomcat, but instead embedded Jetty. Spring Boot handles that for you.
- Got Thymeleaf? There are a few beans that must always be added to your application context; Spring Boot adds them for you.
These are just a few examples of the automatic configuration Spring Boot provides. At the same time, Spring Boot doesn’t get in your way. For example, if Thymeleaf is on your path, Spring Boot adds a SpringTemplateEngine to your application context automatically. But if you define your own SpringTemplateEngine with your own settings, then Spring Boot won’t add one. This leaves you in control with little effort on your part.
Spring Boot doesn't generate code or make edits to your files. Instead, when you start up your application, Spring Boot dynamically wires up beans and settings and applies them to your application context.springbootSpring Boot doesn't generate code or make edits to your files. Instead, when you start up your application, Spring Boot dynamically wires up beans and settings and applies them to your application context.服务器
1.在IDEA中建立一个项目,和一个helloworld模块:mvc
目录结构如图:app
在pom.xml文件中引入Spring-boot的依赖:
<!-- SpringBoot 应用都要继承 spring-boot-starter-parent --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.RELEASE</version> </parent> <modelVersion>4.0.0</modelVersion> <packaging>jar</packaging> <!-- SpringBoot Web 应用须要添加以下依赖 --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!-- SpringBoot 的maven管理插件 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.lzumetal.springboot.helloworld.Application</mainClass> </configuration> </plugin> </plugins> </build>
再建立两个类:
第一个是Application.java,用来启动SpringBoot应用:
package com.lzumtal.springboot.helloworld; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Aplication.class, args); } }
第二个类HelloController.java用来响应web请求:
package com.lzumetal.springboot.helloworld.controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/home") String home() { return "Hello World!"; } @RequestMapping("/hello/{name}") String hello(@PathVariable("name") String name) { return "hello " + name + "!!!"; } }
2.运行
运行Aplication中的main()方法,控制台打印启动信息:
在浏览中访问:http://localhost:8080/home
访问:http://localhost:8080/hello/spring-boot
这样示例就成功运行了。
1.@SpringbootApplication
注解:
Spring Boot默认是扫描@SpringBootApplication
注解的类(即启动类)的同包以及子包下的类。
使用@SpringbootApplication
注解 能够解决根类或者配置类头上注解过多的问题,一个@SpringbootApplication
至关于@Configuration
,@EnableAutoConfiguratio
n和@ComponentScan
并具备他们的默认属性值
@SpringBootApplication is a convenience annotation that adds all of the following:
- @Configuration tags the class as a source of bean definitions for the application context.
- @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
- Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.
- @ComponentScan tells Spring to look for other components, configurations, and services in the hello package, allowing it to find the controllers.
2.@EnableAutoConfiguration
注解(开启自动配置):@EnableAutoConfiguration
注解的做用在于让 Spring Boot 根据应用所声明的依赖来对 Spring 框架进行自动配置,这就减小了开发人员的工做量。
The second class-level annotation is @EnableAutoConfiguration. This annotation tells Spring Boot to “guess” how you will want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration will assume that you are developing a web application and setup Spring accordingly.
Starters and Auto-Configuration
Auto-configuration is designed to work well with “Starters”, but the two concepts are not directly tied. You are free to pick-and-choose jar dependencies outside of the starters and Spring Boot will still do its best to auto-configure your application.
第二个类级别注解是@EnableAutoConfiguration
。这个注解告诉Spring Boot“猜想”将如何配置Spring,它是基于添加的jar依赖。 因为spring-boot-starter-web添加了Tomcat和Spring MVC,所以自动配置将假设正在开发一个Web应用程序并相应地设置Spring。
启动器和自动配置(Starters & Auto-Configuration):
自动配置旨在与“Starters”配合使用,但这两个概念不直接绑定。能够自由选择和选择起始者之外的jar依赖,Spring Boot仍将尽力自动配置应用程序。
3.main()方法
The main() method uses Spring Boot’s SpringApplication.run() method to launch an application. Did you notice that there wasn’t a single line of XML? No web.xml file either. This web application is 100% pure Java and you didn’t have to deal with configuring any plumbing or infrastructure.
“main”方法应用程序的最后一部分是主(main)方法。 这只是一个遵循Java约定的应用程序入口点的标准方法。main方法经过调用run来委托Spring Boot SpringApplication类。SpringApplication将引导应用程序,启动Spring,从而启动自动配置Tomcat Web服务器。须要传递Example.class做为run方法的参数来告诉SpringApplication,这是主要的Spring组件。args数组也被传递以暴露任何命令行参数。
4.继承 spring-boot-starter-parent
做用是Spring-Boot为咱们自动添加相关的依赖,若是不想使用Spring Boot中的默认版本,能够在pom.xml文件的properites标签中指定咱们本身想要引入的版本,这样就能够覆盖默认的版本。
好比想使用不一样版本的Spring Data,具体以下:
<properties> <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version> </properties>
在properties标签中还能够指定JDK编译的版本和项目的编码格式:
<properties> <!-- 指定项目编码为 UTF-8 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- 使用 java 1.8 --> <java.version>1.8</java.version> </properties>
5.spring-boot-maven-plugin 插件
能够为SpringBoog项目提供Maven的操做方式,Spring Boot Maven plugin的5个Goals
* spring-boot:repackage,默认goal。在mvn package以后,再次打包可执行的jar/war,同时保留mvn package生成的jar/war为.origin * spring-boot:run,运行Spring Boot应用 * spring-boot:start,在mvn integration-test阶段,进行Spring Boot应用生命周期的管理 * spring-boot:stop,在mvn integration-test阶段,进行Spring Boot应用生命周期的管理 * spring-boot:build-info,生成Actuator使用的构建信息文件build-info.properties
这里介绍一下 spring-boot:repackage 和 spring-boot:run 这两个。spring-boot:repackage
:能够将项目打包成fat jar(executable jar)后,命令是:mvm package spring-boot:repackage
,或者直接使用 mvm package
,(若是使用 mvn spring-boot:repackage
则会报错)。以后咱们就能够直接 经过 java -jar 的命令来启动这个 SpringBoot 项目(试了一下即便没有配置 mainClass 也是成功的)
mvn spring-boot:run
:在 pom.xml 文件所在的目录下运行该命令便可启动 SpringBoot 项目,和在 Application.java 中运行main()
方法的结果是同样的
The Spring Boot Maven plugin provides many convenient features:
- It collects all the jars on the classpath and builds a single, runnable "über-jar", which makes it more convenient to execute and transport your service.
- It searches for the public static void main() method to flag as a runnable class.
- It provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies. You can override any version you wish, but it will default to Boot’s chosen set of versions.
本文示例代码已上传到github: https://github.com/liaosilzu2...