当前位置:   article > 正文

Spring Boot 自定义starter_aliyun-oss-spring-boot-starter

aliyun-oss-spring-boot-starter

Spring Boot 自定义starter

一、starter 加载原理

springboot通过一个@SpringBootApplication注解启动项目,springboot在项目启动的时候,会将项目中所有声明为Bean对象(注解、xml)的实例信息全部加载到ioc容器当中。 除此之外也会将所有依赖到的starter里的bean信息加载到ioc容器中,从而做到所谓的零配置,开箱即用。

二、自定义starter

这里用aliyunoss 自定义一个starter

  • 思路(一):

创建 aliyun-oss-spring-boot-autoconfigure项目
在pom中引入自己运行时所需要的依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.7.5</version>
        </dependency>
<!--        aliyun -  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>
    </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

创建三个类:AliyunOssAutoConfigure、AliyunOssProperties、AliyunOssUtils

 //	 AliyunOssAutoConfigure类
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @aliyunOssAutoConfigure
 * @Description TODO
 * @Author Wanp
 * @Date 2023/4/14 19:38
 * @Version 1.0
 */
@Configuration
@EnableConfigurationProperties(AliyunOssProperties.class)
public class AliyunOssAutoConfigure {

    @Bean
    public AliyunOssUtils aliyunOssUtils(AliyunOssProperties aliyunOssProperties) {
        return new AliyunOssUtils(aliyunOssProperties);
    }
}


//		AliyunOssProperties 类
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @aliyunOssProperties
 * @Description TODO
 * @Author Wanp
 * @Date 2023/4/14 19:38
 * @Version 1.0
 */
@ConfigurationProperties(prefix = AliyunOssProperties.ALIYUN_OSS_PREFIX )
public class AliyunOssProperties {
    public static final String ALIYUN_OSS_PREFIX = "aliyun.oss";

    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;
    }
}


//		AliyunOssUtils
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

/**
 * @AliyunOssUtils
 * @Description TODO
 * @Author Wanp
 * @Date 2023/4/14 19:41
 * @Version 1.0
 */
public class AliyunOssUtils {

    public AliyunOssUtils(AliyunOssProperties aliyunOssProperties) {
        this.aliyunOssProperties = aliyunOssProperties;
    }
    private AliyunOssProperties aliyunOssProperties;

    public  String uploadImage(MultipartFile file) throws Exception {
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build("https://"+aliyunOssProperties.getEndpoint(),
                aliyunOssProperties.getAccessKeyId(), aliyunOssProperties.getAccessKeySecret());
        InputStream inputStream = file.getInputStream();
        // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String suffixUrl = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String filename = uuid + suffixUrl;
        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(aliyunOssProperties.getBucketName(), filename, inputStream);
        // 设置该属性可以返回response。如果不设置,则返回的response为空。
        putObjectRequest.setProcess("true");
        // 上传文件。
        PutObjectResult result = ossClient.putObject(putObjectRequest);
        if (result.getResponse().getStatusCode() == 200) {
            return "https://" + aliyunOssProperties.getBucketName() + "." + aliyunOssProperties.getEndpoint() + "/" + filename;
        }

        if (ossClient != null) {
            ossClient.shutdown();
        }
        return null;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125

完成后 下载到自己的maven仓库

在这里插入图片描述

  • 思路(二)

创建 aliyun-oss-boot-starter 项目
在pom中引入自己运行时所需要的依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        // 这个是 aliyun-oss-spring-boot-autoconfigure 下载到本地的坐标
        <dependency>
            <groupId>com.pw</groupId>
            <artifactId>aliyun-oss-spring-boot-autoconfigure</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

最后创建一个测试项目
pom 引入 aliyun-oss-boot-starter坐标

<dependencies>
        <dependency>
            <groupId>com.pw</groupId>
            <artifactId>aliyun-oss-spring-boot-starter</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

yml文件 配置连接信息

aliyun:
  oss:
    endpoint: oss-cn-ncs.comxxxxxxxxx
    accessKeyId: LTAI5tBUtqcs3xxxxxxx
    accessKeySecret: UHRdyjXYxxxxx
    bucketName: w-190xxxx
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
// 编写controller
import com.pw.autoconfigure.AliyunOssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @AliyunController
 * @Description TODO
 * @Author Wanp
 * @Date 2023/4/14 19:56
 * @Version 1.0
 */
@RestController
public class AliyunController {

    @Autowired
    private AliyunOssUtils aliyunOSSUtiles;
    @PostMapping("/upload")
    public String upload(MultipartFile file) {
        try {
            String url = aliyunOSSUtiles.uploadImage(file);
            return url;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/762379
推荐阅读
相关标签
  

闽ICP备14008679号