六、读取应用配置+日志配置

本文介绍了在Spring Boot中如何通过Environment、@Value、@ConfigurationProperties和@PropertySource注解来读取配置文件内容。同时,展示了日志配置及处理浏览器乱码的方法,确保应用正常运行。
摘要由CSDN通过智能技术生成

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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时间邮递员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值