1.application.properties文件
#阿里云 OSS #不同的服务器,地址不同 aliyun.oss.file.endpoint=oss-cn-beijing.aliyuncs.com aliyun.oss.file.keyid=LT aliyun.oss.file.keysecret=VQGvW #bucket可以在控制台创建,也可以使用java代码创建 aliyun.oss.file.bucketname=aaa
2.java工具类
package com.stu.oss.utils; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; //项目启动,spring接口,spring加载之后,执行接口一个方法 @Component public class ConstantPropertiesUtil implements InitializingBean { //读取配置文件的内容 @Value("${aliyun.oss.file.endpoint}") private String endpoint; @Value("${aliyun.oss.file.keyid}") private String keyid; @Value("${aliyun.oss.file.keysecret}") private String keysecret; @Value("${aliyun.oss.file.bucketname}") private String bucketname; //定义一些静态常量 public static String END_POINT; public static String KEY_ID; public static String KEY_SECRET; public static String BUCKET_NAME; //上边赋值完成后,会执行afterPropertiesSet方法,这是spring机制 @Override public void afterPropertiesSet() throws Exception { END_POINT = endpoint; KEY_ID = keyid; KEY_SECRET = keysecret; BUCKET_NAME = bucketname; } }
3.通过工具类取得属性文件的值
String endPoint = ConstantPropertiesUtil.END_POINT; String accessKeyId = ConstantPropertiesUtil.KEY_ID; String accessKeySecret = ConstantPropertiesUtil.KEY_SECRET; String bucketName = ConstantPropertiesUtil.BUCKET_NAME;
原文:https://www.cnblogs.com/konglxblog/p/15077011.html