方法一:@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. Netty 学习资料

    Netty 学习资料 Netty 学习资料 链接网址 说明 Netty 4.x 用户指南 http://wiki.jikexueyuan.com/project/netty-4-user-guide/ ...

  2. Java集合整理

    0,基础概念 Collection:统计大小.插入或删除数据.清空.是否包含某条数据,等等.而Collection就是对这些常用操作进行提取,只是其很全面.很通用.size(),isEmpty(),c ...

  3. MySQL程序之mysql参数详解

    MySQL程序之mysql参数详解 mysql 是一个命令行客户程序,用于交互式或以批处理模式执行SQL语句 用法: mysql [OPTIONS] [database] 参数: 1.-? --hel ...

  4. CentOS6.4 添加php-fpm系统服务

    简介: php-fpm安装完成后默认不会注册为系统服务,所以需要手工添加系统服务脚本.在/etc/init.d目录下新建php-fpm文件,并更改权限其即可. 1.检测/usr/local/php/v ...

  5. Linux之chgrp

    命令功能: 在lunix系统里,文件或目录的权限的掌控以拥有者及所诉群组来管理.可以使用chgrp指令取变更文件与目录所属群组,这种方式采用群组名称或群组识别码都可以.Chgrp命令就是change  ...

  6. Kafka研究【一】:bring up环境

    kafka是干什么的,有和特性,我这里就不多说,详情自己研究官方文档. 0. 背景介绍 我需要在三台机器上分别部署kafka broker的实例,构建成一个集群.kafka的broker集群,是基于z ...

  7. 关于使用MAPVIEWOFFILE大文件的读写(DELPHI版)

    unit filemap; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  8. Hiero的spreadsheet中添加tag属性列

    Hiero在对剪辑线上的item进行管理的时候,往往会添加能多tag,而在管 理面板spreadsheet中却无法对tag进行查询,这是一件很麻烦的事,Hiero Development Guide中 ...

  9. Gradle Java Web应用程序并在Tomcat上运行

    1- 创建Gradle工程 打开 Eclipse ,在菜单中找到 File -> New -> Other…,在打开界面中选择 Gradle Project,如下图中所示 - 点击下一步( ...

  10. CheckFail设计很垃圾

        function checkFail(node, onError, fuckIE) {         var id = node.src;//检测是否死链         node.onlo ...