(七)SpringBoot2.0基础篇- application.properties属性文件的解析及获取
注:由于测试代码较多,影响查看效果,所以只放了核心代码,如需查看,请点示例代码
默认访问的属性文件为application.properties文件,可在启动项目参数中指定spring.config.location的参数:
java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
使用@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;
}
}使用@Value注解直接将属性值注入进修饰对对象中:
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*; @Component
public class MyBean { @Value("${name}")
private String name; // ... }使用@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;
}
}可以使用yaml文件格式来替换properties,属性获取方式不变(注:yaml文件后缀名为.yml)
使用POJO方式直接将属性注入进实体对象中:
application.yml
acme:
remote-address: 192.168.1.1
security:
username: admin
password: admincss
roles:
- USER
- ADMINAcmeProperties.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://gitee.com/lfalex/spring-boot-example/tree/dev/spring-boot-properties
(七)SpringBoot2.0基础篇- application.properties属性文件的解析及获取的更多相关文章
- (六)SpringBoot2.0基础篇- Redis整合(JedisCluster集群连接)
一.环境 Redis:4.0.9 SpringBoot:2.0.1 Redis安装:Linux(Redhat)安装Redis 二.SpringBoot整合Redis 1.项目基本搭建: 我们基于(五) ...
- (二)SpringBoot2.0基础篇- 静态资源的访问及Thymeleaf模板引擎的使用
一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf ...
- (五)SpringBoot2.0基础篇- Mybatis与插件生成代码
SpringBoot与Mybatis合并 一.创建SpringBoot项目,引入相关依赖包: <?xml version="1.0" encoding="UTF-8 ...
- (三)SpringBoot2.0基础篇- 持久层,jdbcTemplate和JpaRespository
一.介绍 SpringBoot框架为使用SQL数据库提供了广泛的支持,从使用JdbcTemplate的直接JDBC访问到完整的“对象关系映射”技术(如Hibernate).Spring-data-jp ...
- (一)SpringBoot2.0基础篇- 介绍及HelloWorld初体验
1.SpringBoot介绍: 根据官方SpringBoot文档描述,BUILD ANYTHING WITH SPRING BOOT (用SPRING BOOT构建任何东西,很牛X呀!),下面是官方文 ...
- (四)SpringBoot2.0基础篇- 多数据源,JdbcTemplate和JpaRepository
在日常开发中,经常会遇到多个数据源的问题,而SpringBoot也有相关API:Configure Two DataSources:https://docs.spring.io/spring-boot ...
- SpringBoot2.0 基础案例(14):基于Yml配置方式,实现文件上传逻辑
本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.文件上传 文件上传是项目开发中一个很常用的功能,常见的如头像上 ...
- 【转】WF4.0 (基础篇)
转自:http://www.cnblogs.com/foundation/category/215023.html 作者:WXWinter —— 兰竹菊梅★春夏秋冬☆ —— wxwinter@16 ...
- SpringBoot2.0 基础案例(12):基于转账案例,演示事务管理操作
本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.事务管理简介 1.事务基本概念 一组业务操作ABCD,要么全部 ...
随机推荐
- NIO模式例子
NIO模式主要优势是体现在对多连接的管理,对众多连接各种事件的转发让处理变得更加高效,所以一般是服务器端才会使用NIO模式,而对于客户端为了方便及习惯使用阻塞模式的Socket进行通信.所以NIO模式 ...
- Java-ServletRequestListener-ServletRequestAttributeListener
/** * A ServletRequestListener can be implemented by the developer * interested in being notified of ...
- Linux常用命令(第二版) --压缩解压缩命令
压缩解压缩命令: ----------.gz---------- 1.压缩 gzip[GNU zip]: /bin/gzip 格式: gzip 选项 [文件] #压缩文件,压缩后扩展名为.gz,Lin ...
- C++之默认参数
C++可以为不指定参数提供默认值.一旦给一个参数赋了默认值,后面的所有参数,也都必须为默认值,并且默认值的类型也必须正确,默认值可以在原型或者函数定义中给出,但是不能两个位置同时给出. 接下来我们上代 ...
- android 防止反编译的若干方法
第一种方式:混淆策略 混淆策略是每个应用必须增加的一种防护策略,同时他不仅是为了防护,也是为了减小应用安装包的大小,所以他是每个应用发版之前必须要添加的一项功能,现在混淆策略一般有两种: 对代码的混淆 ...
- RTMPdump(libRTMP) 源代码分析 9: 接收消息(Message)(接收视音频数据)
===================================================== RTMPdump(libRTMP) 源代码分析系列文章: RTMPdump 源代码分析 1: ...
- leetCode之旅(14)-Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...
- iOS 字体权重weight
UIFontWeightUltraLight - 超细字体 UIFontWeightThin - 纤细字体 UIFontWeightLight - 亮字体 UIFontWeightRegular ...
- datetime的精度
最近有需要将分钟线的数据进行内联拼接,但时间没有必要精确到秒,微秒. df['datetime'] = pd.to_datetime(df['datetime']) df = df.set_index ...
- Day7 类的继承和继承实现的原理
继承可以分为但继承,多继承. 继承的基本形式 class ParentClass1(object): #定义父类 pass class ParentClass2: #定义父类 pass class S ...