@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 ...
随机推荐
- Redis 客户端安装与远程连接图解
Linux环境:Centos 6.8 Redis服务端版本:3.2.6 Redis客户端下载链接:https://redisdesktop.com/download 省略Linux系统安装Redis教 ...
- 'xxx' declared `static' but never defined
'xxx' declared `static' but never defined [问题描述] uart.c文件中有函数read_sample的实现: [plain] view plain copy ...
- IOS要用到的零碎东西
有些东西虽然不重要,但是零零碎碎会用到,就做个笔录吧: 协议中有2个关键字可以控制方法是否要实现(默认是@required),在大多数情况下, 用途在于程序员之间的交流 @required:这个方法必 ...
- 塞尔达:旷野之息个人对比上古卷轴V:天际
上古卷轴5是我之前玩过最优秀的作品.玩塞尔达的时候就有跟上古卷轴5比对,真的都是神作.两个游戏的自由度都是真的高. 主线剧情上,老滚5印象不深了,当时就知道战斗,只记住了开头砍头现场,还有奥杜因这个龙 ...
- [poj1144]Network(求割点模板)
解题关键:割点模板题. #include<cstdio> #include<cstring> #include<vector> #include<stack& ...
- MySQL 之 扩展例子
扩展例子 插入一条记录 INSERT INTO 表名 [(字段1,…,字段n)] VALUES (值1,…,值n) 插入查询的结果 INSERT INTO 表名 (字段1,…,字段n) VALUES ...
- 大白话5分钟带你走进人工智能-第二十六节决策树系列之Cart回归树及其参数(5)
第二十六节决策树系列之Cart回归树及其参数(5) 上一节我们讲了不同的决策树对应的计算纯度的计算方法, ...
- VS 远程部署程序
第1步 https://www.cnblogs.com/hydor/p/6604053.html 第2步 http://www.cnblogs.com/potential/p/3751426.html ...
- [UE4]如何替换角色Mesh上的Material材质
http://www.dawnarc.com/2016/10/ue4%E5%A6%82%E4%BD%95%E6%9B%BF%E6%8D%A2%E8%A7%92%E8%89%B2mesh%E4%B8%8 ...
- Unity3D 自动添加Fbx Animation Event
http://blog.csdn.net/aa20274270/article/details/52528449 using UnityEngine; using System.Collections ...