1. Spring Boot配置原理
Spring Boot框架是一个将整合框架的整合代码都写好了的框架。所以我们要知道它的工作原理才能够,找到各种整合框架可以配置的属性,以及属性对应的属性名。
配置原理说明
SpringBoot的spring-boot-autoconfigure-1.5.6.RELEASE.jar中编写了所有内置支持的框架的自动整合代码。
根据功能划分(分注解配置)
所有支持的框架根据功能类型来划分包,每个包都有一个XxxAutoConfiguration配置类,都是一个基于纯注解的配置类,是各种框架整合的代码
如果配置的框架有默认的配置参数,都放在一个命名为XxxProperties的属性类,如图所示:
通过项目的resources下的application.properties文件可以修改每个整合框架的默认属性,从而实现了快速整合的目的。
在spring-boot-autoconfigure-1.5.6.RELEASE.jar的web包的ResourceProperties类中作了默认的配置:
配置流程说明
第一步
第二步
第三步
Spring Boot配置文件
默认情况下,Spring Boot会加载resources目录下的application.properties或application.yml来获得配置的参数。
(SpringBoot支持一种由SpringBoot框架自制的配置文件格式。后缀为yml。yml后缀的配置文件的功能和properties后缀的配置文件的功能是一致的。)
Spring Boot能支持两种配置文件:
——application.propertie:键值对风格配置文件
application.properties(一)
单配置文件:
# 配置数据源
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_db
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.mchange.v2.c3p0.ComboPooledDataSource
多配置文件:(2步)
第一步
application-database.properties
application-jpa.properties
application-freemarker.properties
第二步
spring.profiles.active=database,jpa,freemarker
application.yml(二)
单配置文件:
# 配置数据源
spring:
datasource:
url: jdbc:mysql://localhost:3306/springboot_db
driverClassName: com.mysql.jdbc.Driver
username: root
password: root
type: com.mchange.v2.c3p0.ComboPooledDataSource
其实application.yml配置文件就是将原来application.properties使用(.)分割的方式,改为树状结构,使用(:)分割。
注意:最后key的字段与值之间的冒号(:)后面一定要有一个空格。
多配置文件(2步)
第一步:
application-database.yml
application-jpa.yml
application-freemarker.yml
第二步
在application.yml总配置文件指定,加载的多个配置文件需要在application.yml中指定其它配置文件:
spring:
profiles:
active: database,jpa,freemarker
Spring Boot视图
静态资源html视图
SpringBoot默认有四个静态资源文件夹:
l classpath:/static/
l classpath:/public/
l classpath:/resources/
l classpath:/META-INF/resources/(这里存放的静态资源是不可以直接访问的,要通过内部跳转才可以访问)
在spring-boot-autoconfigure-1.5.6.RELEASE.jar的web包的
ResourceProperties类中作了默认的配置:
测试:
第一步
<!DOCTYPE html>
<html>
<head>
<title>SpringBoot</title>
<meta charset="UTF-8"/>
<meta http-equiv="pragma" content="no-cache"/>
<link rel="shortcut icon" type="image/x-icon" href="logo.ico"/>
</head>
<body>
user
</body>
</html>
第二步
提供HelloController处理器@Controller
public class HelloController {
// 响应数据为静态html页面: http://localhost:8080/user
@GetMapping("/user")
public String user(){
return "/html/user.html";
}
}
JSP视图(不推荐)
第一步:创建Maven项目(war包)
第二步:配置依赖
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<groupId>cn.itcast</groupId>
<artifactId>springboot02-jsp-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- 定义全局属性 -->
<properties>
<!-- 更改JDK版本 -->
<java.version>1.7</java.version>
</properties>
<!-- 配置依赖关系 -->
<dependencies>
<!-- 配置web启动器(spring mvc) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 配置devtools实现热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 配置servlet-api、jsp-api、el-api依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- 配置jstl依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
</project>
第三步:提供application.properties属性文件
# 开启jsp视图
# 设置视图前缀
spring.mvc.view.prefix=/WEB-INF/jsp/
# 设置视图后缀
spring.mvc.view.suffix=.jsp
第四步:开发处理器ItemController
@Controller
public class ItemController {
/** 查询得到数据 */
@GetMapping("/item")
public String item(Model model){
/** 添加响应数据 */
model.addAttribute("itemArr", new String[]{"iphone7手机","华为手机","小米手机"});
/** 返回视图 */
return "item";
}
}
第五步:提供src/main/webapp/WEB-INF/jsp/item.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>SpringBoot</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<ul>
<c:forEach items="${itemArr}" var="item">
<li>${item }</li>
</c:forEach>
</ul>
</body>
</html>
第六步:开发Application作为SpringBoot引启类
@SpringBootApplication // 代表为SpringBoot应用的运行主类
public class Application {
public static void main(String[] args) {
/** 创建SpringApplication应用对象 */
SpringApplication springApplication = new SpringApplication(Application.class);
/** 设置横幅模式(设置关闭) */
springApplication.setBannerMode(Banner.Mode.OFF);
/** 运行 */
springApplication.run(args);
}
}