当前位置:网站首页>六、读取应用配置+日志配置
六、读取应用配置+日志配置
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
边栏推荐
- Thinkphp 5.0.24 Variable Override Vulnerability Causes RCE Analysis
- Thinkphp 5.0.24变量覆盖漏洞导致RCE分析
- 1. Get data - requests.get()
- sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
- 验证addShutdownHook钩子生效
- 2.4 hill sorting
- The implementation and basic operation of sub-database sub-table, ER table, global table, fragmentation rules, global sequence, etc. in MyCat
- 山西省第二届网络安全技能大赛(企业组)部分赛题WP(八)
- Many overseas authoritative media hotly discuss TRON: laying the foundation for the decentralization of the Internet
- Advanced [C] array to participate in the function pointer
猜你喜欢
PyG搭建R-GCN实现节点分类
Azure 开发者新闻快讯丨开发者7月大事记一览
Go 学习笔记(84)— Go 项目目录结构
The VUX Datetime component compute-days-function dynamically sets the date list
The first immersive and high-fidelity metaverse in China, Xiyuan Universe is officially launched
数据库概论 - MySQL的简单介绍
Charles 替换 接口响应信息
2.6归并排序
MySQL installation error solution
@ WebServlet annotations (Servlet annotations)
随机推荐
2.6 Merge Sort
Chapter8 支持向量机
SQLSERVER merges subquery data into one field
精品MySQL面试题,备战八月99%必问!过不了面试算我的
state space representation
cnpm installation steps
Introduction to database - MySQL simple introduction
2.6归并排序
权值线段树+线段树分裂/合并+CF1659D
海外多家权威媒体热议波场TRON:为互联网去中心化奠定基础
2.6基数排序(桶排序)
swagger usage tutorial - quick use of swagger
How to use labelme
山西省第二届网络安全技能大赛(企业组)部分赛题WP(九)
MySQL 安装报错的解决方法
Thymeleaf简介
handler+message【消息机制】
Shell script basic editing specifications and variables
DAY17: weak password detection and test
Advanced [C] array to participate in the function pointer