1. 把云服务的信息写入配置文件
- 由于之后项目开发会有大量的java文件在项目里面,所以把密码等信息写入
application.properties
配置文件方便管理
- 配置文件:
#ali云OSS配置
aliyun.oss.endpoint=https://oss-cn-chengdu.aliyuncs.com
aliyun.oss.accessKeyId=accessKeyId
aliyun.oss.accessKeySecret=accessKeySecret
aliyun.oss.bucketName=mannor-tlias
- 工具类
AliOSSUtils
:通过@Value
注解的方式来将application.properties
配置文件中的值进行传输
@Component
public class AliOSSUtils {
@Value("${aliyun.oss.endpoint}")
private String endpoint ;
@Value("${aliyun.oss.accessKeyId}")
private String accessKeyId ;
@Value("${aliyun.oss.accessKeySecret}")
private String accessKeySecret ;
@Value("${aliyun.oss.bucketName}")
private String bucketName ;
}
}
2.配置格式
XML
(臃肿)
<server>
<port>8080</port>
<address>127.0.0.1</address>
</server>
application.properties
(层次结构不清晰,不适合框架开发)
server.port=8080
server.address=127.0.0.1
application.yml
(.yml和.yaml简洁,以数据为中心)
server:
port: 8080
address: 127.0.0.1
application.yaml
(与.yml用法相同)
server:
port: 8080
address: 127.0.0.1
3.yml文件
1. 语法:
- 大小写敏感
- 数值前边必须有空格,作为分隔符
- 使用缩进表示层级关系,缩进时,不允许使用Tab键,只能用空格(idea中会自动将Tab转换为空格)
- 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
- #表示注释,从这个字符一直到行尾,都会被解析器忽略
2. 数据格式
- 对象/Map集合:
name:
zhangsanage: 18
password: 123456
- 数组/List/Set集合:
hobby:
- java
- game
- sport
4.@ConfigurationProperties注解
- 由于工具类
AliOSSUtils
通过@Value
注解的方式来将application.properties
配置文件中的值进行传输时可能会造成臃肿、如有大量的值需要传输时就会显得繁琐,所以开发次注解。 - 如使用
@ConfigurationProperties
注解需要将之前传递阿里云参数的代码进行修改:
application.properties
还是原来的方式:
#阿里云OSS
aliyun:
oss:
endpoint: https://oss-cn-chengdu.aliyuncs.com
accessKeyId: LTAI5tPbHxBQmNEkP6mqdcCM
accessKeySecret: WSX0EkD94IYMOguJGhMk1PpkaqPmGV
bucketName: mannor-tlias
AliOSSUtils
工具类同一目录下新增AliOSSProperties
的Java类:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data //需要提供getter,setter等方法
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class AliOSSProperties {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
}
- 在
AliOSSUtils
工具类中使用@Autowired
注入AliOSSProperties
对象,并且进行调用:
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.UUID;
/**
* 云 OSS 工具类
*/
@Component
public class AliOSSUtils {
@Autowired
private AliOSSProperties aliOSSProperties;
/**
* 实现上传图片到OSS
*/
public String upload(MultipartFile file) throws IOException {
//从AliOSSProperties对象中获取oss的参数:
String endpoint= aliOSSProperties.getEndpoint();
String accessKeyId= aliOSSProperties.getAccessKeyId();
String accessKeySecret= aliOSSProperties.getAccessKeySecret();
String bucketName= aliOSSProperties.getBucketName();
// 获取上传的文件的输入流
InputStream inputStream = file.getInputStream();
// 避免文件覆盖
String originalFilename = file.getOriginalFilename();
String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
//上传文件到 OSS
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
ossClient.putObject(bucketName, fileName, inputStream);
//文件访问路径
String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
// 关闭ossClient
ossClient.shutdown();
return url;// 把上传到oss的路径返回
}
}
- 此外,还需要引入一个依赖,此依赖的作用是自动识别被
@ConfigurationProperties
标识的bean对象,在properities
或者yml
文件中配置时会有提示相关的配置项,从而方便开发。
- 但是此步骤并不会对程序有任何的影响,只是便于开发。(配置完之后需要重新启动)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
5.@ConfigurationProperties 与@Value的区别
- 相同点:都是用来注入外部配置的属性的。
- 不同点:
@Value
注解只能单个的进行外部属性的注入。ConfigurationProperties
可以多个的将外部的属性配置注入到bean
对象的属性中。