自定springBoot starter

共有两个模块:

  • 启动器模块: 只负责导入自动配置模块的依赖
    • 当引用了 启动器的依赖,自动配置的依赖自然也会被导入
  • 自动配置模块: 只引入spring-boot-start模块

测试场景: 定义一个启动器,实现功能, spring-boot 应用启动后,自动将一个服务类HelloService注入到容器中,输入人名后,返回一句打招呼的语句,语句的属性可以通过自主配置改变

  • name-----> “可配置” + name + "可配置"

1.创建启动器模块

pom.xml

 <!--启动器的版本坐标-->
    <groupId>com.mqy</groupId>
    <artifactId>mqy-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

   <!--引入的下面自动配置模块的依赖-->
     <dependencies>
         <dependency>
             <groupId>com.mqy</groupId>
             <artifactId>mqy-spring-boot-starter-autoconfigurer</artifactId>
             <version>0.0.1-SNAPSHOT</version>
         </dependency>
     </dependencies>

2.创建自动配置模块

1.pom.xml

   
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
     <!--自动配置模块的版本坐标-->
    <groupId>com.mqy</groupId>
    <artifactId>mqy-spring-boot-starter-autoconfigurer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mqy-spring-boot-starter-autoconfigurer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!--只引入 spring-boot-starter-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
2.自动配置的属性类

通过读取 application.paroperties/yaml 中以 mqy.hello 为前缀的配置值,注入属性

/**
 * 这是一个自动配置的属性类
 *     里面的属性通过所有前缀为 mqy.hello 的配置注入
 *      在一些类中会使用到这些属性
 */
@ConfigurationProperties(prefix = "mqy.hello")
public class HelloProperties {

    // 要注入的属性

    private String prefix = "";  //hello 招呼的前缀
    private String suffix = "";  //hello 招呼的后缀

    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;
    }
}
3.业务类
/**
 *  提供 say hello 服务的一个类
 *     需要set helloProperties 属性
 */
public class HelloService {

    private 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();
    }
}

4.自动配置类
  • @Configuration:这是一个配置类
  • @ConditionalOnXXX:这个类在什么条件下才能生效
  • @EnableConfigurationProperties(XXXX.class):让指定配置生效,能够被注入到容器中

/**
 *  自动配置类
 *
 */
@Configuration
@ConditionalOnWebApplication //web 应用才生效
@EnableConfigurationProperties(HelloProperties.class)  //使用 配置属性类
public class HelloServiceAutoConfiguration {

    @Autowired
    private HelloProperties helloProperties;

    // 会被放入到 ioc 容器中去
    @Bean
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}

5.编写 META-INF/spring.factories

spring-boot 应用启动后会扫描spring.factories 文件中的org.springframework.boot.autoconfigure.EnableAutoConfiguration,将里里面的@Bean 标注的对象自动天骄到容器中去


# springboot应用启动时,会自动读取 自动配置类
# 并将里面的 @Bean 对象自动创建并放入IOC容器
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.mqy.starter.HelloServiceAutoConfiguration

####3.将两个模块安装到maven 仓库

  • 先安装自动配置模块
  • 再安装starter模块
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CGRN5ISw-1583987917488)(./QQ截图20200312122326.png)]
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8d6w5iWj-1583987917490)(./QQ截图20200312122342.png)]

4.测试

1.导入依赖

注: 前面有设置,自动配置类生效,需要是web应用

 <dependencies>
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--自定义依赖-->
        <dependency>
            <groupId>com.mqy</groupId>
            <artifactId>mqy-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
 </dependencies>
2.application.properties 配置属性值

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ozdbOSL4-1583987917491)(./QQ截图20200312122555.png)]

3. 编写测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    @Autowired
    private HelloService helloService;

    @Test
    public void contextLoads() {
        String hello = helloService.sayHello("lisi");
        System.out.println(hello);
    }
}
4.结果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XqqAGBwn-1583987917492)(./QQ截图20200312122834.png)]

发布了47 篇原创文章 · 获赞 7 · 访问量 2324

猜你喜欢

转载自blog.csdn.net/qq_43616898/article/details/104816356