1、Environment
a>添加配置文件内容
test.msg=read config
b>创建控制器类
package com.yzh.boot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EnvReaderConfigController {
@Autowired
private Environment env;
@RequestMapping("/testEnv")
public String testEnv() {
return "方法一:使用Environment类读取配置文件中的数据" + env.getProperty("test.msg") ;
}
}
2、@Value
a>创建控制器类
package com.yzh.boot.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ValueReaderConfigController {
@Value("${test.msg}")
private String msg;
@RequestMapping("/testValue")
public String testValue() {
return "方法二:使用@Value注解读取配置文件中的数据" + msg;
}
}
3、@ConfigurationProperties
a>添加配置文件内容
obj.sname=叶子航
obj.sage=20
b>建立配置文件与对象的映射关系
package com.yzh.boot.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component//使用Component注解,声明一个组件,被控制器依赖注入
@ConfigurationProperties(prefix = "obj")//obj为配置文件中key的前缀
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class StudentProperties {
private String sname;
private int sage;
}
c>创建控制器类
package com.yzh.boot.controller;
import com.yzh.boot.model.StudentProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigurationPropertiesController {
@Autowired
StudentProperties studentProperties;
@RequestMapping("/testConfigurationProperties")
public String testConfigurationProperties() {
return studentProperties.toString();
}
}
4、@PropertySource
a>创建配置文件
ok.properties
your.msg=hello.
test.properties
my.msg=test PropertySource
b>创建控制器类
package com.yzh.boot.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@PropertySource({"test.properties","ok.properties"})
public class PropertySourceValueReaderOhterController {
@Value("${my.msg}")
private String mymsg;
@Value("${your.msg}")
private String yourmsg;
@RequestMapping("/testProperty")
public String testProperty() {
return "其他配置文件test.properties:" + mymsg + "<br>" + "其他配置文件ok.properties:" + yourmsg;
}
}
5、日志配置
package com.yzh.boot.controller;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class LogTestController {
private Log log = LogFactory.getLog(LogTestController.class);
@RequestMapping("/testLog")
public String testLog() {
log.info("测试日志");
return "测试日志" ;
}
}
#输出日志到文件,默认输出到项目的工程目录下
logging.file.name=my.log
6、处理浏览器乱码
#处理浏览器乱码(按顺序配置)
server.tomcat.uri-encoding=UTF-8
server.servlet.encoding.force=true
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true