自定义springboot-starter

Spring Boot 最强大的功能就是把我们常用的场景抽取成了一个个Starter(场景启动器),我们通过引入Spring Boot为我提供的这些场景启动器,我们再进行少量的配置就能使用相应的功能。即使是这样,SpringBoot也不能囊括我们所有的使用场景,往往我们需要自定义Starter,来简化我们对Spring Boot的使用。

项目截图

在这里插入图片描述

项目源码代码量很少,浪费积分,建议跟着下方流程搭建一遍。~~~~~~

自定义springboot-starter项目源码下载

===================================================================

项目搭建流程

在这里插入图片描述

1. 新建maven项目custome-starter

新建项目,不做说明,自己新建即可
groupId : com.custome.starter
artifactId : custome-starter

在这里插入图片描述

2. 新建module:hello-spring-boot-starter-autoconfigure

新建module不做说明,自己新建即可
artifactId: hello-spring-boot-starter-autoconfigure

2.1 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>custome-starter</artifactId>
        <groupId>com.custome.starter</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>hello-spring-boot-starter-autoconfigure</artifactId>
    
    <properties>
        <maven.test.skip>true</maven.test.skip>
        <maven.javadoc.skip>true</maven.javadoc.skip>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring.boot.version>2.1.0.RELEASE</spring.boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.boot.version}</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>${spring.boot.version}</version>
            <optional>true</optional>
        </dependency>
        <!-- 依据需要可自行添加需要的依赖包 -->
    </dependencies>

</project>

2.2 HelloProperties.java

package com.hello.starter.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "hello.starter")
public class HelloProperties {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

2.3 IndexController.java

package com.hello.starter.controller;

import com.hello.starter.properties.HelloProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {

    private HelloProperties helloProperties;

    public IndexController(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    @GetMapping(value = "/hello-starter")
    public String index() {
        return helloProperties.getName() + " - 自定义starter!";
    }

}

2.4 HelloAutoConfitguration.java

package com.hello.starter.properties;

import com.hello.starter.controller.IndexController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 此注解信息不做说明,不清楚的可自行官网查询
 */
@Configuration
@ConfigurationProperties(value = "hello.starter.name")
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfitguration {
    @Autowired
    private HelloProperties helloProperties;

    @Bean
    public IndexController indexController() {
        return new IndexController(helloProperties);
    }

}

3. 新建module:hello-spring-boot-starter

新建module不做说明,自己新建即可
artifactId: hello-spring-boot-starter

3.1 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>custome-starter</artifactId>
        <groupId>com.custome.starter</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>hello-spring-boot-starter</artifactId>

    <dependencies>
        <!-- 引入 hello-spring-boot-starter-autoconfigure -->
        <dependency>
            <groupId>com.custome.starter</groupId>
            <artifactId>hello-spring-boot-starter-autoconfigure</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

</project>

3.2 spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.hello.starter.properties.HelloAutoConfitguration

4.打包项目

mvn clean compile install

5.其它项目引入

5.1 pom.xml

<dependency>
     <groupId>com.custome.starter</groupId>
     <artifactId>hello-spring-boot-starter</artifactId>
     <version>1.0</version>
</dependency>

5.2 properties配置文件增加配置

hello.starter.name=xxx

6.运行项目并进行访问

http://localhost:port/hello-starter
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小安灬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值