(15)spring boot使用@Value,@PropertySource注解使用
来源:互联网 发布:淘宝返现规则 编辑:程序博客网 时间:2024/06/08 18:33
******@Value******
在spring boot中,有些变量根据需求配置在application.properties中,
在应用程序中使用@Value注解获取值。
eg:
在配置application.properteis配置一个键值对:
TestValue=This is my test!
程序中获取方式:
/** 使用@value注解,从配置文件读取值 */@Value("${TestValue}")private String testValueAnno;
将变量testValueAnno值初始化为This is my test!
******@PropertySource******
@PropertySource注解用于指定目录,指定编码读取properties文件,
如果将TestValue在配置文件中对应的值加上中文,通过@Value读取
到的值会出现中文乱码,因为spring boot加载application.properties
采用的是unicode编码形式,后台读取的变量值自然是乱码,解决办法
就是通过@PropertySource注解指定文件路径,通过utf-8的编码读取文件。
eg:
package com.lanhuigu.hello;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.ResponseBody;import org.springframework.web.bind.annotation.RestController;/** * @RestController这个注解等价于spring mvc用法中的@Controller+@ResponseBody */@RestController@PropertySource(value = {"classpath:application.properties"},encoding="utf-8")@RequestMapping(value="hello")public class HelloController {/** 使用@value注解,从配置文件读取值 */@Value("${TestValue}")private String testValueAnno;@RequestMapping(value="sayHello")@ResponseBodyprivate String sayHello() {System.out.println("测试:"+testValueAnno+"一意孤行!");return "hello world!";}}通过以上方式,能够解决spring boot通过@Value读取变量值出现中文乱码问题。
阅读全文
1 0
- spring boot使用@Value,@PropertySource注解使用
- (15)spring boot使用@Value,@PropertySource注解使用
- spring boot中使用@Value注解
- Spring的@PropertySource和@Value注解例子
- Spring 4 @PropertySource和@Value注解示例
- Spring的@PropertySource和@Value注解例子
- Spring 4 @PropertySource和@Value注解示例
- Spring的@PropertySource和@Value注解例子
- Springboot中PropertySource注解的使用
- Spring @Value注解的使用
- Spring 从文件系统读取配置文件, 使用@PropertySource
- Spring 使用@PropertySource读取properties文件
- Spring 使用@PropertySource+Emvironment读取properties文件
- spring注解 @PropertySource 配置数据源
- Spring中Value注解的使用
- Spring中Value注解的使用
- 使用spring 的注解 @value使用方法
- Spring @Value注解使用${}进行注入
- Moving Averages(1): Introduction
- Java基础——注解(Annotation)
- Hibernate逆向代码问题
- c++ 用socket实现TCPIP通信
- MSSQL起手式
- (15)spring boot使用@Value,@PropertySource注解使用
- XML解析
- Magento 2 日志的更多细节及数据库日志活动
- build zxing 3.3.1
- 文件属性相关(一)
- 计蒜客 2017 第四场 商汤科技的行人检测(中等)(几何)
- NoSQL简介
- glog使用说明
- java关键字