功能

加载指定的属性文件(*.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配置的用法的更多相关文章

  1. Mybatis 系列7-结合源码解析核心CRUD 配置及用法

    [Mybatis 系列10-结合源码解析mybatis 执行流程] [Mybatis 系列9-强大的动态sql 语句] [Mybatis 系列8-结合源码解析select.resultMap的用法] ...

  2. richface的配置、用法介绍和注意事项

    richface的配置.用法介绍和注意事项一.RichFaces (3.1.x) 技术需求 1.JDK 1.5 或更高版本: 2.支持的 JSF 实现: Sun JSF 1.1 RI - 1.2 My ...

  3. 【Django】MEDIA的配置及用法

    如果需要在数据库中存储图片或视频类的数据,我们可以配置MEDIA. 下面的示例将以上传一张图片的形式来说明MEDIA的配置及用法. 第一步 settings.py # media配置 MEDIA_UR ...

  4. .net core json配置相关用法

    在.net core中,配置文件差不多都是json文件.我们在开发程序的时候,可以使用系统默认的appsettings.json,可以自定义json配置文件.当json配置文件里面的参数改变时,程序也 ...

  5. Python3 中 configparser 模块解析配置的用法详解

    configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已 ...

  6. GIT配置及用法

    ssh配置 TortoiseGit配置 用法: 下面是我整理的常用 Git 命令清单.几个专用名词的译名如下. Workspace:工作区 Index / Stage:暂存区 Repository:仓 ...

  7. 【翻译】Tusdotnet中文文档(1)配置和用法

    TUSDOTNET Tusdotnet是tus协议的一个dotnet实现.tus协议是用来规范文件上传的整个过程,tus基于http协议,规定了一些上传过程中的头部(headers)和对上传过程的描述 ...

  8. log4net的基本配置及用法

    [1].[代码] [C#]代码 跳至 [1] [2] ? 1 2 using System.Reflection;  //使用反射 static private ILog log = log4net. ...

  9. httpd-2.2 配置及用法完全攻略

    导读 apache是一款稳定的流行的web软件,是linux操作系统中默认的web管理软件.在RHEL/Centos系列中可以用rpm直接进行安装,服务名为httpd.apache有很多设置和调优 的 ...

随机推荐

  1. forceUpdate() & set

    前言 在开发过程中,我们时常会遇到这样一种情况:当vue的data里边声明或者已经赋值过的对象或者数组(数组里边的值是对象)时,向对象中添加新的属性,如果更新此属性的值,是不会更新视图的. 根据官方文 ...

  2. TLFS 内存分配算法详解

    文章目录 1. DSA 背景介绍 1.1 mmheap 1.2 mmblk 2. TLFS 原理 2.1 存储结构 2.2 内存池初始化 2.3 free 2.4 malloc 参考资料 1. DSA ...

  3. tomcat9启动报错too low setting for -Xss

    在tomcat下部署war包启动时报错,关键错误信息如下: Caused by: java.lang.IllegalStateException: Unable to complete the sca ...

  4. python递归三战:Sierpinski Triangle、Tower of Hanoi、Maze Exploring

    本文已做成视频教程投稿b站(视频版相对文本版有一些改进),点击观看视频教程 本文主要通过三个实例来帮助大家理解递归(其展示动画已上传B站): 谢尔宾斯基三角形(Sierpinski Triangle) ...

  5. Nginx大厂面试需要掌握多少v1.21.3

    概述 **本人博客网站 **IT小神 www.itxiaoshen.com Nginx官网 最新版本为1.21.3 Nginx (engine x) 是一个开源的.高性能的HTTP和反向代理web服务 ...

  6. 实践案例1-利用低代码开发平台Odoo快速构建律师事务所管理系统

    今年10月份中旬的时候,有一段时间没联系的中学同学,我跟他关系比较好,突然打电话给我,希望我给他夫人的律所开发一个小系统.记得十几年前,当他还在他叔叔公司上班的,他是负责销售的,我们几乎每周都碰面,那 ...

  7. FJD1T1

    在考场上因为一些原因,系统编译不了. 于是在最后\(1h\)把\(T3\)得重打一遍,所以这题的暴力没有写完. 不过也确实很蠢,没想到做法. 考虑搜索原串中的字母的对应取值,然后计算出结果的柿子. 考 ...

  8. FVCOM泥沙模块河流边界处理

    简介 入流河流携带泥沙可以按照节点和边界两种形式给定,这两种方法都是在相关的节点上进行直接赋值,并不能保证进入计算域内泥沙总体积. 相关设置 XX_run.nml 河流参数设置 &NML_RI ...

  9. Machine Learning读书会,面试&算法讲座,算法公开课,创业活动,算法班集锦

    Machine Learning读书会,面试&算法讲座,算法公开课,创业活动,算法班集锦 近期活动: 2014年9月3日,第8次西安面试&算法讲座视频 + PPT 的下载地址:http ...

  10. PHP生成EXCEL,支持多个SHEET

    PHP生成EXCEL,支持多个SHEET 此版本为本人演绎版本,原版本地址http://code.google.com/p/php-excel/ php-excel.class.php: <?p ...