
通过 Spring Boot 可以快速构建⼀个基于 Spring 框架的 Java Application,简化配置,⾃动装配。
JavaConfiguration ⽤ Java 类替代 XML 的配置⽅式。
Spring Boot 对常⽤的第三⽅库提供了配置⽅案,可以很好地和 Spring 进⾏整合,⼀键式搭建功能完备的 Java 企业级应⽤。
开箱即⽤是 Spring Boot 的特点
package com.southwind.springboot.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloHandler {
@GetMapping("/index")
public String index(){
return "Hello Spring Boot";
}
}
package com.southwind.springboot.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloHandler {
@GetMapping("/index")
public String index(){
return "Hello Spring Boot";
}
}
启动类必须覆盖所有与业务相关的类:启动类所在的包必须是业务类所在包的同包或者⽗包,如果没有覆盖,业务类就不会⾃动装配到 IoC 容器中。
自定义banner
Properties
#端⼝ server.port=8181 #项⽬访问路径 server.servlet.context-path=/springboot #cookie失效时间 server.servlet.session.cookie.max-age=100 #session失效时间 server.servlet.session.timeout=100 #编码格式 server.tomcat.uri-encoding=UTF-8
YAML
YAML 是不同于 Properties 的另外⼀种⽂件格式,同样可以⽤来写配置⽂件,Spring Boot 默认⽀持YAML 格式,YAML 的优点在于编写简单,结构清晰,利⽤缩紧的形式来表示层级关系。
相⽐于 Properties,YAML 可以进⼀步简化配置⽂件的编写,更加⽅便。
server:
port: 8181
servlet:
context-path: /springboot
session:
cookie:
max-age: 100
timeout: 100
tomcat:
uri-encoding: UTF-8
需要注意的是 YAML 格式书写规范⾮常严格,属性名和属性值之间必须⾄少⼀个空格。
如果 Properties 和 YAML 两种类型的⽂件同时存在,Properties 的优先级更⾼。
配置⽂件除了可以放置在 resources 路径下之外,还有 3 个地⽅可以放置,如下图所示。
优先级顺序如下所示:
package com.southwind.springboot.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloHandler {
@Value("${server.port}")
private String port;
@GetMapping("/index")
public String index(){
return "当前服务的端⼝是:"+this.port;
}
}
@Value 注解同样适⽤于 Properties ⽂件。
Spring Boot 整合 JSPSpring Boot 与视图层的整合
org.springframework.boot spring-boot-starter-parent 2.2.4.RELEASE org.springframework.boot spring-boot-starter-web 2.2.4.RELEASE org.apache.tomcat.embed tomcat-embed-jasper 9.0.19
package com.southwind.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/hello")
public class HelloHandler {
@GetMapping("/index")
public ModelAndView index(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
modelAndView.addObject("mess","Hello Spring Boot");
return modelAndView;
}
}
Title Index
${mess}
server:
port: 8181
spring:
mvc:
view:
prefix: /
suffix: .jsp
package com.southwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
看到如下界⾯,说明访问成功