方法一:@Value获取属性值

首先在application.properties中添加属性值

app.name=MyApp
app.description=${app.name} is a Spring Boot application

编写工具类,通过注解@Value获取属性值(类名这里就不讲究命名规范了)

package com.example.spdemo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Mytest {

    @Value("${app.name}")
    private String appName;

    @Value("${app.description}")
    private String appDescription;

    public String getAppName() {
        return appName;
    }

    public String getAppDescription() {
        return appDescription;
    }

}

调用工具类,输出属性值

package com.example.spdemo.controller;

import com.example.spdemo.Application;
import com.example.spdemo.Mytest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Index {
    @Autowired
    private Mytest mytest;

    @RequestMapping("/")
    public String HelloWorld(){

        System.out.println();
        System.out.println(mytest.getAppName());
        System.out.println(mytest.getAppDescription());
        return "Hello World !";

    }
}

方法二:@ConfigurationProperties

package com.example.spdemo.controller;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@ConfigurationProperties(prefix = "app")
public class Index {

    private String name;
    private String description;

    @RequestMapping("/")
    public String getProps() {
        return name + "|" + description;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

如果配置没有app的前缀的话,去掉注解中的(prefix = "app"),保持属性名与类成员属性名一致即可。

Spring获取application.properties的更多相关文章

  1. 【转】spring boot application.properties 配置参数详情

    multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold: 大于该值的文件会被写到磁盘上 multipart. ...

  2. Inspection info: Checks Spring Boot application .properties configuration files. Highlights unresolved and deprecated configuration keys and in

    Cannot resolve class or package ‘jdbc’ less… (Ctrl+F1) Inspection info: Checks Spring Boot applicati ...

  3. Spring boot application.properties 配置

    原文链接: http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.ht ...

  4. spring boot application.properties 属性详解

    2019年3月21日17:09:59 英文原版: https://docs.spring.io/spring-boot/docs/current/reference/html/common-appli ...

  5. Spring 的application.properties项目配置与注解

    一.项目结构介绍 如上图所示,Spring Boot的基础结构共三个文件: src/main/java  程序开发以及主程序入口 src/main/resources 配置文件 src/test/ja ...

  6. spring boot application.properties 配置参数详情

    multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold: 大于该值的文件会被写到磁盘上 multipart. ...

  7. Spring boot application.properties和 application.yml 初学者的学习

    来自于java尚硅谷教程 简单的说这两个配置文件更改配置都可以更改默认设置的值比如服务器端口号之类的,只需再文件中设置即可, properties可能是出现的比较早了,如果你不调你的默认编码,中文可能 ...

  8. spring boot application.properties详解

    附上最新文档地址:https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-propertie ...

  9. spring boot application.properties乱码问题

    1. 在application.properties 中增加 spring.http.encoding.force=true spring.http.encoding.charset=UTF- spr ...

随机推荐

  1. Jenkins入门-部署gitlab 项目(8)

    目前很多公司代码管理已经迁入到了git上,大多数的公司使用的代码管理服务器是gitlab,目前持续交付的流行很多公司都采用Jenkins完成持续交付工作.首先我们需要通过Jenkins来获取我们的项目 ...

  2. linux 安装多个版本JDK,指定tomcat的jdk版本

    JDK的下载可以直接到官网下载,这里不再介绍 一.安装JDK 7 vi /etc/profile #set java environmentexport JAVA_HOME=/usr/java/jdk ...

  3. C++Builder XE7 up1 简单测试

    很久没用BCB了, 新装了BCBXE7up1试试了,发现有点找不到北了,好像与BCB6的一些默认设置项不一样,编译了一个空APP,提示找不到bpl 和 dll. 设置为不带包编译后,还是提示DLL找不 ...

  4. SynergyS7G2RTC时钟模块的使用

    RTC功能描述 RTC时钟模块是Synergy芯片的一个时间外设,主要用于日期时间的存储和控制,有别于一般MCU中的Timer,RTC时钟有两种计时模式,日期模式和二进制计时模式,其中日期模式的时间可 ...

  5. Azure Storage (26) HTML5播放Azure Storage MP4问题

    <Windows Azure Platform 系列文章目录> 问题:客户描述说在Azure Storage上的MP4视频无法在线播放,只能看到黑屏的视频,有声音但是没有图像 问题排查:H ...

  6. Python开发工具Atom

    python基础教程之Python开发工具Atom   本节内容如下: Atom简介 下载安装Atom 安装Python开发包 使用Atom开发Python程序 Atom简介 Atom是Github开 ...

  7. PREV-6_蓝桥杯_翻硬币

    问题描述 小明正在玩一个“翻硬币”的游戏. 桌上放着排成一排的若干硬币.我们用 * 表示正面,用 o 表示反面(是小写字母,不是零). 比如,可能情形是:**oo***oooo 如果同时翻转左边的两个 ...

  8. 若是汉字的一半,就舍弃这个汉字输出,例如:“js3范ad啊asd”,截取4,则输出:“js3”

    package com.jt.test.redis; import org.junit.Test; /* 题目要求 * 编码:GBK,一个英文字符占一个字节,一个汉字占2个字节 * 随机给定一个字符串 ...

  9. JavaScript for...in 循环

    JavaScript for...in 语句循环遍历对象的属性. 语法 for (对象中的变量) { 要执行的代码 } 注释:for...in 循环中的代码块将针对每个属性执行一次. 实例 循环遍历对 ...

  10. 廖雪峰Java2-2数据封装-2构造方法

    在2-2-1中,创建1个实例需要3步 Person ming = new Person(); ming.setName(" 小明 "); ming.setAge(16); 问题:能 ...