SpringBoot核心原理---自动配置 之创建自己的starter pom maven依赖包

上一篇:SpringBoot 的运行原理之自动配置,了解SpringBoot自动配置的原理,在此基础上动手做一个自己的自动配置依赖包。


一、准备工作

先创建Maven工程:
这里写图片描述
目录结构:
这里写图片描述


二、编码

MistraService.java
这个类就是要自动配置的Bean,条件就设为是否存在这个类的Bean,没有的话就创建。

package com.mistra.mistrastarter;

/**
 * Author: RoronoaZoro丶WangRui
 * Time: 2018/6/25/025
 * Describe: 是否进行自动配置的判断依据类,根据此类是否存在来决定是否创建这个类的Bean
 */
public class MistraService {

    private String name;

    public String sayYourName(){
        return "I'm " + name + "! ";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

MistraServiceProperties .java
为MistraService提供配置参数值得类。name设有默认值,配置文件配置前缀是”mistra”—>”mistra.name”,当没有在配置文件配置时就取默认值。

package com.mistra.mistrastarter;

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

/**
 * Author: RoronoaZoro丶WangRui
 * Time: 2018/6/25/025
 * Describe: 自动配置的类型安全的注入参数类
 * 通过application.properties配置mistra.name的值来设置参数,若不设置,默认为"RoronoaZoro丶小王瑞"
 */
@ConfigurationProperties(prefix = "mistra")
public class MistraServiceProperties {

    private static final String NAME = "RoronoaZoro丶小王瑞";

    private String name = NAME;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

MistraServiceAutoConfiguration.java
自动配置类。

package com.mistra.mistrastarter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Author: RoronoaZoro丶WangRui
 * Time: 2018/6/25/025
 * Describe: 自动配置类
 * 根据条件判断是否要自动配置,创建Bean
 */
@Configuration
@EnableConfigurationProperties(MistraServiceProperties.class)
@ConditionalOnClass(MistraService.class)//判断MistraService这个类在类路径中是否存在
@ConditionalOnProperty(prefix = "mistra",value = "enabled",matchIfMissing = true)
public class MistraServiceAutoConfiguration {

    @Autowired
    private MistraServiceProperties mistraServiceProperties;

    @Bean(name = "mistraService")
    @ConditionalOnMissingBean(MistraService.class)//当容器中没有这个Bean时(MistraService)就自动配置这个Bean,Bean的参数来自于MistraServiceProperties
    public MistraService mistraService(){
        MistraService mistraService = new MistraService();
        mistraService.setName(mistraServiceProperties.getName());
        return mistraService;
    }
}

根据MistraServiceProperties提供的参数,判断MistraService这个类在类路径中是否存在,并且当容器中没有这个Bean(MistraService )时就自动配置创建这个Bean。
spring.factories
注册自动配置类。注意位置:src.main.resources.META-INF.spring.factories
这里写图片描述


三、maven打包

然后打包,并注册到本地maven仓库:
CMD 切换到项目根目录下:mvn install
这里写图片描述

然后在本地仓库就可以看见依赖包了:
这里写图片描述


四、引用

在别的项目引入依赖:

<dependency>
    <groupId>com.mistra</groupId>
    <artifactId>mistrastarter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

刷新Maven后可以在依赖里面找到自定义的包:
这里写图片描述
SpringBoot启动类TestApplication.java

@SpringBootApplication(scanBasePackages = "com.spring.boot.test")
public class TestApplication {

    public static void main(String[] args) {
        System.out.println("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆   RoronoaZoro丶小王瑞   ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆");
        SpringApplication.run(TestApplication.class, args);
    }

在配置文件配置debug:true,可以看到自动配置的情况,可以找到自己写的配置类:
这里写图片描述

单元测试类StarterPomTest.java

package com.spring.boot.test.starter;

import com.mistra.mistrastarter.MistraService;
import com.spring.boot.test.TestApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Author: RoronoaZoro丶WangRui
 * Time: 2018/6/26/026
 * Describe: 自定义starter测试
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class StarterPomTest {

    @Autowired
    private MistraService mistraService;

    @Test
    public void say() {
        System.out.println(mistraService.sayYourName());
    }
}

在配置文件中加入参数:

mistra:
  name: 罗罗诺亚索隆

如果不配置,就是取类中定义的默认值。
运行say():
这里写图片描述

至此,自定义的starter测试就完成了,也是对SpringBoot的核心自动配置的一次理解。
Demo源码GitHub


这里写图片描述

猜你喜欢

转载自blog.csdn.net/Axela30W/article/details/80809311