1.在application.properties中添加以下内容:

learn.blog.name=hello
learn.blog.title=千回教育系统

2.新增属性关联的类:

package com.czhappy.learn.BootLearn.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component
public class BlogProperties { @Value("${learn.blog.name}")
private String name; @Value("${learn.blog.title}")
private String title; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
}
}

3.编写controller:

package com.czhappy.learn.BootLearn.controller;

import com.czhappy.learn.BootLearn.config.BlogProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class IndexController {
@Autowired
private BlogProperties blogProperties; @RequestMapping("/")
String index() {
System.out.println(blogProperties.getTitle());
return blogProperties.getName()+"——"+blogProperties.getTitle();
}
}

4.启动应用,在浏览器中调用:

注意事项:

如果浏览器中显示中文乱码,需要修改idea的配置:

然后复制application.properties文件中的内容并保存到一个临时文本中,把application.properties文件删除,在原目录重新新建一个application.properties,把临时文本中的内容copy到新建的application.properties文件中,启动项目,即可正确获取中文内容。

SpringBoot读取properties中的属性值的更多相关文章

  1. AJPFX关于读取properties 配置文件 返回属性值

    :Properties的概述        * Properties 类表示了一个持久的属性集.        * Properties 可保存在流中或从流中加载.        * 属性列表中每个键 ...

  2. SpringBoot @Value读取properties文件的属性

    SpringBoot在application.properties文件中,可以自定义属性. 在properties文件中如下示: #自定义属性 mail.fromMail.addr=lgr@163.c ...

  3. Spring中使用@Value读取porperties文件中的属性值方法总结及注意事项

    本文为博主原创,转载请注明出处. 此前曾总结过使用工具类读取properties文件中的属性值,有兴趣的可以看一下. 如何快速获取properties中的配置属性值:https://www.cnblo ...

  4. springboot读取配置文件中的信息

    在一个项目中,我们有时候会把一些配置信息写入到一个配置文件中,在java代码中读取配置文件的信息.在此记录下读取属性文件中的内容. 在springboot项目中,springboot的配置文件可以使用 ...

  5. 通过getResourceAsStream 获得Properties文件属性和属性值

    1.Class.getResourceAsStream(String path) path:不以‘/’开头默认是从此类所在的包下取资源:以'/'开头则是从ClassPath根目录下获取 2.Class ...

  6. gradle-wrapper.properties中各属性的含义

    gradle-wrapper.properties中各属性的含义 1. gradle-wrapper.properties 每一个用gradle编译的工程,都会有一个gradle\wrapper目录. ...

  7. <s:property="a" value=""/>取的<s:debug></s:debug>中的value stack中的属性值

    <s:property="a"  value=""/>取的<s:debug></s:debug>中的value stack中 ...

  8. 将source类中的属性值赋给target类中对应的属性

    /** * 对象的属性值拷贝 * <p> * 将source对象中的属性值赋值到target对象中的属性,属性名一样,类型一样 * <p> * example: * <p ...

  9. 【Python】获取翻页之后的各页面中的属性值。

    如何获取翻页之后的页面中的html标签中的属性值? # coding=utf-8 from selenium import webdriver if __name__=="__main__& ...

随机推荐

  1. 格式化字符串——初级% 和format

    print '{a},{b}'.format(a='hello',b='word') st='a %s %s x y z' st1=('b','c') print st%st1 print '%s % ...

  2. 分析和研究Monkey Log文件

    Log 在Android中的地位非常重要,要是作为一个android程序员不能过分析log这关,算是android没有入门吧 . 下面我们就来说说如何处理log文件 . 什么时候会有Log文件的产生 ...

  3. python开发笔记-变长字典Series的使用

    Series的基本特征: 1.类似一维数组的对象 2.由数据和索引组成 import pandas as pd >>> aSer=pd.Series([1,2.0,'a']) > ...

  4. Zabbix4.0国内下载源

    国内zabbix源总结 目前发现的有以下几个站点: 1.阿里巴巴开源镜像站(推荐使用) 地址:https://mirrors.aliyun.com/zabbix/ 2.华为开源镜像站(推荐使用) 地址 ...

  5. nginx的alias与root的区别

    root的写法: location /request_path/image/ { root /local_path/image/; } 这样配置的结果就是当客户端请求 /request_path/im ...

  6. SQL操作Spark SQL--BasicSQLTestt

    object BasicSQLTest { def main(args: Array[String]): Unit = { val spark = SparkSession .builder() .a ...

  7. 对象key值排序,以key值(数字)大小顺序遍历属性,helper._sort()

    var helper = { _sort:function(data){ //{“20141216”:{},“20141217”:{}}按大小排序, var arr1 = [],arr2=[]; fo ...

  8. 使用mybatis框架实现带条件查询-多条件(传入Map集合)

    我们发现我们可以通过传入javaBean的方式实现我们的需求,但是就两个条件,思考:现在就给他传入一个实体类,对系统性能的开销是不是有点大了. 现在改用传入Map集合的方式: 奥!对了,在创建map集 ...

  9. web自动化测试-selenium的三种等待

    一.等待的作用 1.在系统的功能运行过程中,所有的内容是需要一定的时间来实现展示, 2.时间耗费长短与网络速度.系统框架设定.接口的执行复杂度有关, 3.因此需要设置缓冲时间,若未设置缓冲时间,容易导 ...

  10. Tips on Python

    python是一种解释性文件,代码要通过解释器解释运行.python解释器就是python.exe这个程序. pip也是一个pip.exe的程序,是用来管理python的第三方库. 有两种执行方式:脚 ...