功能

加载指定的属性文件(*.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. java问题排查总结
  2. 关于js中window.location.href,location.href,parent.location.href,top.location.href的用法
  3. 如何理解和熟练运用js中的call及apply?
  4. arcgis engine 调用arcgis server服务
  5. iOS tableview 选中Cell后的背景颜色和文字颜色
  6. PHP 开发环境配置
  7. Linux下修改网卡的mac地址
  8. Codevs 1702 素数判定 2(Fermat定理)
  9. Linux命令之ssh
  10. Mnesia基本用法
  11. Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架
  12. SDUTOJ 2054 双向链表
  13. Jquery EasyUI datagrid 的一些问题
  14. 网络通信协议,TCP和UDP 的区别
  15. innobackupex 远程备份
  16. java将图片传为设定编码值显示(可做刺绣)
  17. What&#39;s the Difference Between Iterators and Generators in Python
  18. Java中的 内部类(吐血总结)
  19. 给自己的博客上添加个flash宠物插件
  20. C#调用百度地图API经验分享(四)

热门文章

  1. Python进阶(多线程)
  2. ndarray 数组的创建和变换
  3. Roslyn 编译器Api妙用:动态生成类并实现接口
  4. [cf1434E]A Convex Game
  5. 一文理解Java-class字节码文件
  6. docker创建mongodb并且测试代码
  7. 由于vue的for循环id并不严谨,提高id严谨性
  8. perl中tr的用法(转载)
  9. 学习java的第二十七天
  10. 巩固javaweb的第二十天