010SpringBoot--自定义starter

目录

说明

命名归约:

编写启动器

1、在IDEA中新建一个空项目 

2、新建一个普通Maven模块:008springboot-starter

3、新建一个Springboot模块:008springboot-starter-autoconfigure

4、点击apply即可,基本结构

5、在我们的 starter 中 导入  autoconfigure 的依赖!

6、将 autoconfigure 项目下多余的文件都删掉,Pom中只留下一个 starter,这是所有的启动器基本配置!

7、我们编写一个自己的服务

8、编写HelloProperties 配置类

9、编写我们的自动配置类并注入bean,测试!

10、在resources编写一个自己的 META-INF\spring.factories

11、编写完成后,可以安装到maven仓库中!

新建项目测试我们自己写的启动器

1、新建一个SpringBoot 项目

2、导入我们自己写的启动器 008springboot-starter-test

​ ​​​​​​​3、编写一个 HelloController  进行测试我们自己的写的接口!

4、编写配置文件 application.properties​​​​​​​

5、启动项目进行测试,结果成功 !


​​​​​​​

我们分析完毕了源码以及自动装配的过程,我们可以尝试自定义一个启动器来玩玩!

说明

启动器模块是一个 空 jar 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库;

命名归约:

官方命名:

  • 前缀:spring-boot-starter-xxx

  • 比如:spring-boot-starter-web....

自定义命名:

  • xxx-spring-boot-starter

  • 比如:mybatis-spring-boot-starter

编写启动器

1、在IDEA中新建一个空项目 

2、新建一个普通Maven模块:008springboot-starter

3、新建一个Springboot模块:008springboot-starter-autoconfigure

4、点击apply即可,基本结构

5、在我们的 starter 中 导入  autoconfigure 的依赖!

    <!-- 启动器 -->
    <dependencies>
        <!--  引入自动配置模块 -->
        <dependency>
            <groupId>com.gh</groupId>
            <artifactId>008springboot-starter-autoconfigure</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>

6、将 autoconfigure 项目下多余的文件都删掉,Pom中只留下一个 starter,这是所有的启动器基本配置!

7、我们编写一个自己的服务

/**
 * @author gh  Email:@2495140780qq.com
 * @Description
 * @date 2022-02-11-上午 10:38
 */
public class HelloService {

    HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    public String sayHello(String name){
        return helloProperties.getPrefix() + name + helloProperties.getSuffix();
    }

}

8、编写HelloProperties 配置类

/**
 * @author gh  Email:@2495140780qq.com
 * @Description
 * @date 2022-02-11-上午 10:38
 */
// 前缀 kuang.hello
@ConfigurationProperties(prefix = "kuang.hello")
public class HelloProperties {

    private String prefix;
    private String suffix;

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

9、编写我们的自动配置类并注入bean,测试!

/**
 * @author gh  Email:@2495140780qq.com
 * @Description
 * @date 2022-02-11-上午 10:39
 */
@Configuration
@ConditionalOnWebApplication //web应用生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService(){
        HelloService service = new HelloService();
        service.setHelloProperties(helloProperties);
        return service;
    }

}

10、在resources编写一个自己的 META-INF\spring.factories

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.gh.HelloServiceAutoConfiguration

11、编写完成后,可以安装到maven仓库中!

新建项目测试我们自己写的启动器

1、新建一个SpringBoot 项目

2、导入我们自己写的启动器​​​​​​​ 008springboot-starter-test

        <dependency>
            <groupId>com.gh</groupId>
            <artifactId>008springboot-starter</artifactId>
            <version>1.0.0</version>
        </dependency>

 ​​​​​​​3、编写一个 HelloController  进行测试我们自己的写的接口!

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @RequestMapping("/hello")
    public String hello() {
        return helloService.sayHello("zxc");
    }

}

4、编写配置文件 application.properties​​​​​​​

kuang.hello.prefix="ppp"kuang.hello.suffix="sss"

5、启动项目进行测试,结果成功 !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

gh-xiaohe

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

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

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

打赏作者

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

抵扣说明:

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

余额充值