功能

  加载指定的属性文件(*.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());
} }

启动项目即可看到效果。

  从截图中可以看出,需要读取的属性配置,都已经成功读取出来了。

最新文章

  1. js构建ui的统一异常处理方案(二)
  2. utils.js
  3. Halcon与opencv格式的转换
  4. linq 之 Distinct的使用
  5. non
  6. sql over开窗函数,
  7. GIT 命令 操作 记录
  8. 谷歌笔试题&mdash;&mdash;排序,只允许0和其他元素交换
  9. Epoll之ET、LT模式
  10. U5首次登录
  11. Spring + Spring MVC + Hibernate项目开发集成(注解)
  12. [Jenkins]怎么删除jenkins里项目配置的svn记录
  13. CSS中RGBA的兼容方法以及透明度计算方法
  14. 使用dom4j解析XML例子
  15. javascript 函数 add(1)(2)(3)(4)实现无限极累加 —— 一步一步原理解析
  16. 某次模拟考试day2t3 菊菊的数据结构
  17. LeakCanary使用手册
  18. 【朝花夕拾】Android安全之(一)权限篇
  19. 一起学爬虫——使用xpath库爬取猫眼电影国内票房榜
  20. POJ 1390 Blocks(区间DP)

热门文章

  1. python爬虫知识点总结(二)爬虫的基本原理
  2. PLSQ创建用户L
  3. 《精通Spring4.X企业应用开发实战》读后感第四章(Application中Bean的生命周期)
  4. 懒人模式开启Android模块自动化Api之旅
  5. MYSQL数据库设计规范11111
  6. xampp搭建discuz论坛
  7. Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)
  8. excel 恢复忘记保存的文档
  9. [Xcode 实际操作]六、媒体与动画-(9)使用CATransaction Push制作入场动画
  10. hadoop是什么?新手自学hadoop教程【附】大数据系统学习教程