@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的更多相关文章
- 【译】Spring 4 @PropertySource和@Value注解示例
前言 译文链接:http://websystique.com/spring/spring-propertysource-value-annotations-example/ 本篇文章将展示如何通过@P ...
- Spring3.1新属性管理API:PropertySource、Environment、Profile
Spring3.1提供了新的属性管理API,而且功能非常强大且很完善,对于一些属性配置信息都应该使用新的API来管理.虽然现在Spring已经到4版本了,这篇文章来的晚点. 新的属性管理API Pro ...
- SpringBoot2 java配置方式 Configuration和PropertySource结合读取配置文件
JdbcConfig.java Configuration是配置文件 PropertySource 引入配置文件 value读取配置文件内容 package cn.itcast.config; imp ...
- SpringBoot入门教程(十八)@value、@Import、@ImportResource、@PropertySource
Spring Boot提倡基于Java的配置.这两篇博文主要介绍springboot 一些常用的注解介绍 v@value 通过@Value可以将外部的值动态注入到Bean中. 添加applicatio ...
- Spring-boot中@ConfigurationProperties,@Value,@PropertySource
1.利用@ConfigurationProperties获取配置的值,@ConfigurationProperties是springboot提供的基于安全类型的配置放置. application.pr ...
- SpringBoot标签之@ConfigurationProperties、@PropertySource注解的使用
当获取主配置文件中属性值时,只需@ConfigurationProperties(prefix = "person")注解来修饰某类,其作用是告诉springBoot,此类中的属性 ...
- Spring读取外部的资源配置文件—@PropertySource和@Value实现资源文件配置
通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值: @PropertySource注解主要是让Spring的Environment接口读取属性配置文件用的,标识在@ ...
- 02、@PropertySource指定配置文件的属性映射到JavaBean属性
零.@PropertySource 功能类似于 <context:property-placeholder location="classpath*:/config/load.prop ...
- 基于spring的PropertySource类实现配置的动态替换
public class ConfigPropertySource extends PropertySource<Properties> implements PriorityOrdere ...
- springboot @PropertySource
@ConfigurationProperties(prefix="person") 默认加载全局配置文件 application.properties或application.ym ...
随机推荐
- javaCV开发详解之8:转封装在rtsp转rtmp流中的应用(无须转码,更低的资源消耗)
javaCV系列文章: javacv开发详解之1:调用本机摄像头视频 javaCV开发详解之2:推流器实现,推本地摄像头视频到流媒体服务器以及摄像头录制视频功能实现(基于javaCV-FFMPEG.j ...
- 逻辑回归(LogisticRegression)(未完)
部分引用: http://blog.csdn.net/pakko/article/details/37878837 http://blog.csdn.net/sunbow0/article/detai ...
- vim跳转到文件的指定偏移位置
:goto 偏移量 例如:偏移到文件的第100个字节处 :goto 100
- UVaLive 3266 Tian Ji -- The Horse Racing (贪心)
题意:田忌赛马,每胜一局就得200,负一局少200,问最多得多少钱. 析:贪心,如果最快的马比齐王的还快,就干掉它,如果最慢的马比齐王的马快,就干掉它,否则用最慢的马去和齐王最快的马比. 代码如下: ...
- sqlserver2012——INTERSECT交查询
1. select a.成绩编号,a.分数,b.姓名 From 成绩信息 a,学生信息 b ' 一般的查询 a.成绩编号,a.分数,b.姓名 From 成绩信息 a,学生信息 b ' order a. ...
- 《剑指offer》面试题22—栈的压入、弹出序列
<程序员面试宝典>上也有经典的火车进站问题,类似. 如果12345是压栈顺序,序列45321可能是出栈顺序,但序列43512不可能. 规律:对序列中任意元素n,排在n后且比n小的元素一定是 ...
- 计总与排名SUM和RANK函数
准备一些数据: CREATE TABLE [dbo].[SalesPerformance]( ,) NOT NULL, ) NOT NULL, [OrderDate] [DATE] NULL, ,) ...
- SCUT - 205 - 饲养牛 - 最大流
https://scut.online/p/205 连着做所以一开始就觉得是网络流. 这种至多分配几次的很有网络流的特征. 一开始想从食物和饮料流向牛,但是怎么搞都不对. 其实可以从s流向食物,食物流 ...
- 51nod1060(反素数&dfs)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1060 题意:中文题诶- 思路: 这里用到了反素数的性质: 对 ...
- 洛谷 P2327 [SCOI2005]扫雷
P2327 [SCOI2005]扫雷 https://www.luogu.org/problem/show?pid=2327 题目描述 输入输出格式 输入格式: 第一行为N,第二行有N个数,依次为第二 ...