方法一:@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. spring-整合Struts2

    1. Spring 如何在 WEB 应用中使用 ? 1). 需要额外加入的 jar 包: spring-web-4.0.0.RELEASE.jarspring-webmvc-4.0.0.RELEASE ...

  2. VS2013 未找到与约束。。。

    新建项目或打开项目时,VS2013 未找到与约束...问题: 解决方案:控制面板>程序>程序和功能 找到如下选中软件右击修复 即可(修复前需关闭VS2013)

  3. NET设计模式 第二部分 结构性模式(14):结构型模式专题总结

    ——探索设计模式系列之十五 Terrylee,2006年5月 摘要:结构型模式,顾名思义讨论的是类和对象的结构,它采用继承机制来组合接口或实现(类结构型模式),或者通过组合一些对象,从而实现新的功能( ...

  4. Linux lsattr命令详解

    Linux lsattr命令 Linux lsattr命令用于显示文件属性. 用chattr执行改变文件或目录的属性,可执行lsattr指令查询其属性 用法: lsattr [-adlRvV][文件或 ...

  5. nginx只允许移动端访问( 判断拦截pc浏览器访问)

    set $mobile_request '0'; if ($http_user_agent ~* (Android|webOS|iPhone|iPod|BlackBerry)) { set $mobi ...

  6. python 多线程 并发socket实例

    sever side: import socketserver class MyTCPHandler(socketserver.BaseRequestHandler): def handle(self ...

  7. 黄聪:php7配置php.ini使其支持<? ?>

    <? ?>这种写在php配置文件里php.ini法叫short_tags,默认是不打开的,也就是,在默认配置的php里,这样写法不被认为是php脚本的,除非设置 short_open_ta ...

  8. HAAR小波

    HAAR小波分解信号或图像的“平滑”部分和“变化”部分(也许所有小波都这样?). 比如信号[1 2 3 4 5 6 7 8] 分解后(不考虑系数): [1.5 3.5 5.5 7.5]         ...

  9. 使用Html Agility Pack快速解析Html内容

    Html Agility Pack 是一个开源的.NET 方案HTML解析器. 开源地址:https://github.com/zzzprojects/html-agility-pack 用法:vs上 ...

  10. 【springmvc】之使用jQuery接收前端传入List对象

    前端代码: <form id="person_add" method="post" action="user"> <tab ...