注:由于测试代码较多,影响查看效果,所以只放了核心代码,如需查看,请点示例代码

  1. 默认访问的属性文件为application.properties文件,可在启动项目参数中指定spring.config.location的参数:

    java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

    参考官方文档:https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/reference/htmlsingle/#boot-features-external-config-application-property-files

  2. 使用@PropertySource来获取配置文件的中属性值(注意:在使用该注解时,属性文件必须为properties文件,yaml文件不可用):

    @Configuration
    @PropertySource("classpath:/app.properties")
    public class AppConfig { @Autowired
    Environment env; @Bean
    public TestBean testBean() {
    TestBean testBean = new TestBean();
    testBean.setName(env.getProperty("testbean.name"));
    return testBean;
    }
    }

    参考官方文档:https://docs.spring.io/spring/docs/5.0.6.RELEASE/javadoc-api/org/springframework/context/annotation/PropertySource.html

  3. 使用@Value注解直接将属性值注入进修饰对对象中:

    import org.springframework.stereotype.*;
    import org.springframework.beans.factory.annotation.*; @Component
    public class MyBean { @Value("${name}")
    private String name; // ... }

    参考官方文档:https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/reference/htmlsingle/#boot-features-external-config

  4. 使用@ConfigurationProperties(prefix="my")将属性值注入进对象,可以注入对象的属性key的对象,也可以注入进List或Set中,但是属性的书写需要有规范:

    my.servers0=dev.example.com
    my.servers1=another.example.com

    使用方式

    @ConfigurationProperties(prefix="my")
    public class Config {
    //set,list不需要Setter方法
    private List<String> servers = new ArrayList<String>(); public List<String> getServers() {
    return this.servers;
    }
    }

    参考官方文档:https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/reference/htmlsingle/#boot-features-external-config-loading-yaml

  5. 可以使用yaml文件格式来替换properties,属性获取方式不变(注:yaml文件后缀名为.yml)

  6. 使用POJO方式直接将属性注入进实体对象中:

    application.yml

    acme:
    remote-address: 192.168.1.1
    security:
    username: admin
    password: admincss
    roles:
    - USER
    - ADMIN

    AcmeProperties.java

    package com.example;
    
    import java.net.InetAddress;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties("acme")
    public class AcmeProperties { private InetAddress remoteAddress; private final Security security = new Security(); public InetAddress getRemoteAddress() { ... } public void setRemoteAddress(InetAddress remoteAddress) { ... } public Security getSecurity() { ... } public static class Security { private String username; private String password; private List<String> roles = new ArrayList<>(Collections.singleton("USER")); public String getUsername() { ... } public void setUsername(String username) { ... } public String getPassword() { ... } public void setPassword(String password) { ... } public List<String> getRoles() { ... } public void setRoles(List<String> roles) { ... } }
    }

参考文档:https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/reference/htmlsingle/#boot-features-external-config-typesafe-configuration-properties

代码示例:https://gitee.com/lfalex/spring-boot-example/tree/dev/spring-boot-properties

(七)SpringBoot2.0基础篇- application.properties属性文件的解析及获取的更多相关文章

  1. (六)SpringBoot2.0基础篇- Redis整合(JedisCluster集群连接)

    一.环境 Redis:4.0.9 SpringBoot:2.0.1 Redis安装:Linux(Redhat)安装Redis 二.SpringBoot整合Redis 1.项目基本搭建: 我们基于(五) ...

  2. (二)SpringBoot2.0基础篇- 静态资源的访问及Thymeleaf模板引擎的使用

    一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf ...

  3. (五)SpringBoot2.0基础篇- Mybatis与插件生成代码

    SpringBoot与Mybatis合并 一.创建SpringBoot项目,引入相关依赖包: <?xml version="1.0" encoding="UTF-8 ...

  4. (三)SpringBoot2.0基础篇- 持久层,jdbcTemplate和JpaRespository

    一.介绍 SpringBoot框架为使用SQL数据库提供了广泛的支持,从使用JdbcTemplate的直接JDBC访问到完整的“对象关系映射”技术(如Hibernate).Spring-data-jp ...

  5. (一)SpringBoot2.0基础篇- 介绍及HelloWorld初体验

    1.SpringBoot介绍: 根据官方SpringBoot文档描述,BUILD ANYTHING WITH SPRING BOOT (用SPRING BOOT构建任何东西,很牛X呀!),下面是官方文 ...

  6. (四)SpringBoot2.0基础篇- 多数据源,JdbcTemplate和JpaRepository

    在日常开发中,经常会遇到多个数据源的问题,而SpringBoot也有相关API:Configure Two DataSources:https://docs.spring.io/spring-boot ...

  7. SpringBoot2.0 基础案例(14):基于Yml配置方式,实现文件上传逻辑

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.文件上传 文件上传是项目开发中一个很常用的功能,常见的如头像上 ...

  8. 【转】WF4.0 (基础篇)

    转自:http://www.cnblogs.com/foundation/category/215023.html 作者:WXWinter  ——  兰竹菊梅★春夏秋冬☆ —— wxwinter@16 ...

  9. SpringBoot2.0 基础案例(12):基于转账案例,演示事务管理操作

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.事务管理简介 1.事务基本概念 一组业务操作ABCD,要么全部 ...

随机推荐

  1. java自带dom工具使用实例

    代码参考自 黄亿华大神的<<1000行代码读懂Spring(一)- 实现一个基本的IoC容器>> 原网页如下 http://my.oschina.net/flashsword/ ...

  2. C++智能指针及其简单实现

    本文将简要介绍智能指针shared_ptr和unique_ptr,并简单实现基于引用计数的智能指针. 使用智能指针的缘由 1. 考虑下边的简单代码: int main() { ); ; } 就如上边程 ...

  3. 瑞芯微RK3188如何配置USB摄像头支持

  4. FineReport性能调优的一些办法

    FineReport性能调优的基本思路,就要对应用服务器的内存大小进行合理的设置. 一般服务器默认的内存配置都比较小,在较大型的应用项目中,这点内存是不够的,因此需要加工使其调大. 各应用服务器的内存 ...

  5. SharePoint 读取选项字段所有Choise

    对象模型SPFieldChoice SPSite site = SPContext.Current.Site; SPWeb web = site.OpenWeb(SubWebUrl); SPList ...

  6. C# 操作Word 文档——添加Word页眉、页脚和页码

    在Word文档中,我们可以通过添加页眉.页脚的方式来丰富文档内容.添加页眉.页脚时,可以添加时间.日期.文档标题,文档引用信息.页码.内容解释.图片/LOGO等多种图文信息.同时也可根据需要调整文字或 ...

  7. OpenNMS安装手册

    一. 系统需求Windows Server 2008 R2 SP1 64位JDK 8 update 5 for Windows 64位PostgreSQL 9.3.5 for Windows 64位O ...

  8. 听《津津乐道》ThinkPad专题节目有感

    自2011年使用Mac以来,就没怎么想过要再换一个windows使用,可是前几天听了<津津乐道>播客节目,主播朱峰讲了ThinkPad的使用经历,这个倒是让我回想起第一次见到IBM电脑时的 ...

  9. 北京一家JAVA开发公司面试题(留给后人)

    1.jsp有哪些内置对象?作用分别是什么? 2.描述一下servlet的生命周期和基本架构. 3.多线程有几种实现方法,都是什么? 同步有几种实现方法,都是什么? 4.作用域public   priv ...

  10. LINQ、Lambda与委托

    首先定义个Person类: public class Person { public string Name{get;set;} //姓名 public int Age{get;set;} //年龄 ...