1. Spring Boot 核心配置文件 16
Spring Boot 的核心配置文件用于配置 Spring Boot 程序,名字必须以 application 开始
扩展名有: properties( k=v) ; yml ( k: v)
使用application.properties, application.yml
重点默认使用的式properties文件
2. properties 文件(默认采用该文件) 17
例1:application.properties设置 端口和上下文
通过修改 application.properties 配置文件,在修改默认 tomcat 端口号及项目上下文件根
键值对的 properties 属性文件配置方式
application.properties
#设置端口号
server.port=8082
#设置访问应用上下文路径, contextpath
server.servlet.context-path=/myboot
BootController
package com.bjpowernode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
// properties 文件(默认采用该文件) 16
@Controller
public class BootController {
@RequestMapping("/hello")
@ResponseBody//将doSome方法的返回值作为数据
public String doSome(){
return "hello SpringBoot应用 ,设置了端口8082上下文路径 /myboot ";
}
}
MyApplication
package com.bjpowernode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class,args);
}
}
访问路径变成了http://localhost:8082/myboot/hello
3. yml配置文件 18
yml 是一种 yaml 格式的配置文件,主要采用一定的空格、换行等格式排版进行配置。
yaml 是一种直观的能够被计算机识别的的数据序列化格式,容易被人类阅读,yaml 类
似于 xml,但是语法比 xml 简洁很多,值与前面的冒号配置项必须要有一个空格, yml
缀也可以使用 yaml 后缀
注意 : 当两种格式配置文件同时存在 ,在 SpringBoot2.4 开始, 使用的是 yml 配置文件.
修改配置名称都为 application。
推荐使用 yml 格式配置文件
server:
port: 8083
servlet:
context-path: /myboot2
BootController
package com.bjpowernode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
// properties 文件(默认采用该文件) 16
@Controller
public class BootController {
@RequestMapping("/hello")
@ResponseBody//将doSome方法的返回值作为数据
public String doSome(){
return "hello SpringBoot应用 ,设置了端口8082上下文路径 /myboot ";
}
}
MyApplication
package com.bjpowernode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class,args);
}
}
重新运行 Application ,查看启动的端口及上下文根
访问路径http://localhost:8083/myboot2/hello
4. 多环境配置 19
代码写在course3_1中
在实际开发的过程中,我们的项目会经历很多的阶段(开发->测试->上线),每个阶段的配置也会不同,例如:端口、上下文根、数据库等,那么这个时候为了方便在不同的环境之间切换,SpringBoot 提供了多环境配置
为每个环境创建一个配置文件,命名必须以 application-环境名称.properties|yml
例如
创建开发环境的配置文件: application-dev.properties( application-dev.yml )
创建测试者使用的配置: application-test.properties
4.1 具体如下 19-21
application-dev.yml
#开发环境的配置文件 19
server:
port: 8081
servlet:
context-path: /mydev
application-online.yml
#项目上线使用的配置文件 19
server:
port: 9002
servlet:
context-path: /myonline
application-test.yml
#测试使用的配置文件 19
server:
port: 9001
servlet:
context-path: /mytest
在application.yml文件中调用指定用那个环境
application.yml
#激活使用的dev环境 20
#spring:
# profiles:
# active: dev
#激活使用的test环境 20
spring:
profiles:
active: test
BootController
package com.bjpowernode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//多环境配置 19
@Controller
public class BootController {
@RequestMapping("/hello")
@ResponseBody
public String doSome(){
return "Springboot多环境配置";
}
}
Application
package com.bjpowernode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//多环境配置 19
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
5. Spring Boot 自定义配置 22
SpringBoot 的核心配置文件中,除了使用内置的配置项之外,我们还可以在自定义配
置,然后采用如下注解去读取配置的属性值
6. @Value 注解 22
6.1 读取配置文件数据 22
application.properties
#配置端口号 22
server.port=8082
#context-path
server.servlet.context-path=/myboot
#自定义key-value
school.name=动力节点
http://school.website=www.bjpowernode.com
school.address=北京大兴区
http://site=www.bjpowernode.com
HelloController
package com.bjpowernode.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
//读取配置文件中的数据 22
@Controller
public class HelloController {
@Value("${server.port}")
private Integer port;
@Value("${server.servlet.context-path}")
private String contextPath;
@Value("${school.name}")
private String name;
@Value("${site}")
private String site;
@RequestMapping("/data")
@ResponseBody
public String queryData(){
return name+",site="+site+", 项目的访问地址="+contextPath+",使用的端口="+port;
}
}
Application
package com.bjpowernode;
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);
}
}
启动应用 Application , 访问浏览器
7. @ConfigurationProperties 23
将整个文件映射成一个对象,用于自定义配置项比较多的情况
7.1 案例演示
在 com.bjpowernode.vo 包下创建 SchoolInfo 类,并为该 类加上Component 和 ConfigurationProperties 注解,prefix 可以不指定,如果不指定,那么会去配置文件中寻找与该类的属性名一致的配置,prefix 的作用可以区分同名配置
application.properties
#自定义key-value
school.name=动力节点
http://school.website=www.bjpowernode.com
school.address=北京大兴区
package com.bjpowernode.controller;
import com.bjpowernode.vo.SchoolInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
//读取配置文件中的数据 22
@Controller
public class HelloController {
@Resource//自动注入 23
private SchoolInfo info;
@RequestMapping("/info")
@ResponseBody
public String queryInfo(){
return "SchoolInfo对象=="+info.toString();
}
}
7.2 解决警告 23
在 SchoolInfo 类中使用了 ConfigurationProperties 注解,IDEA 会出现一个警告,不影响程序的执行
点击 open documentnation 跳转到网页,在网页中提示需要加一个依赖,我们将这个依赖拷贝,粘贴到 pom.xml 文件中
pom.xml文件中接入以下依赖
<!-- 处理 @ConfigurationProperties注解 元数据 23-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
3.3 中文乱码
如果在 SpringBoot 核心配置文件中有中文信息,会出现乱码:
◼ 一般在配置文件中,不建议出现中文(注释除外)
◼ 如果有,可以先转化为 ASCII 码
4. springboot中使用jsp 24
SpringBoot不推荐使用jsp ,而是使用模板技术代替jsp
4.1 使用jsp需要配置: 25
代码写在course4_1
加入一个处理jsp的依赖。 负责编译jsp文件
<!--处理jsp依赖-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
如果需要使用servlet, jsp,jstl的功能
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
创建一个存放jsp的目录,一般叫做webapp
如果在webapp目录下右键,没有创建jsp的选项,可以在Project Structure中指定webapp
为 Web Resource Directory
index.jsp
<%--在springboot中使用jsp 25--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>jsp文件</title>
</head>
<body>
<h3>使用jsp,显示Controller中的数据 ${data}</h3>
</body>
</html>
需要在pom.xml指定jsp文件编译后的存放目录。
<!--指定jsp编译后的存放目录 25-->
<resources>
<resource>
<!--jsp原来的目录-->
<directory>src/main/webapp</directory>
<!--指定编译后的存放目录-->
<targetPath>META-INF/resources</targetPath>
<!--指定处理的目录和文件-->
<includes>
<include>**/*.*</include>
</includes>
</resource>
META-INF/resources
创建Controller, 访问jsp
package com.bjpowernode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
//在springboot中使用jsp 25
@Controller
public class JspController {
/*public String doJsp(HttpServletRequest request){
request.setAttribute("data","SpringBoot使用Jsp");
//视图的逻辑名称
return "index";
}*/
//上面的doJsp还可以写成以下 25
/**
* ModelAndView:
* @param model
* @return
*/
@RequestMapping("/myjsp")
public String doJsp(Model model){
//把数据放入到request作用域
//以下两种写法等价
model.addAttribute("data","SpringBoot使用Jsp");
//request.setAttribute("data","SpringBoot使用Jsp");
//视图的逻辑名称
return "index";
}
}
在application.propertis文件中配置视图解析器
#配置端口号 25
server.port=9090
server.servlet.context-path=/myboot
#配置视图解析器 25
# prefix=/ 中的 / = src/main/webapp
http://spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
测试