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

  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. mysql导入导出.sql数据

    导入sql的命令:source "路径名"+/mytest_emp_dept.sql 常用source 命令 进入mysql数据库控制台, 如mysql -u root -p my ...

  2. saiku应用的调试

    ubuntu下解压saiku包后使用: 运行.sh命令(.bat是windows命令).运行时注意权限.可以先chmod a+x *.sh 提示,catali?.sh出错. 这是tomcat的一个文件 ...

  3. 任务管理器中的PID找不到

    PID是Process ID的简称,这对WINDOWS开发人员来说是非常有用的信息,但对于普通用户来说则根本不必去理会.   举个例子来说: 在网站发布的时候,需要安装IIS,那么iis的tcp的80 ...

  4. 新版MATERIAL DESIGN 官方动效指南(一)

    Google 刚发布了新版Material Design 官方动效指南,全文包括三个部分:为什么说动效很重要?如何制作优秀的Material Design动效及转场动画,动效的意义.新鲜热辣收好不谢! ...

  5. ActiveMQ系列之一:ActiveMQ简介

    ActiveMQ是什么  ActiveMQ是Apache推出的,一款开源的,完全支持JMS1.1和J2EE 1.4规范的JMS   Provider实现的消息中间件 (Message Oriented ...

  6. linux下由带-开头文件想到的

    如果要删除文件-aaa,使用rm -aaa是不行的,rm会认为-后面的是参数.2种办法: 1 带明确路径指示 rm ./-aaa 2 使用 -- :rm -- -aaa 因为命令如果发现参数中有--, ...

  7. javascript中的instanceof运算符

    instanceof运算符希望左操作数是一个对象,右操作数表示对象的类:如果左侧的对象是右侧类的实例,则返回true,否则返回false.由于js中对象的类是通过初始化它们的构造函数来定义的,因此in ...

  8. 近期Mac上编译geany软件的总结

    以前在Mac上装过port,后来改为brew.但是原来port装在/opt/local下的很多程序没删掉,path路径也没改,导致很多程序被重复安装,配置混乱.最后我痛下狠心用port uninsta ...

  9. 简单了解JS中的几种遍历

    忙了好一段时间,项目上线后终于有那么一点点空档期静下来整理一些问题了.当我们在开发项目的时候,用到遍历的地方肯定少不了,那么我们有那么多的遍历方法,在不同情况下用那种方法会更优雅而且还没bug呢? 首 ...

  10. JFinal配合Shiro权限控制在FreeMarker模板引擎中控制到按钮粒度的使用

    实现在FreeMarker模板中控制对应按钮的显示隐藏主要用到了Shiro中的hasRole, hasAnyRoles, hasPermission以及Authenticated等方法,我们可以实现T ...