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的使用的更多相关文章

  1. SpringBoot系列——加载自定义配置文件

    前言 SpringBoot启动时默认加载bootstrap.properties或bootstrap.yml(这两个优先级最高).application.properties或application. ...

  2. 19、属性赋值-@PropertySource加载外部配置文件

    19.属性赋值-@PropertySource加载外部配置文件 加载外部配置文件的注解 19.1 [xml] 在原先的xml 中需要 导入context:property-placeholder 声明 ...

  3. springboot加载外部配置文件

    网上搜集和整理如下(自己已验证过) 1. war包在tomcat中加载外部配置文件 war包运行在独立tomcat下时,如何加载war包外部配置application.properties,以达到每次 ...

  4. @ConfigurationProperties(pref="")加载局部配置文件

    刚开始@ConfigurationProperties(文件名)直接在参数里加文件名,其实是配置前缀pref="前缀".加载局部配置文件是@PropertySource(value ...

  5. Spring中加载xml配置文件的六种方式

    Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog  因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...

  6. selenium启动Chrome时,加载用户配置文件

    selenium启动Chrome时,加载用户配置文件   Selenium操作浏览器是不加载任何配置的,网上找了半天,关于Firefox加载配置的多点,Chrome资料很少,下面是关于加载Chrome ...

  7. 背水一战 Windows 10 (64) - 控件(WebView): 加载指定 HttpMethod 的请求, 自定义请求的 http header, app 与 js 的交互

    [源码下载] 背水一战 Windows 10 (64) - 控件(WebView): 加载指定 HttpMethod 的请求, 自定义请求的 http header, app 与 js 的交互 作者: ...

  8. Spring加载XML配置文件

    原创链接:http://www.cnblogs.com/yanqin/p/5282929.html(允许转载,但请注明原创链接) BeanFactory加载单个文件 当使用beanfactory去获取 ...

  9. JVM自定义类加载器加载指定classPath下的所有class及jar

    一.JVM中的类加载器类型 从Java虚拟机的角度讲,只有两种不同的类加载器:启动类加载器和其他类加载器. 1.启动类加载器(Boostrap ClassLoader):这个是由c++实现的,主要负责 ...

随机推荐

  1. confluence -- 命令行备份还原

    备份:confluence每日凌晨2:00都在 /data/atlassian/application-data/confluence/backups/ 下生成备份包,其中包括文档,附件,用户 还原: ...

  2. kali修改更新源及更新

    进入源文件进行修改 leafpad /etc/apt/sources.list (其实系统本身就自带了更新源,去掉其注释也是可以的,现在官方源的下载速度也还ok,下面的三选一亦可) #kali官方源 ...

  3. jQuery实现表单动态添加与删除数据操作示例

    <!DOCTYPE html> <html lang="en"> <head>   <meta charset="UTF-8&q ...

  4. C++入门经典-例2.10-控制输出精确度

    1:代码如下: // 2.10.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> usin ...

  5. 云服务器搭建anaconda pytorch torchvision

    (因为在普通用户上安装有些权限问题安装出错,所以我在root用户下相对容易安装,但是anaconda官网说可以直接在普通用户下安装,不过,在root下安装,其他用户也是能用的. 访问Anaconda官 ...

  6. c++ STL -- set和multiset

    set和multiset 1.结构 set和multiset会根据特定的排序原则将元素排序.两者不同之处在于,multisets允许元素重复,而set不允许重复. 只要是assignable.copy ...

  7. 全面解读php-运算符

    一.运算符的优先级 二.短路作用 本文为袋鼠学习中的总结,如有转载请注明出处:https://www.cnblogs.com/chrdai/p/11074776.html

  8. Django中csrf token验证原理

    我多年没维护的博客园,有一篇初学Django时的笔记,记录了关于django-csrftoekn使用笔记,当时几乎是照抄官网的使用示例,后来工作全是用的flask.博客园也没有维护.直到我的博客收到了 ...

  9. react-native 环境安装常见问题

    npm install react-native-cli -g react-native init yourproject npm install react-native run-ios 问题1:卡 ...

  10. linux如何杀掉进程(kill)

    方法/步骤1: 使用“ps -e|grep mysql”命令,查看mysql程序的对应的pid号.结果如下图:   方法/步骤2: 使用“kill -9 2891”命令,可以结束掉mysqld_saf ...