为什么自定义起步依赖?

就像把共同代码抽成一个方法一样,比如对集合需要遍历,求最值等,你可以抽成一个方法,需要时直接调用就行;在项目中,很多技术十分通用,我们就可以自定义起步依赖,我们需要用的时候就可以直接导坐标,这样就很方便;

在SpringBoot项目中就有很多起步依赖,比如下面的:

接下来就直接自定义一个起步依赖

自定义aliyun-oss-spring-boot-starter,完成阿里云OSS操作工具类AliyunOSSUtils 的自动配置

先创建两个项目

第一个:aliyun-oss-spring-boot-starter

创建完这个,不需要的可以删除,大概留这两个就行

接下来创建:aliyun-oss-spring-boot-autoconfigure

步骤也一样,但是这个需要留下的多一些,如下

接下来创建几个类和文件夹

最下面文件夹名字 META-INF\spring\org.springframework.boot.autoconfigure.AutoConfiguration.imports

接下来完成每个文件的编写

AliOSSAutoConfiguration.java

点击查看代码
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 AliOSSAutoConfiguration {
    @Bean
    public AliOSSUtils aliOSSUtils(AliOSSProperties aliOSSProperties){
        AliOSSUtils aliOSSUtils = new AliOSSUtils();
        aliOSSUtils.setAliOSSProperties(aliOSSProperties);
        return aliOSSUtils;

AliOSSProperties.java

点击查看代码
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;

AliOSSUtils.java

点击查看代码
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
public class AliOSSUtils {
    private AliOSSProperties aliOSSProperties;
    public void setAliOSSProperties(AliOSSProperties aliOSSProperties) {
        this.aliOSSProperties = aliOSSProperties;
    public AliOSSProperties getAliOSSProperties() {
        return aliOSSProperties;
     * 实现上传图片到OSS
    public String upload(MultipartFile file) throws IOException {
        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的路径返回

org.springframework.boot.autoconfigure.AutoConfiguration.imports

com.aliyun.oss.AliOSSAutoConfiguration

pom.xml

点击查看代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.13</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-oss-spring-boot-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>

:模块aliyun-oss-spring-boot-starter

只有一个pom.xml配置文件只需要加入一个aliyun-oss-spring-boot-autoconfigure的坐标,如下:

点击查看代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.13</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-oss-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-oss-spring-boot-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

最后你可以在建一个测试模块springboot-autoconfiguration-test 如下:

UploadController.java

点击查看代码
import com.aliyun.oss.AliOSSUtils;
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;
@RestController
public class UploadController {
    @Autowired
    private AliOSSUtils aliOSSUtils;
    @PostMapping("/upload")
    public String upload(MultipartFile image) throws Exception {
        //上传文件到阿里云 OSS
        String url = aliOSSUtils.upload(image);
        return url;

SpringbootAutoconfigurationTestApplication.java 启动类

点击查看代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootAutoconfigurationTestApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootAutoconfigurationTestApplication.class, args);

application.yml 配置文件,配置阿里云oss一些信息

点击查看代码
aliyun:
    endpoint: https://oss-cn-xxxx.aliyuncs.com
    accessKeyId: xxxxxxxxx
    accessKeySecret: xxxxxxxx
    bucketName: xxxxxxx

pom.xml文件 主要主要导一个aliyun-oss-spring-boot-starter坐标

点击查看代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.13</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.it</groupId>
    <artifactId>springboot-autoconfiguration-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-oss-spring-boot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

最后通过apifox或者postman接口测试即可

我使用的是apifox

最后且记一定检查一下自己的pom.xml坐标有没有问题,其次就是一定要把aliyun-oss-spring-boot-autoconfigure和aliyun-oss-spring-boot-starter打jar包安装到本地maven仓库,不打jar包会出现下列问题

Caused by: java.lang.NoClassDefFoundError: Lcom/aliyun/oss/AliOSSUtils;

运行成功后

apifox测试成功后会返回一个路径,如下:

如果运行不了可能就是pom.xml文件有问题