问题分析
问题解决:
参数配置化
application.properties文件
#阿里云OSS配置
aliyun.oss.endpoint=https://oss-cn-hangzhou.aliyuncs.com
aliyun.oss.accessKeyId=LTAI4GCH1vX6DKqJWxd6nEuW
aliyun.oss.accessKeySecret=yBshYweHOpqDuhCArrVHwIiBKpyqSL
aliyun.oss.bucketName=web-tlias
注入:
@Value注解通常用于外部配置的属性注入,具体用法为:@Value("${配置文件中的key}")
@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;
/**
* 实现上传图片到OSS
*/
public String upload(MultipartFile file) throws IOException {
// 获取上传的文件的输入流
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的路径返回
}
}
yml配置文件
配置格式
SpringBoot提供了多种属性配置方式
常见配置文件格式对比
yml基本语法
大小写敏感
数值前边必须有空格,作为分隔符
使用缩进表示层级关系,缩进时,不允许使用Tab键,只能用空格 (idea中会自动将Tab转换为空格)
缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
#表示注释,从这个字符一直到行尾,都会被解析器忽略
yml数据格式
对象/Map集合:
#定义对象/Map集合 格式: name:空格属性
user:
name: Tom
age: 18
gender: boy
数组/List/Set集合:
#数组/List/Set集合 格式: -空格属性
hobby:
- java
- game
- C++
yml配置
在application.yml中的配置案例相关的配置项
spring:
datasource:
# 驱动类名称
driver-class-name: com.mysql.cj.jdbc.Driver
# 数据库连接的URL
url: jdbc:mysql://localhost:3306/mybatis
# 连接数据库的用户名
username: root
# 连接数据库的密码
password: 123456
servlet:
multipart:
#配置单个文件上传大小限制
max-file-size: 10MB
#配置单个请求最大大小的限制(一次请求中可以上传多个文件)
max-request-size: 100MB
mybatis:
configuration:
# 配置MyBatis的日志,指定输出到控制台
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#开启mybatis的驼峰命名自动映射开关
map-underscore-to-camel-case: true
#阿里云OSS配置
aliyun:
oss:
endpoint: https://oss-cn-hangzhou.aliyuncs.com
accessKeyId: LTAI4GCH1vX6DKqJWxd6nEuW
accessKeySecret: yBshYweHOpqDuhCArrVHwIiBKpyqSL
bucketName: web-tlias
#定义对象/Map集合 格式: name:空格属性
user:
name: Tom
age: 18
gender: boy
#数组/List/Set集合 格式: -空格属性
hobby:
- java
- game
- C++
@ConfigurationProperties
问题分析
解决方案
1.创建AliOSSProperties类,通过@ConfigurationProperties将外部配置 application.yml
中的属性绑定到 Java Bean 的注解
@Data
@Component //交给IOC容器管理
@ConfigurationProperties(prefix = "aliyun.oss")
public class AliOSSProperties {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
}
2.然后在AliOSSUtils类中通过@Autowired注入AliOSSProperties的实例对象
3.通过
String endpoint = aliOSSProperties.getEndpoint();
String accessKeyId = aliOSSProperties.getAccessKeyId();
String accessKeySecret = aliOSSProperties.getAccessKeySecret();
String bucketName = aliOSSProperties.getBucketName();
获取阿里云OSS参数
4.代码实现
/**
* 阿里云 OSS 工具类
*/
@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;
@Autowired
private AliOSSProperties aliOSSProperties;
/**
* 实现上传图片到OSS
*/
public String upload(MultipartFile file) throws IOException {
//获取阿里云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的路径返回
}
}
如果有警告可添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
并且在配置的时候也会有提示:
@ConfigurationProperties 与 @Value
相同点
都是用来注入外部配置的属性的
不同点
@Value注解只能一个一个的进行外部属性的注入。
@ConfigurationProperties可以批量的将外部的属性配置注入到bean对象的属性中。