@PropertySource
目录
- 功能
- 源码
- 使用示例
- 示例测试
-
功能
加载指定的属性文件(*.properties)到 Spring 的 Environment 中。可以配合 @Value 和 @ConfigurationProperties 使用。
- @PropertySource 和 @Value 组合使用,可以将自定义属性文件中的属性变量值注入到当前类的使用@Value注解的成员变量中。
- @PropertySource 和 @ConfigurationProperties 组合使用,可以将属性文件与一个Java类绑定,将属性文件中的变量值注入到该Java类的成员变量中
源码:12345678910111213141516171819202122232425262728293031323334353637383940414243
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
123
demo.name=huang
demo.sex=
1
demo.type=demo
示例一:@PropertySource + @Value
-
12345678910111213141516171819202122232425262728293031
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
-
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
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 + '\
''
+
'}'
;
}
}
示例测试
-
12345678910111213141516171819202122232425262728
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());
}
}
分类:
springboot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!