Spring Boot 官方文档学习入门及使用
Posted 官小飞
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot 官方文档学习入门及使用相关的知识,希望对你有一定的参考价值。
一、内置Servlet Container:二、使用Spring Boot。 你可以像使用标准的Java库文件一样使用Spring Boot。简单的将需要的 spring-boot-*.jar 添加到classpath即可。 Spring Boot不要求任何特殊的工具集成,所以可以使用任何IDE,甚至文本编辑器。 只是,仍然建议使用build工具:Maven 或 Gradle。 Spring Boot依赖 使用此外,你仍然可以部署Spring Boot项目到任何兼容Servlet3.0+的容器。
Name Servlet Version Java Version Tomcat 8
3.1
Java 7+
Tomcat 7
3.0
Java 6+
Jetty 9.3
3.1
Java 8+
Jetty 9.2
3.1
Java 7+
Jetty 8
3.0
Java 6+
Undertow 1.3
3.1
Java 7+
org.springframework.boot
groupId
。
通常,让你的Maven POM文件继承 spring-boot-starter-parent,并声明一个或多个 Starter POMs依赖即可。Spring Boot也提供了一个可选的 Maven Plugin来创建可执行的jars。 如下:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent> <!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>


<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent> <!-- Additional lines to be added here... --> </project>




<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>



import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; @RestController @EnableAutoConfiguration public class Example @RequestMapping("/") String home() return "Hello World!"; public static void main(String[] args) throws Exception SpringApplication.run(Example.class, args);


<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>



<properties> <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version> </properties>想使用不同版本的JDK:
<!-- 使用 java 1.8 --> <java.version>1.8</java.version>2、不继承 spring-boot-starter-parent : 这种情况下,仍然可以使用dependency management,但不能使用plugin management啦。方式如下:

<dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.4.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>


<dependencyManagement> <dependencies> <!-- Override Spring Data release train provided by Spring Boot --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-releasetrain</artifactId> <version>Fowler-SR2</version> <scope>import</scope> <type>pom</type> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.4.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

另外,Spring Boot还提供了一个Maven Plugin:spring-boot-maven-plugin,用于将项目打包成fat jar(executable jar)。 继承时只需要 声明一下即可使用:

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

六、Starters 可以创建自己的Starter,但名字格式不能是 spring-boot-starter-*,而是 *-spring-boot-starter。类似Maven插件的规则。 七、自动配置 @Import 和 @ComponentScan 类似; @EnableAutoConfiguration 和 @SpringBootApplication 类似;---注意,只能使用一次,建议用在primary @Configuration class上。 注意,自动配置永远是第二位的,一旦你配置自己的东西,那自动配置的就会被覆盖。 查看自动配置都配置了什么,以及为什么,启动应用的时候加上 --debug即可。 禁用特定的自动配置:

import org.springframework.boot.autoconfigure.*; import org.springframework.boot.autoconfigure.jdbc.*; import org.springframework.context.annotation.*; @Configuration @EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class) public class MyConfiguration

@Configuration
,
@EnableAutoConfiguration
and @ComponentScan
。
-- 注意,@ComponentScan 不能凭空使用。
十、运行Spring Boot Application
1、从IDE中运行
需要导入现有Maven项目。
如果不小心运行了两次,出现端口占用问题,STS(Spring Tools Suite)使用Relaunch即可。
2、运行fat jar(executable jar)
java -jar target/xxxx.jar 注意,是在项目路径下执行。
开启
远程调试支持:
java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n -jar target/myproject-0.0.1-SNAPSHOT.jar
3、使用Maven Plugin
mvn spring-boot:run

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <opti以上是关于Spring Boot 官方文档学习入门及使用的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot 2从入门到入坟 | 基础入门篇:你会看Spring Boot的官方文档吗?
Spring Boot 2从入门到入坟 | 基础入门篇:你会看Spring Boot的官方文档吗?