当前位置:网站首页>Six, read application configuration + log configuration
Six, read application configuration + log configuration
2022-07-30 04:40:00 【time postman】
文章目录
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 "方法一:使用EnvironmentThe class reads the data in the configuration file" + 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 "方法二:使用@ValueAnnotations read data from configuration files" + 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注解,声明一个组件,Dependency injected by the controller
@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 "测试日志" ;
}
}
#输出日志到文件,The default output is to the project directory of the project
logging.file.name=my.log

6、Handle browser garbled characters
#Handle browser garbled characters(Configure in order)
server.tomcat.uri-encoding=UTF-8
server.servlet.encoding.force=true
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true
边栏推荐
猜你喜欢

GCC Rust is approved to be included in the mainline code base, or will meet you in GCC 13
![handler+message [message mechanism]](/img/4b/981d5eb2f1afc98a4ceab38c505325.png)
handler+message [message mechanism]

【线性表】- LeetCode力扣三道练习题详解

labelme的使用技巧

MySQL 操作语句大全(详细)

Introduction to database - MySQL simple introduction

山西省第二届网络安全技能大赛(企业组)部分赛题WP(七)

【周周有奖】云原生编程挑战赛“边缘容器”赛道邀你来战!

动态规划问题(完结篇)

DAY17:弱口令的探测与测试
随机推荐
cnpm installation steps
How to use labelme
2.5快速排序
Naive Bayes Classification
模拟问题(中)
Shanxi group (enterprises) in the second network security skills competition part problem WP (7)
小程序npm包--API Promise化
SVN 查看用户名密码
swagger使用教程——快速使用swagger
《构建之法》笔记---第十章 典型用户和场景
See you in shenzhen!Cloud native to accelerate the application building special: see cloud native FinOps, SRE, high-performance computing scenario best practices
C. Travelling Salesman and Special Numbers (binary + combination number)
VUX Datetime 组件compute-days-function动态设置日期列表
file system two
2.6 Merge Sort
软件测试员必看!数据库知识mysql查询语句大全
七、自定义配置
DAY17:弱口令的探测与测试
[C language] Program environment and preprocessing
山西省第二届网络安全技能大赛(企业组)部分赛题WP(十)