当前位置:网站首页>六、读取应用配置+日志配置
六、读取应用配置+日志配置
2022-07-30 04:33:00 【时间邮递员】
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
边栏推荐
猜你喜欢
[Redis Master Cultivation Road] Jedis - the basic use of Jedis
How does the Snapdragon 7 series chip perform?Reno8 Pro proves a new generation of God U
The first immersive and high-fidelity metaverse in China, Xiyuan Universe is officially launched
Thinkphp 5.0.24变量覆盖漏洞导致RCE分析
Is the end of the universe a bank?Talk about those things about doing software testing in the bank
What is CDH/CDP?
2.6归并排序
Golang eight-legged text finishing (continuous handling)
海外多家权威媒体热议波场TRON:为互联网去中心化奠定基础
DAY17:弱口令的探测与测试
随机推荐
Data Lake: Data Integration Tool DataX
Code open source design and implementation ideas
山西省第二届网络安全技能大赛(企业组)部分赛题WP(九)
JQ源码分析(环境处理)
Go study notes (84) - Go project directory structure
MySQL 字符串拼接 - 多种字符串拼接实战案例
Shell脚本基本编辑规范及变量
Image stitching (registration) case based on OpenCV
05 Detailed explanation of the global configuration file application.properties
Shanxi group (enterprises) in the second network security skills competition part problem WP (8)
2.6 Merge Sort
Install MySQL Database on Kylin V10 Operating System
The difference between forward and redirect
【翻译】Envoy Fundamentals,这是一个培训课程,使人们能够更快地采用Envoy Proxy。...
[C language] Program environment and preprocessing
DAY17, CSRF vulnerability
机器学习:知道通过低方差过滤实现降维过程
Charles 替换 接口响应信息
"Translation" Envoy Fundamentals, this is a training course, make people to more quickly using Envoy Proxy..
解决报错SyntaxError: (unicode error) ‘utf-8‘ codec can‘t decode byte 0xb7 in position 0: invalid start b