(七)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,要么全部 ...
随机推荐
- 【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片
一. Application用途 1. Application用途 创建Application时机 : Application在启动的时候会调用Application无参的构造方法创建实例; Appl ...
- LeetCode之“链表”:Remove Nth Node From End of List
题目链接 题目要求: Given a linked list, remove the nth node from the end of list and return its head. For ex ...
- mysql清空表
清空某个mysql表中所有内容 delete from 表名; truncate table 表名; 不带where参数的delete语句可以删除mysql表中所有内容,使用truncate tabl ...
- 飞思卡尔IMX6处理器的GPIO配置方式
在linux或android系统中,假如我们要配置飞思卡尔IMX6处理器的GPIO管脚,比如是GPIO_19这个管脚,那么要像这样: [cpp] view plaincopy #define MX6 ...
- 基于SVMLight的文本分类
支持向量机(Support Vector Machine)是Cortes和Vapnik于1995年首先提出的,它在解决小样本 .非线性及高维模式识别 中表现出许多特有的优势,并能够推广应用到函数拟合等 ...
- ORALCE EBS ALERT 初体验
Oracle EBS Alert Alert 是一种Oracle系统中的一种机制,它可以监视系统数据库,在规定的情况下给规定用户一个通知,通知可以是邮件或者其他形式,在标注的系统和客户化系统中都是可以 ...
- How tomcat works 读书笔记十五 Digester库 下
在这一节里我们说说ContextConfig这个类. 这个类在很早的时候我们就已经使用了(之前那个叫SimpleContextConfig),但是在之前它干的事情都很简单,就是吧context里的co ...
- openresty+websocket+redis simple chat
openresty 很早就支持websocket了,但是早期的版本cosocket是单工的,处理起来比较麻烦参见邮件列表讨论 websocket chat,后来的版本cosocket是双全工的,就可以 ...
- java设计原则---开闭原则
开闭原则:指的是一个软件实体应对对扩展开发,对修改关闭(Software entities should be open for extension, but closed for modificat ...
- 温故而后知新——对ado.net中常用对象的一些解释
在使用ado.net连接数据库获取数据,一般的步骤是: 1.设置好web.config //用来设置服务器数据库的地址以及登录名密码 2.创建Connection对象 //用来创建访问数据 ...