@PropertySource配置的用法
功能
加载指定的属性文件(*.properties)到 Spring 的 Environment 中。可以配合 @Value 和@ConfigurationProperties 使用。
@PropertySource 和 @Value
组合使用,可以将自定义属性文件中的属性变量值注入到当前类的使用@Value注解的成员变量中。
@PropertySource 和 @ConfigurationProperties
组合使用,可以将属性文件与一个Java类绑定,将属性文件中的变量值注入到该Java类的成员变量中。
源码


package org.springframework.context.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.io.support.PropertySourceFactory;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource {
/**
* 属性源的名称
*/
String name() default "";
/**
* 属性文件的存放路径
*/
String[] value();
/**
* 如果指定的属性源不存在,是否要忽略这个错误
*/
boolean ignoreResourceNotFound() default false;
/**
* 属性源的编码格式
*/
String encoding() default "";
/**
* 属性源工厂
*/
Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
}
使用示例
属性文件:demo.properties
demo.name=huang
demo.sex=1
demo.type=demo
示例一:@PropertySource + @Value
package com.huang.pims.demo.props;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = {"demo/props/demo.properties"})
public class ReadByPropertySourceAndValue {
@Value("${demo.name}")
private String name;
@Value("${demo.sex}")
private int sex;
@Value("${demo.type}")
private String type;
@Override
public String toString() {
return "ReadByPropertySourceAndValue{" +
"name='" + name + '\'' +
", sex=" + sex +
", type='" + type + '\'' +
'}';
}
}
示例二:@PropertySource 和 @ConfigurationProperties
package com.huang.pims.demo.props;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = {"demo/props/demo.properties"})
@ConfigurationProperties(prefix = "demo")
public class ReadByPropertySourceAndConfProperties {
private String name;
private int sex;
private String type;
public void setName(String name) {
this.name = name;
}
public void setSex(int sex) {
this.sex = sex;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public int getSex() {
return sex;
}
public String getType() {
return type;
}
@Override
public String toString() {
return "ReadByPropertySourceAndConfProperties{" +
"name='" + name + '\'' +
", sex=" + sex +
", type='" + type + '\'' +
'}';
}
}
示例测试
package com.huang.pims.demo.runners;
import com.huang.pims.demo.props.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class OutputPropsRunner implements CommandLineRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(OutputPropsRunner.class);
@Autowired
private ReadByPropertySourceAndValue readByPropertySourceAndValue;
@Autowired
private ReadByPropertySourceAndConfProperties readByPropertySourceAndConfProperties;
@Override
public void run(String... args) throws Exception {
LOGGER.info(readByPropertySourceAndValue.toString());
LOGGER.info(readByPropertySourceAndConfProperties.toString());
}
}
启动项目即可看到效果。

@PropertySource配置的用法的更多相关文章
- Mybatis 系列7-结合源码解析核心CRUD 配置及用法
[Mybatis 系列10-结合源码解析mybatis 执行流程] [Mybatis 系列9-强大的动态sql 语句] [Mybatis 系列8-结合源码解析select.resultMap的用法] ...
- richface的配置、用法介绍和注意事项
richface的配置.用法介绍和注意事项一.RichFaces (3.1.x) 技术需求 1.JDK 1.5 或更高版本: 2.支持的 JSF 实现: Sun JSF 1.1 RI - 1.2 My ...
- 【Django】MEDIA的配置及用法
如果需要在数据库中存储图片或视频类的数据,我们可以配置MEDIA. 下面的示例将以上传一张图片的形式来说明MEDIA的配置及用法. 第一步 settings.py # media配置 MEDIA_UR ...
- .net core json配置相关用法
在.net core中,配置文件差不多都是json文件.我们在开发程序的时候,可以使用系统默认的appsettings.json,可以自定义json配置文件.当json配置文件里面的参数改变时,程序也 ...
- Python3 中 configparser 模块解析配置的用法详解
configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已 ...
- GIT配置及用法
ssh配置 TortoiseGit配置 用法: 下面是我整理的常用 Git 命令清单.几个专用名词的译名如下. Workspace:工作区 Index / Stage:暂存区 Repository:仓 ...
- 【翻译】Tusdotnet中文文档(1)配置和用法
TUSDOTNET Tusdotnet是tus协议的一个dotnet实现.tus协议是用来规范文件上传的整个过程,tus基于http协议,规定了一些上传过程中的头部(headers)和对上传过程的描述 ...
- log4net的基本配置及用法
[1].[代码] [C#]代码 跳至 [1] [2] ? 1 2 using System.Reflection; //使用反射 static private ILog log = log4net. ...
- httpd-2.2 配置及用法完全攻略
导读 apache是一款稳定的流行的web软件,是linux操作系统中默认的web管理软件.在RHEL/Centos系列中可以用rpm直接进行安装,服务名为httpd.apache有很多设置和调优 的 ...
随机推荐
- git rebase 合并提交
git rebase 合并提交 合并最近多次提交记录 语法 git rebase -i HEAD~n 1.进入合并模式 合并最近三次提交 git rebase -i HEAD~3 然后你会看到一个像下 ...
- postman使用(待更新)
参考文档
- Merge into用法总结
简单的说就是,判断表中有没有符合on()条件中的数据,有了就更新数据,没有就插入数据. 有一个表T,有两个字段a.b,我们想在表T中做Insert/Update,如果条件满足,则更新T中b的值,否则在 ...
- 暑假算法练习Day3
第三天!!!最近要开始归纳总结Python学习啦!! 1006 换个格式输出整数 (15 分) 让我们用字母 B 来表示"百".字母 S 表示"十",用 12. ...
- nrf52832蓝牙开发踩过的坑
接触nrf52832芯片已经有一段时间了,记录我踩过的坑.这些坑大多都补回来了,愿后来的开发者没有坑~ 先来点开胃小菜 一.环境问题 安装离线包遇到的错误--svdconv exited with a ...
- Linux(kali)基础设置
本笔记的友情链接 常用目录介绍 boot 存放启动文件 dev 存放设备文件 etc 存放配置文件 home 普通用户家目录,以/home/$username的方式存放 media 移动存储自动挂载目 ...
- [noi1773]function
以统计x坐标的数量为例:x为下标建一棵线段树,然后对每一个区间按照y坐标建一棵可持久化线段树(每一个x只保留最大的一个y),询问时,二分找到这个区间内最大的y以前的点并统计,复杂度为$o(nlog^{ ...
- javascript-初级-day04js数据类型
day01-js数据类型 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type& ...
- NOIp2021 原地退役记
JS-0013 Day -2 - 2021.11.17 打了场 cmd_blk 出的模拟赛,T2 T3 都是做过的原题(AGC010C & ARC092D),于是直接摆烂交暴力垫底,成功为我的 ...
- 【2020五校联考NOIP #8】自闭
题目传送门 题意: 有一个 \(n \times m\) 的矩阵,里面已经填好了 \(k\) 个非负整数. 问是否能在其它 \(n \times m-k\) 个格子里各填上一个非负整数,使得得到的矩阵 ...