Springboot自定义Stater

1、默认启动器

Boot会将项目中常用的场景做成对应的starter启动器,项目中涉及到什么场景就引入该场景对应的启动器,项目中引入这些启动器之后,和这个starter相关的依赖也会被引入。比如引入测试和基本的启动器

 再如这里引入web开发的启动器

其他stater查看官方文档

 

 

 

2、自定义启动器

如上,boot已经提供了好多常用的starter场景,即使这样有时候我们需要将自己一个公用的业务模块抽成一个starter,需要用到的地方直接使用starter即可,比如分析

分析官网starter定义方式,如引入测试starter

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
</dependency>

分析可以看出如下,starter没有任何实现,只是依赖管理,底层的依赖是一个自动配置类和其他依赖

底层依赖有个spring-boot-test-autoconfigure依赖用于和boot配置

 

按照上面的模式,定义一个starter步骤如下

  • 启动器(starter)是一个空的jar文件,仅仅提供辅助性依赖管理,这些依赖可能用于自动装配或其他类库。
  • 需要专门写一个类似spring-boot-autoconfigure的配置模块
  • 用的时候只需要引入启动器starter,就可以使用自动配置了

SpringBoot提供的starter以spring-boot-starter-xxx的方式命名的。官方建议自定义的starter使用xxx-spring-boot-starter命名规则。以区分SpringBoot生态提供的starter。

我们定义hello-spring-boot-starter,创建一个starter,是一个空的jar项目
 

 

再创建一个自动配置项目hello-spring-boot-starter-autoconfig

 

依赖 

<?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.5.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>hello-spring-boot-starter-autoconfig</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello-spring-boot-starter-autoconfig</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</artifactId>
        </dependency>
    </dependencies>
 
 
</project>

自动配置类

/**
 * 当前类是一个自动配置类
 * 只有当容器中没有hello组件的时候才会注入
 * 和配置文件绑定
 */
@Configuration
@ConditionalOnMissingBean({Hello.class})
@EnableConfigurationProperties({HelloProperties.class})
public class HelloAutoConfiguration {
    @Bean
    public Hello hello() {
        Hello hello = new Hello();
        return hello;
    }
}

和自动配置类绑定的配置文件类

@ConfigurationProperties("hello")
public class HelloProperties {
    private String pre;
    private String end;
 
    public String getPre() {
        return pre;
    }
 
    public void setPre(String pre) {
        this.pre = pre;
    }
 
    public String getEnd() {
        return end;
    }
 
    public void setEnd(String end) {
        this.end = end;
    }
}

主业务类

/**
 * 默认不需要自动放入容器中
 */
public class Hello {
 
    @Autowired
    HelloProperties helloProperties;
 
    public String sysHello(String userName){
        return helloProperties.getPre()+userName+helloProperties.getEnd();
    }
}

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.auto.HelloAutoConfiguration

最后将这两个项目install,然后再其他工程引入starter即可

<dependency>
  <groupId>org.example</groupId>
  <artifactId>hello-spring-boot-starter</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

且可以配置文件中配置

hello.pre=zhangsan
hello.end=你好
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序三两行

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

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

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

打赏作者

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

抵扣说明:

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

余额充值