方式一
使用@Value 注解加上${}对配置文件中的参数进行提取
application.properties 中配置如下
aliyun.oss.file.keyid=LTAI4GBo8hXy3mh4cC2xkcoB
使用如下方式进行读取
@Value("${aliyun.oss.file.keyid}")
private String keyID;
正确读取后,会出现如下效果
方式二
使用@ConfigurationProperties 进行读取
在resource 目录下创建配置文件config.properties
demo.phone=10086
demo.wife=self
@ConfigurationProperties(prefix = “demo”)用于绑定属性,其中prefix表示所绑定的属性的前缀。
@PropertySource(value = “config.properties”)表示配置文件路径。
@Component
@ConfigurationProperties(prefix = "demo")
@PropertySource(value = "config.properties")
public class ConfigBeanProp {
private String phone;
private String wife;
}
注意方式属性名要与配置文件变量名要一致,才能自动配置。