在 Spring 框架中,@PropertySource
注解用于将外部的属性文件(如 .properties
或 .yml
文件)加载到 Spring 的环境(Environment
)中,从而可以在应用程序中通过 @Value
注解或 Environment
对象访问这些属性。
1. @PropertySource 的作用
- 加载属性文件:将指定的属性文件加载到 Spring 的
Environment
中。 - 属性注入:通过
@Value
注解或Environment
对象将属性值注入到 Bean 中。 - 支持多文件:可以加载多个属性文件,并指定优先级。
2. @PropertySource 的基本用法
(1)定义属性文件
在 src/main/resources
目录下创建一个属性文件,例如 app.properties
:
# app.properties
app.name=MyApplication
app.version=1.0.0
(2)使用 @PropertySource 加载属性文件
在配置类中使用 @PropertySource
注解加载属性文件。
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:app.properties")
public class AppConfig {
}
(3)通过 @Value 注入属性值
使用 @Value
注解将属性值注入到 Bean 中。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class AppInfo {
@Value("${app.name}")
private String appName;
@Value("${app.version}")
private String appVersion;
public void printInfo() {
System.out.println("App Name: " + appName);
System.out.println("App Version: " + appVersion);
}
}
(4)通过 Environment 对象获取属性值
通过 Environment
对象动态获取属性值。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class AppInfo {
@Autowired
private Environment env;
public void printInfo() {
String appName = env.getProperty("app.name");
String appVersion = env.getProperty("app.version");
System.out.println("App Name: " + appName);
System.out.println("App Version: " + appVersion);
}
}
3. @PropertySource 的高级用法
(1)加载多个属性文件
可以加载多个属性文件,并指定优先级。后加载的文件会覆盖先加载的文件中的同名属性。
@Configuration
@PropertySource("classpath:app.properties")
@PropertySource("classpath:override.properties")
public class AppConfig {
}
(2)忽略资源未找到
如果属性文件不存在,默认会抛出异常。可以通过 ignoreResourceNotFound
属性忽略未找到的资源。
@Configuration
@PropertySource(value = "classpath:missing.properties", ignoreResourceNotFound = true)
public class AppConfig {
}
(3)指定编码
如果属性文件使用了非默认编码(如 UTF-8),可以通过 encoding
属性指定编码。
@Configuration
@PropertySource(value = "classpath:app.properties", encoding = "UTF-8")
public class AppConfig {
}
(4)加载 YAML 文件
默认情况下,@PropertySource
不支持加载 YAML 文件。如果需要加载 YAML 文件,可以使用 YamlPropertySourceLoader
或第三方库(如 snakeyaml
)。
4. @PropertySource 与 @ConfigurationProperties 的结合
@PropertySource
可以与 @ConfigurationProperties
结合使用,将属性文件中的值绑定到一个 Java 对象中。
示例
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:app.properties")
@ConfigurationProperties(prefix = "app")
public class AppProperties {
private String name;
private String version;
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}
5. @PropertySource 的局限性
- 不支持 YAML 文件:默认不支持加载 YAML 文件,需要额外配置。
- 不支持动态刷新:
@PropertySource
加载的属性文件不支持动态刷新,如果需要动态刷新,可以使用 Spring Cloud Config 或@RefreshScope
。
总结
@PropertySource
的作用:加载外部属性文件到 Spring 的Environment
中。- 基本用法:
- 定义属性文件。
- 使用
@PropertySource
加载属性文件。 - 通过
@Value
或Environment
获取属性值。
- 高级用法:加载多个文件、忽略资源未找到、指定编码、结合
@ConfigurationProperties
使用。 - 局限性:不支持 YAML 文件和动态刷新。
THE END
暂无评论内容