赞
踩
目录
3.2.5.2 新建aliyun-oss-spring-boot-starter模块
3.5.2.3 新建模块 aliyun-oss-spring-boot-autoconfigure
3.5.2.4 aliyun-oss-spring-boot-starter 引入
3.5.2.5 aliyun-oss-spring-boot-autoconfigure引入依赖
3.5.2.9 新建org.springframework.boot.autoconfigure.AutoConfiguration.imports
前言:SpringBoot配置优先级、Bean管理、SpringBoot原理
- <!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
- <dependency>
- <groupId>org.dom4j</groupId>
- <artifactId>dom4j</artifactId>
- <version>2.1.3</version>
- </dependency>
- package com.runa.config;
-
- import org.dom4j.io.SAXReader;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- @Configuration //配置类
- public class CommonConfig {
-
- //声明第三方Bean
- @Bean // 将当前方法的返回值对象交给IOC容器管理,成为IOC容器的bean
- //通过@bean注解的name/value的属性指定bean名称,如果未指定就是默认方法名
- public SAXReader saxReader(){
- return new SAXReader();
- }
- }
三个常见
删除pom多余内容
删除多余文件
删除多余pom内容和文件
- <dependency>
- <groupId>com.aliyun.oss</groupId>
- <artifactId>aliyun-oss-spring-boot-autoconfigure</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </dependency>
- <!-- 阿里云OSS依赖-->
- <dependency>
- <groupId>com.aliyun.oss</groupId>
- <artifactId>aliyun-sdk-oss</artifactId>
- <version>3.15.1</version>
- </dependency>
- <dependency>
- <groupId>javax.xml.bind</groupId>
- <artifactId>jaxb-api</artifactId>
- <version>2.3.1</version>
- </dependency>
- <dependency>
- <groupId>javax.activation</groupId>
- <artifactId>activation</artifactId>
- <version>1.1.1</version>
- </dependency>
- <!-- no more than 2.3.3-->
- <dependency>
- <groupId>org.glassfish.jaxb</groupId>
- <artifactId>jaxb-runtime</artifactId>
- <version>2.3.3</version>
- </dependency>
- <!-- web 起步依赖 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- package com.aliyun.oss;
-
- import org.springframework.boot.context.properties.EnableConfigurationProperties;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- @Configuration
- @EnableConfigurationProperties(AliOSSProperties.class)
- public class AliOSSAutoConfigure {
-
- @Bean
- public AliOSSUtils aliOSSUtils(AliOSSProperties aliOSSProperties){
- AliOSSUtils aliOSSUtils = new AliOSSUtils();
- aliOSSUtils.setAliOSSProperties(aliOSSProperties);
- return aliOSSUtils;
-
- }
- }
- package com.aliyun.oss;
-
- import org.springframework.web.multipart.MultipartFile;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.UUID;
-
- /**
- * 阿里云 OSS 工具类
- */
-
- public class AliOSSUtils {
-
-
-
- private AliOSSProperties aliOSSProperties;
-
- public AliOSSProperties getAliOSSProperties() {
- return aliOSSProperties;
- }
-
- public void setAliOSSProperties(AliOSSProperties aliOSSProperties) {
- this.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的路径返回
- }
-
- }
- package com.aliyun.oss;
-
-
- import org.springframework.boot.context.properties.ConfigurationProperties;
-
-
- @ConfigurationProperties(prefix = "aliyun.oss")
- public class AliOSSProperties {
-
- private String endpoint;
- private String accessKeyId;
- private String accessKeySecret;
- private String bucketName;
-
- public String getEndpoint() {
- return endpoint;
- }
-
- public void setEndpoint(String endpoint) {
- this.endpoint = endpoint;
- }
-
- public String getAccessKeyId() {
- return accessKeyId;
- }
-
- public void setAccessKeyId(String accessKeyId) {
- this.accessKeyId = accessKeyId;
- }
-
- public String getAccessKeySecret() {
- return accessKeySecret;
- }
-
- public void setAccessKeySecret(String accessKeySecret) {
- this.accessKeySecret = accessKeySecret;
- }
-
- public String getBucketName() {
- return bucketName;
- }
-
- public void setBucketName(String bucketName) {
- this.bucketName = bucketName;
- }
- }
新建文件 文件名:
org.springframework.boot.autoconfigure.AutoConfiguration.imports
输入内容
com.aliyun.oss.AliOSSAutoConfigure
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。