Springboot2.0加载指定配置文件@PropertySource的使用
1. 在resouces下编写待加载的配置文件
这里使用person.properties
# String
person.last-name=john
# int
person.age=112
# boolean
person.boss=false
# Date
person.birth=2019/11/12
# List<Object>
person.dogs[0].name=jj
person.dogs[0].age=111
person.dogs[1].name=tom
person.dogs[1].age=111
# Map<String, Object>
person.maps.Jack=jackc2
person.maps.Iris=yili8
person.maps.Panda=pandax
person.maps.Adun=gengwang
person.maps.Caedmon=chuankwa
# List
person.lists[0]=1
person.lists[1]=2
person.lists[2]=3
person.lists[3]=4
person.lists[4]=5
2. 在启动类中添加bean类@EnableConfigurationProperties
package cn.jfjb;
import cn.jfjb.bean.Person;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@SpringBootApplication
@EnableConfigurationProperties({Person.class})
public class SbHelloworld01QuickApplication {
public static void main(String[] args) {
SpringApplication.run(SbHelloworld01QuickApplication.class, args);
}
}
3. 在bean中使用@PropertySource加载
package cn.jfjb.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @author john
* @date 2019/11/22 - 8:56
*/
@Component
@PropertySource("classpath:person.properties")
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private boolean boss;
private Date birth;
private List<Dog> dogs;
private Map<String, Object> maps;
private List<Object> lists;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public boolean isBoss() {
return boss;
}
public void setBoss(boolean boss) {
this.boss = boss;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public List<Dog> getDogs() {
return dogs;
}
public void setDogs(List<Dog> dogs) {
this.dogs = dogs;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", dogs=" + dogs +
", maps=" + maps +
", lists=" + lists +
'}';
}
}
4. test
Springboot2.0加载指定配置文件@PropertySource的使用的更多相关文章
- SpringBoot系列——加载自定义配置文件
前言 SpringBoot启动时默认加载bootstrap.properties或bootstrap.yml(这两个优先级最高).application.properties或application. ...
- 19、属性赋值-@PropertySource加载外部配置文件
19.属性赋值-@PropertySource加载外部配置文件 加载外部配置文件的注解 19.1 [xml] 在原先的xml 中需要 导入context:property-placeholder 声明 ...
- springboot加载外部配置文件
网上搜集和整理如下(自己已验证过) 1. war包在tomcat中加载外部配置文件 war包运行在独立tomcat下时,如何加载war包外部配置application.properties,以达到每次 ...
- @ConfigurationProperties(pref="")加载局部配置文件
刚开始@ConfigurationProperties(文件名)直接在参数里加文件名,其实是配置前缀pref="前缀".加载局部配置文件是@PropertySource(value ...
- Spring中加载xml配置文件的六种方式
Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog 因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...
- selenium启动Chrome时,加载用户配置文件
selenium启动Chrome时,加载用户配置文件 Selenium操作浏览器是不加载任何配置的,网上找了半天,关于Firefox加载配置的多点,Chrome资料很少,下面是关于加载Chrome ...
- 背水一战 Windows 10 (64) - 控件(WebView): 加载指定 HttpMethod 的请求, 自定义请求的 http header, app 与 js 的交互
[源码下载] 背水一战 Windows 10 (64) - 控件(WebView): 加载指定 HttpMethod 的请求, 自定义请求的 http header, app 与 js 的交互 作者: ...
- Spring加载XML配置文件
原创链接:http://www.cnblogs.com/yanqin/p/5282929.html(允许转载,但请注明原创链接) BeanFactory加载单个文件 当使用beanfactory去获取 ...
- JVM自定义类加载器加载指定classPath下的所有class及jar
一.JVM中的类加载器类型 从Java虚拟机的角度讲,只有两种不同的类加载器:启动类加载器和其他类加载器. 1.启动类加载器(Boostrap ClassLoader):这个是由c++实现的,主要负责 ...
随机推荐
- spring boot 项目打包后无法通过命令行传入参数
java -jar .\tk-provider.jar --spring.profiles.active=test 本想用测试环境的配置文件运行项目可项目启动时一直是使用dev配置文件运行. java ...
- web文件夹上传下载方案
第一点:Java代码实现文件上传 FormFile file = manform.getFile(); String newfileName = null; String newpathname = ...
- AcWing:245. 你能回答这些问题吗(线段树最大子段和)
给定长度为N的数列A,以及M条指令,每条指令可能是以下两种之一: 1.“1 x y”,查询区间 [x,y] 中的最大连续子段和,即 maxx≤l≤r≤ymaxx≤l≤r≤y{∑ri=lA[i]∑i=l ...
- Python中的不可变对象类型与可变对象类型
https://blog.csdn.net/answer3lin/article/details/86430074 其实各个标准资料中没有说明Python有值类型和引用类型的分类,这个分类一般是C++ ...
- C++入门经典-例2.5-使用附加格式说明scanf函数的格式输入
1:代码如下: // 2.5.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" int main() { long iLong; /*长整型变 ...
- TCP 之 TCP首部
TCP首部图 TCP首部说明 源端口,目的端口 用于寻找发送端和接收端应用进程.(源IP,源端口,目的IP,目的端口) 四元组确定唯一一个TCP连接:(IP,端口)也称为一个插口(socket): 序 ...
- day8_文件操作及编码解码
一.文件操作基本流程 计算机系统分为:计算机硬件,操作系统,应用程序三部分. 我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,众所 ...
- Color色彩
Element 为了避免视觉传达差异,使用一套特定的调色板来规定颜色,为你所搭建的产品提供一致的外观视觉感受. ¶主色 Element 主要品牌颜色是鲜艳.友好的蓝色. Blue #409EFF ¶辅 ...
- Go语言程序结构
注意:Go语言源码文件编码格式必须是 UTF-8 格式,否则会导致编译器出错. 1.语言变量 a) 指定变量类型,声明后若不赋值,使用默认值. var name string b) 根据值自行判定变量 ...
- Selenium 2自动化测试实战10(鼠标事件)
一.鼠标事件 1. webdriver中,关于鼠标操作的方法封装在ActionChains类中 ActionChains类提供了鼠标操作的常用方法: perform:执行所有ActionChains中 ...