目录
参数配置化:
- 问题:如果把参数在类中写死的话,需要修改的时候定位起来很麻烦,所以考虑将参数配置化,写在application配置文件中
- 解决:在application配置文件中自定义命名key名称然后再把对应的参数值value写好,配置文件中已经是字符串的格式,所以不需要加双引号
- 使用注解@Value的时候注意是引用Springboot中的,不要使用Lombok中的了。
YML配置文件格式:
虽然配置文件有很多种格式,但是最常用的是yml的格式。通过空格来设置对应的层级。
配置格式:
基础语法:
数据格式:
#yml文件配置
spring:
#数据库连接信息
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/tlias
username: root
password: 1234
#文件上传的配置
servlet:
multipart:
#单个文件最大内存限制
max-file-size: 10MB
#单次请求上传文件总内存限制
max-request-size: 100MB
#Mybatis配置
mybatis:
configuration:
#日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#驼峰映射
map-underscore-to-camel-case: true
#自定义阿里云OSS参数配置化
aliyun:
oss:
endpoint: https://oss-cn-hangzhou.aliyuncs.com
accessKeyId: LTAI4GCH1vX6DKqJWxd6nEuW
accessKeySecret: yBshYweHOpqDuhCArrVHwIiBKpyqSL
bucketName: web-tlias
参数配置化--优化:
使用@ConfigurationProperties注解可以简化@Value注解参数配置
使用@Data注解实现基础方法
使用@Component注解加入到容器中
使用@ConfigurationProperties注解需要指明yml配置文件中参数的共同的前缀"xxx.xxx",会将配置文件中的值注入到对象中。
package com.itheima.utils;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
//配置类
@Data
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class AliOSSProperties {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
}
package com.itheima.utils;
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 {
//不需要再使用Value注解了
// @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参数
//通过注入对象的get方法即可获得参数
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的路径返回
}
}
可选操作:
通过在pom.xml文件中配置对应的依赖,能够识别项目中含有@ConfigurationProperties注解的属性值,能够在yml配置文件中显示自动补全。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
如果参数配置少的话,可以直接通过Value来进行配置即可,如果参数多的话,还是建议使用@ConfigurationProperties进行参数配置化。