第一个 Spring Boot 应用程序
#本节视频
#概述
这里我们使用 Intellij IDEA 来新建一个 Spring Boot 项目。
#打开 IDEA -> New Project -> Spring Initializr
#填写项目信息
#选择 Spring Boot 版本及 Web 开发所需的依赖
#保存项目到指定目录
#工程目录结构
创建完成后的工程目录结构如下:
│ .gitignore
│ pom.xml
│
│
└─src
├─main
│ ├─java
│ │ └─com
│ │ └─funtl
│ │ └─hello
│ │ └─spring
│ │ └─boot
│ │ HelloSpringBootApplication.java
│ │
│ └─resources
│ │ application.properties
│ │
│ ├─static
│ └─templates
└─test
└─java
└─com
└─funtl
└─hello
└─spring
└─boot
HelloSpringBootApplicationTests.java
- .gitignore:Git 过滤配置文件
- pom.xml:Maven 的依赖管理配置文件
- HelloSpringBootApplication.java:程序入口
- resources:资源文件目录
- static: 静态资源文件目录
- templates:模板资源文件目录
- application.properties:Spring Boot 的配置文件,实际开发中会替换成 YAML 语言配置(application.yml)
#pom.xml
<?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.funtl</groupId> <artifactId>hello-spring-boot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>hello-spring-boot</name> <description></description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
- parent:继承了 Spring Boot 的 Parent,表示我们是一个 Spring Boot 工程
spring-boot-starter-web
:包含了spring-boot-starter
还自动帮我们开启了 Web 支持
#功能演示
我们创建一个 Controller 来演示一下 Spring Boot 的神奇功能
package com.funtl.hello.spring.boot.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping(value = "", method = RequestMethod.GET) public String sayHi() { return "Hello Spring Boot"; } }
启动 HelloSpringBootApplication
的 main()
方法,浏览器访问 http://localhost:8080 可以看到:
Hello Spring Boot
#神奇之处
- 没有配置 web.xml
- 没有配置 application.xml,Spring Boot 帮你配置了
- 没有配置 application-mvc.xml,Spring Boot 帮你配置了
- 没有配置 Tomcat,Spring Boot 内嵌了 Tomcat 容器
Spring Boot 常用配置
#本节视频
#概述
本章节主要介绍一下 Spring Boot 中的一些常用配置,比如:自定义 Banner、配置日志、关闭特定的自动配置等。
#自定义 Banner
在 Spring Boot 启动的时候会有一个默认的启动图案
. ____ _ __ _ _ /\ / ___'_ __ _ _(_)_ __ __ _ ( ( )\___ | '_ | '_| | '_ / _` | \/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.8.RELEASE)
我们在 src/main/resources
目录下新建一个 banner.txt
通过 http://patorjk.com/software/taag 网站生成字符串,将网站生成的字符复制到 banner.txt 中
再次运行这个程序
${AnsiColor.BRIGHT_RED} //////////////////////////////////////////////////////////////////// // _ooOoo_ // // o8888888o // // 88" . "88 // // (| ^_^ |) // // O = /O // // ____/`---'\____ // // .' \| |// `. // // / \||| : |||// // // / _||||| -:- |||||- // // | | \ - /// | | // // | \_| ''---/'' | | // // .-\__ `-` ___/-. / // // ___`. .' /--.-- `. . ___ // // ."" '< `.___\_<|>_/___.' >'"". // // | | : `- \`.;` _ /`;.`/ - ` : | | // // `-. \_ __ /__ _/ .-` / / // // ========`-.____`-.___\_____/___.-`____.-'======== // // `=---=' // // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // // 佛祖保佑 永不宕机 永无BUG // ////////////////////////////////////////////////////////////////////
常用属性设置:
${AnsiColor.BRIGHT_RED}
:设置控制台中输出内容的颜色${application.version}
:用来获取MANIFEST.MF
文件中的版本号${application.formatted-version}
:格式化后的${application.version}
版本信息${spring-boot.version}
:Spring Boot 的版本号${spring-boot.formatted-version}
:格式化后的${spring-boot.version}
版本信息
#配置文件
Spring Boot 项目使用一个全局的配置文件 application.properties
或者是 application.yml
,在 resources
目录下或者类路径下的 /config
下,一般我们放到 resources
下。
修改 Tomcat 的端口为 9090,并将默认的访问路径 "/" 修改为 "boot",可以在 application.properties
中添加:
server.port=9090
server.context-path=/boot
或在 application.yml 中添加:
server: port: 9090 context-path: /boot
测试效果:
#Starter POM
Spring Boot 为我们提供了简化企业级开发绝大多数场景的 starter pom ,只要使用了应用场景所需要的 starter pom ,相关的技术配置将会消除,就可以得到 Spring Boot 为我们提供的自动配置的 Bean。
#日志配置
Spring Boot 对各种日志框架都做了支持,我们可以通过配置来修改默认的日志的配置
默认情况下,Spring Boot 使用 Logback 作为日志框架
logging: file: ../logs/spring-boot-hello.log level.org.springframework.web: DEBUG
#关闭特定的自动配置
关闭特定的自动配置使用 @SpringBootApplication
注解的 exclude
参数即可,这里以关闭数据源的自动配置为例
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})