Spring Boot获取文件总的来说有三种方式,分别是@Value注解,@ConfigurationProperties注解和Environment接口。这三种注解可以配合着@PropertySource来使用,@PropertySource主要是用来指定具体的配置文件。
@PropertySource解析
@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;
}
- value():指定配置文件
- encoding():指定编码,因为properties文件的编码默认是ios8859-1,读取出来是乱码
- factory():自定义解析文件类型,因为该注解默认只会加载properties文件,如果想要指定yml等其他格式的文件需要自定义实现。

一、@Value注解读取文件
新建两个配置文件config.properties和configs.properties,分别写

本文介绍了Spring Boot中读取配置文件的三种方法:@Value注解、Environment接口和@ConfigurationProperties注解。@PropertySource用于指定配置文件,可配合编码设置和自定义工厂。@Value适合读取简单值,而@ConfigurationProperties适用于映射配置到实体类。
最低0.47元/天 解锁文章
9703

被折叠的 条评论
为什么被折叠?



