功能

  加载指定的属性文件(*.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. 【译】Spring 4 @PropertySource和@Value注解示例

    前言 译文链接:http://websystique.com/spring/spring-propertysource-value-annotations-example/ 本篇文章将展示如何通过@P ...

  2. Spring3.1新属性管理API:PropertySource、Environment、Profile

    Spring3.1提供了新的属性管理API,而且功能非常强大且很完善,对于一些属性配置信息都应该使用新的API来管理.虽然现在Spring已经到4版本了,这篇文章来的晚点. 新的属性管理API Pro ...

  3. SpringBoot2 java配置方式 Configuration和PropertySource结合读取配置文件

    JdbcConfig.java Configuration是配置文件 PropertySource 引入配置文件 value读取配置文件内容 package cn.itcast.config; imp ...

  4. SpringBoot入门教程(十八)@value、@Import、@ImportResource、@PropertySource

    Spring Boot提倡基于Java的配置.这两篇博文主要介绍springboot 一些常用的注解介绍 v@value 通过@Value可以将外部的值动态注入到Bean中. 添加applicatio ...

  5. Spring-boot中@ConfigurationProperties,@Value,@PropertySource

    1.利用@ConfigurationProperties获取配置的值,@ConfigurationProperties是springboot提供的基于安全类型的配置放置. application.pr ...

  6. SpringBoot标签之@ConfigurationProperties、@PropertySource注解的使用

    当获取主配置文件中属性值时,只需@ConfigurationProperties(prefix = "person")注解来修饰某类,其作用是告诉springBoot,此类中的属性 ...

  7. Spring读取外部的资源配置文件—@PropertySource和@Value实现资源文件配置

    通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值: @PropertySource注解主要是让Spring的Environment接口读取属性配置文件用的,标识在@ ...

  8. 02、@PropertySource指定配置文件的属性映射到JavaBean属性

    零.@PropertySource 功能类似于 <context:property-placeholder location="classpath*:/config/load.prop ...

  9. 基于spring的PropertySource类实现配置的动态替换

    public class ConfigPropertySource extends PropertySource<Properties> implements PriorityOrdere ...

  10. springboot @PropertySource

    @ConfigurationProperties(prefix="person") 默认加载全局配置文件 application.properties或application.ym ...

随机推荐

  1. Django 发送email配置详解及各种错误类型

    跟随Django Book的内容发送邮件不成功,总结一下需要配置好settings.py文件,还要注意一些细节. 1.在settings文件最后添加以下内容,缺一不可! EMAIL_HOST= 'sm ...

  2. 闪回之 Flashback Query (dml表、过程、函数、包等)、Flashback version Query

    Flashback Query 背景:Flashback 是 ORACLE 自 9i 就开始提供的一项特性,在 9i 中利用oracle 查询多版本一致的特点,实现从回滚段中读取表一定时间内操作过的数 ...

  3. drop asm disk、撤销drop asm disk

    drop asm disk.撤销drop asm disk drop asm disk:SQL> alter diskgroup XXX offline disk XXXX drop after ...

  4. CodeForces 1131G. Most Dangerous Shark

    题目简述:从左到右依次有$n \leq 10^7$个Domino骨牌,高度为$h_i$,手动推倒他的花费为$c_i$.每个骨牌之间的距离为$1$.一个骨牌可以被向左或者向右推倒.当第$i$个骨牌被推倒 ...

  5. Umbraco examine search media folder 中的pdf文件

    可以参考的文章 http://sleslie.me/2015/selecting-media-using-razor-slow-performance-examine-to-the-rescue/ h ...

  6. MS SQL的CASE...WHEN...THEN...END语法

    根据多个可能的答案检查一个值或变量. 举例说明: SELECT [type],CASE [type] WHEN 'TT' THEN 'TYPE_TABLE' WHEN 'FN' THEN 'SQL_S ...

  7. ZOJ3526【缩点思想】

    一开始做的时候(TLE),A,C,G这三类作为边,然后点和点直接建边搜个环:then time BOOM! 可以发现只属于"A"类的之间都并在一起就好,同理"G" ...

  8. Solr 6.7学习笔记(02)-- 配置文件 managed-schema (schema.xml) - Analyzer, tokenizer(4)

    有些时候,我们需要自定义 fieldType.下面的例子就是自定义的 fieldType,<analyzer type="index"> 表示索引时怎么处理,<a ...

  9. python的编码问题整理

    一.编码和解码 1.编码(encode):将人类可以识别的语言(英文.中文等)转化成机器语言(01串)的过程,用于存储. 2.解码(decode):将机器语言转化成人类可识别的语言的过程,用于显示. ...

  10. GHOST操作

    通过详细的图文说明,怎么用ghost备份系统,以备系统中毒或者感觉不好用时,快速恢复到备份时的状态! 工具/原料 2013最新雨林木风ghostxpxp3系统或者老毛桃等工具 方法/步骤 1 重新启动 ...