Spring Boot自定义starters

八、Spring Boot自定义starters

**starters:**场景启动器

1. 导入依赖

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

2. 编写自动配置

指定配置类

@Configuration(
        proxyBeanMethods = false
    )

public @interface Configuration {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";

    boolean proxyBeanMethods() default true;
}

在指定条件成立的条件下X自动配置类生效@ConditionalOnXxx

@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})

指定自动配置类的顺序

@AutoConfigureOrder
@AutoConfigureAfter

给容器中添加组件

@Bean

给容器添加组件时绑定相关配置

@ConditionalOnProperty(
    prefix = "spring.mvc.hiddenmethod.filter",
    name = {"enabled"},
    matchIfMissing = false
)


public @interface ConditionalOnProperty {
    String[] value() default {};

    String prefix() default "";

    String[] name() default {};

    String havingValue() default "";

    boolean matchIfMissing() default false;
}

加载自动配置类

将需要启动就加载的自动配置类,配置在META-INF/spring.factories中

image-20201229161739716

如下列配置:

org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration,\

3. 模式

启动器(Starter)

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

命名规范

官方命名空间:

前缀:spring-boot-starter-

模式:spring-boot-starter-模块名

举例:spring-boot-starter-web

自定义命名空间:

后缀:-spring-boot-starter

模式:模块-spring-boot-starter

举例:mybatis-spring-boot-starter

模式

image-20201229162643709

自定义xxxx-starter,自动配置应该再编写xxxx-starter-autoconfigurer

4. 创建空项目

image-20201229163302060 image-20201229163424916

file - > Project Settings - > Modules

image-20201229163621445

创建新工程,创建Maven工程,用于做启动器

image-20201229163814841

自定义Starter名

image-20201229164033139

利用Spring Boot初始化器创建新模块,用作自动配置

image-20201229164242865 image-20201229164400514

5. 自定义Starter

创建的目录结构

image-20201229174115394

5.1 why-spring-boot-starter设置

引入why-spring-boot-autoconfig中的groupId、artifactId和version

<!--    启动器-->
    <dependencies>
<!--        引入自动配置模块-->
        <dependency>
            <groupId>com.why.starter</groupId>
            <artifactId>why-spring-boot-starter-autoconfig</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
5.2 why-spring-boot-autoconfig中的设置

删除并新建相关文件或文件夹

目录结构:

image-20201229174512838

pom.xml文件中删除多余依赖

保留的依赖:

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

创建HelloService服务

public class HelloService {

    @Autowired
    HelloProperties helloProperties;

    public String sayHelloWhy(String name){
        return helloProperties.getPrefix()+"-"+name + helloProperties.getSuffix();
    }
}

创建属性类

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

/**
 * @Description TODO 属性类
 * @Author why
 * @Date 2020/12/29 16:51
 * Version 1.0
 **/
@ConfigurationProperties(prefix = "why.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;
    }
}

创建自动配置类

@Configuration
@ConditionalOnMissingBean(HelloService.class)//容器中没有HelloService时才生效
@EnableConfigurationProperties(HelloProperties.class)//让配置类生效,开启属性文件绑定功能,默认放在容器中
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

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

创建配置factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.why.starter.HelloServiceAutoConfiguration
5.3 将自动配置模块和启动器模块导入Maven仓库

将自动配置类导入Maven仓库

按住ctrl键同时 选中clean和install运行

image-20201229175108523

将Starter模块导入Maven仓库

操作同上

image-20201229175243647

引用自定义的Starter

pom.xml文件中添加依赖

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

创建测试实例

RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping("/starter/hello")
    public String sayHello(){
        String wyy = helloService.sayHelloWhy("wyy");
        return wyy;
    }
}

配置文件中配置前后缀

why.hello.prefix="why"
why.hello.suffix="666"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值