幽兰生空谷
--绝世独自开

SpringBoot快速入门(十)-提高:自定义starter

提高:自定义starter

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

说明

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

命名归约:

官方命名:

  • 前缀: spring-boot-starter-xxx
  • 比如:spring-boot-starter-web….

自定义命名:

  • xxx-spring-boot-starter
  • 比如:mybatis-spring-boot-starter 

编写启动器

1、在IDEA中新建一个空项目 spring-boot-starter-diy

2、新建一个普通Maven模块:itheibai-spring-boot-starter

3、新建一个Springboot模块:itheibai-spring-boot-starter-autoconfifigure 

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

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

<dependencies>
    <dependency>
        <groupId>com.itheibai</groupId>
        <artifactId>itheibai-spring-boot-starter-autoconfigure</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

6、将 autoconfifigure 项目下多余的文件都删掉,Pom中只留下一个 starter,这是所有的启动器基本配置,(test文件夹也删掉,这里忘记删了,包括下面的截图)

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

package com.itheibai;
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 配置类 

package com.itheibai;
import org.springframework.boot.context.properties.ConfigurationProperties;
//前缀 itheibai.hello
@ConfigurationProperties(prefix = "itheibai.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,测试! 

package com.itheibai;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnWebApplication
@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.itheibai.HelloServiceAutoConfiguration

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

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

1、新建一个SpringBoot 项目

2、导入我们自己写的启动器

<dependency>
    <groupId>com.itheibai</groupId>
    <artifactId>itheibai-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

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

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;
    @RequestMapping("/hello")
    public String hello(){
        return helloService.sayHello("itheibai");
    }
}

4、编写配置文件 application.properties 

itheibai.hello.prefix="www.
itheibai.hello.suffix=.com"

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

赞(3) 打赏
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《SpringBoot快速入门(十)-提高:自定义starter》
文章链接:https://www.itheibai.com/archives/572
免责声明:根据《计算机软件保护条例》第十七条规定“为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。”您需知晓本站所有内容资源均来源于网络,仅供用户交流学习与研究使用,版权归属原版权方所有,版权争议与本站无关,用户本人下载后不能用作商业或非法用途,需在24个小时之内从您的电脑中彻底删除上述内容,否则后果均由用户承担责任;如果您访问和下载此文件,表示您同意只将此文件用于参考、学习而非其他用途,否则一切后果请您自行承担,如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
本站是非经营性个人站点,所有软件信息均来自网络,所有资源仅供学习参考研究目的,并不贩卖软件,不存在任何商业目的及用途,网站会员捐赠是您喜欢本站而产生的赞助支持行为,仅为维持服务器的开支与维护,全凭自愿无任何强求。

评论 抢沙发

评论前必须登录!

 

养成“打赏”的好习惯,从我做起!

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

登录

找回密码

注册