一、配置banner
1.1、在resources下新建banner.txt
__ _ _______ ____ _________ ____
\ \/ \/ /\__ \ / ___\ / ___\__ \ / ___\
\ / / __ \_/ /_/ > /_/ > __ \_/ /_/ >
\/\_/ (____ /\___ /\___ (____ /\___ /
\//_____//_____/ \//_____/
1.2、启动springboot即可看到自定义的banner
二、Web容器配置
2.1、springboot默认使用Tomcat容器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>
2.2、配置jetty容器
配出tomcat依赖,配置jetty依赖即可
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
<version>2.1.7.RELEASE</version>
</dependency>
<!--配置jetty服务器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
2.3、配置Undertow容器
Undertow 是一个采用 Java 开发的灵活的高性能 Web 服务器,提供包括阻塞和基于 NIO 的非堵塞机制。Undertow 是红帽公司的开源产品,是 Wildfly默认的 Web 服务器。Undertow 提供一个基础的架构用来构建 Web 服务器,这是一个完全为嵌入式设计的项目,提供易用的构建器 API,完全兼容 Java EE Servlet 3.1 和低级非堵塞的处理器。
排除Tomcat的依赖,配置undertow即可
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
<version>2.1.7.RELEASE</version>
</dependency>
<!--配置UnderTow服务器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
三、类型安全配置属性
Spring提供了@Value将数据注入到属性上的方法,SpringBoot对此进行了改进,提出类类型安全匹配属性。
3.1、在application.properties添加如下配置
user.name=waggag
user.age=23
3.2、将数据注入到JavaBean中
package cn.waggag.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "user")
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
3.3、使用Controller简单测试
package cn.waggag.controller;
import cn.waggag.config.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private User user;
@GetMapping("/user")
public String user(){
return user.toString();
}
}
3.4、测试结果