SpringBoot系列_04_SpringBoot自定义启动器

命名规范

官方命名规范

  • 模式:spring-boot-starter-模块名
  • 举例:spring-boot-starter-web、spring-boot-starter-jdbc

自定义命名规范

  • 模式:模块-spring-boot-starter
  • 举例:mybatis-spring-boot-starter

实例

项目

父项目:hello-spring-boot-starter-parent

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.hehe.springboot</groupId>
    <artifactId>hello-spring-boot-starter-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>hello-spring-boot-starter</module>
        <module>hello-spring-boot-autoconfigurer</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.4.5</version>
        </dependency>
    </dependencies>

</project>

子项目:hello-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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>hello-spring-boot-starter-parent</artifactId>
        <groupId>cn.hehe.springboot</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>hello-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <dependencies>
        <dependency>
            <groupId>cn.hehe.springboot</groupId>
            <artifactId>hello-spring-boot-autoconfigurer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!--如果当前starter 还需要其他的类库就在这里引用-->
    </dependencies>
</project>

子项目:hello-spring-boot-autoconfigurer

<?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.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.hehe.springboot</groupId>
    <artifactId>hello-spring-boot-autoconfigurer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello-spring-boot-autoconfigurer</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--导入配置文件处理器,配置文件进行绑定就会有提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!--安装到仓库后,引用时不出现 BOOT-INF文件夹(会导致找不到相关类)-->
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

在这里插入图片描述

属性类

package cn.hehe.springboot.hellospringbootautoconfigurer.config;

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

/**
 * @program: hello-springbootstarterparent
 * @description: 属性类
 * @author: hehe
 * @create: 2021-04-16 16:03
 **/
@ConfigurationProperties("hello")
public class HelloProperties {

    private String name;

    public String getName() {
        return name;
    }

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

控制类

package cn.hehe.springboot.hellospringbootautoconfigurer.controller;

import cn.hehe.springboot.hellospringbootautoconfigurer.config.HelloProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: hello-springbootstarterparent
 * @description: hello控制类
 * @author: hehe
 * @create: 2021-04-16 16:05
 **/
@RestController
public class HelloController {

    private HelloProperties helloProperties;

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

    @GetMapping("hello")
    public String hello(){
        return helloProperties.getName()+"欢迎您!";
    }

}

自动配置类

package cn.hehe.springboot.hellospringbootautoconfigurer.config;

import cn.hehe.springboot.hellospringbootautoconfigurer.controller.HelloController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: hello-springbootstarterparent
 * @description: Hello自动配置类
 * @author: hehe
 * @create: 2021-04-16 16:08
 **/
@Configuration
@ConditionalOnProperty(value="hello.name")
@EnableConfigurationProperties({HelloProperties.class})
public class HelloAutoConfitguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloController helloController(HelloProperties helloProperties){
        return new HelloController(helloProperties);
    }

}

spring.factories

在这里插入图片描述

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.hehe.springboot.hellospringbootautoconfigurer.config.HelloAutoConfitguration

安装starter

在这里插入图片描述

引用测试

①引用项目

<dependency>
    <groupId>cn.hehe.springboot</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

②配置属性
在这里插入图片描述
③访问地址:http://localhost:8080/hello
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值