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++实现的,主要负责 ...
随机推荐
- 【杂题】[CodeForces 1172D] Nauuo and Portals【构造】
Description 有一个n*n的网格,你需要在上面设置一些传送门,传送门由两个配对的格子组成,从一个进入会立刻从另一个同一方向出来. 现在有n个人从第1列出发向右走,位于(i,1)的人要走到(r ...
- [LibreOJ 3124]【CTS2019】氪金手游【容斥原理】【概率】【树形DP】
Description Solution 首先它的限制关系是一个树形图 首先考虑如果它是一个外向树该怎么做. 这是很简单的,我们相当于每个子树的根都是子树中最早出现的点,概率是容易计算的. 设DP状态 ...
- 【BZOJ4456】 [Zjoi2016]旅行者 / 【UOJ #184】 【ZJOI2016】旅行者
Description 小Y来到了一个新的城市旅行.她发现了这个城市的布局是网格状的,也就是有n条从东到西的道路和m条从南到北 的道路,这些道路两两相交形成n×m个路口 (i,j)(1≤i≤n,1≤j ...
- ubuntu开启ssh服务时,报:start:Unknown job : ssh
这里是参考网站资料,并记录下. 如图所示: 解决方法: 输入以下命令即可 /usr/sbin/sshd mkdir /var/run/sshd /usr/sbin/sshd netstat -nlt ...
- Netflix的Ribbon主要负载均衡策略
1.简单轮询负载均衡 2.加权响应时间负载均衡 3.随机负载均衡 4.区域感知轮询负载均衡
- 套接字之select系统调用
select是IO多路复用的一种方式,用来等待一个列表中的多个描述符的可读可写状态: SYSCALL_DEFINE5(select, int, n, fd_set __user *, inp, fd_ ...
- Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 1099 bytes) in
解释是可用内存已耗尽,这关系到PHP的memory_limit的设置问题. 我在网上看到,有两种方法解决 1.修改php.ini memory_limit = 128 这种方法需要重启服务器,很显然, ...
- Postman下载与安装
融e学-一个专注于重构知识,培养复合型人才的平台:http://www.i-ronge.com/ Postman 的官网下载地址是:https://www.getpostman.com/ 下载后看到压 ...
- What is the !! (not not) operator in JavaScript?
What is the !! (not not) operator in JavaScript? 解答1 Coerces强制 oObject to boolean. If it was falsey ...
- 何为受控组件(controlled component)
在 HTML 中,类似 , 和 这样的表单元素会维护自身的状态,并基于用户的输入来更新:当用户提交表单时,前面提到的元素的值将随表单一起被发送.但在 React 中会有些不同,包含表单元素的组件将会在 ...