Resource目录结构说明:
static:保存所有的静态资源; js css images;
templates:保存所有的模板页面;(Spring Boot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面);可以使用模板引擎(freemarker、thymeleaf、velocity);
application.properties(application.yml):Spring Boot应用的配置文件;可以修改一些默认设置;例如可以修改启动端口
server.port=8089
properties文件一般都是一个key=value,下面我们着重介绍下yml的配置方式:
如果我们在编写yml文件有提示首先需要导入
org.springframework.boot
spring-boot-configuration-processor
true
编写两个配置bean:
【AdminInfo】
package com.swk.springboot.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
/**
* 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
*/
@Component
/**
* 将配置文件中配置的每一个属性的值,映射到这个组件中
* @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
* prefix = "admininfo":配置文件中哪个下面的所有属性进行一一映射
*/
@ConfigurationProperties(prefix = "admininfo")
public class AdminInfo {
private String loginName;
private String password;
private boolean isSuper;
private UserInfo userInfo;
private Map map;
private List list;
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isSuper() {
return isSuper;
}
public void setSuper(boolean aSuper) {
isSuper = aSuper;
}
public UserInfo getUserInfo() {
return userInfo;
}
public void setUserInfo(UserInfo userInfo) {
this.userInfo = userInfo;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
@Override
public String toString() {
return "AdminInfo{" +
"loginName='" + loginName + '\'' +
", password='" + password + '\'' +
", isSuper=" + isSuper +
", map=" + map +
", list=" + list +
'}';
}
}
【UserInfo】
package com.swk.springboot.config;
import java.util.Date;
public class UserInfo {
private String userName;
private int age;
private Date loginTime;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getLoginTime() {
return loginTime;
}
public void setLoginTime(Date loginTime) {
this.loginTime = loginTime;
}
@Override
public String toString() {
return "UserInfo{" +
"userName='" + userName + '\'' +
", age=" + age +
", loginTime=" + loginTime +
'}';
}
}
编写yml配置文件:
server:
port: 8081
# 配置用户信息
admininfo:
loginName: sunwukong
password: 123456
isSuper: true
userInfo: {userName: test,age: 18,loginTime: 2019/01/06 21:00:00}
map:
ip: 192.168.1.1
port: 3306
list:
- userName: user1
age: 20
loginTime: 2019/01/06 21:00:01
- userName: user1
age: 20
loginTime: 2019/01/06 21:00:01
测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootSwkApplicationTests {
@Autowired
AdminInfo admininfo;
@Test
public void contextLoads() {
System.out.println(admininfo.toString());
}
}
运行结果如下:
2019-01-06 21:30:04.426 INFO 8008 --- [ main] c.s.s.s.SpringbootSwkApplicationTests : Started SpringbootSwkApplicationTests in 1.795
seconds (JVM running for 2.63)
AdminInfo{loginName='sunwukong', password='123456', isSuper=false, map={ip=192.168.1.1,
port=3306}, list=[UserInfo{userName='user1', age=20, loginTime=Sun Jan 06 21:00:01 CST
2019}, UserInfo{userName='user1', age=20, loginTime=Sun Jan 06 21:00:01 CST 2019}]}
yml语法介绍:
【基本语法】
k:(空格)v:表示一对键值对,k和v之间必须有空格
以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的
info:
level1: test
level1: test
level2: test
level3: test
属性和值大小写敏感
"":双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思
name: "zhangsan \n lisi":输出;zhangsan 换行 lisi
'':单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据
name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi
【对象、Map(属性和值)(键值对)】
user:
name: swk
age: 500
## 或者
user: {name: swk,age: 500}
【数组(List、Set)】
list:
‐ you
‐ me
‐ him
#List
list:
- name: swk
age: 500
- name: zbj
age: 360