Spring Boot 動手寫一個 Start

我們在使用SpringBoot 項目時,引入一個springboot start依賴,只需要很少的代碼,或者不用任何代碼就能直接使用默認配置,再也不用那些繁瑣的配置了,感覺特別神奇。我們自己也動手寫一個start.

一、新建一個 Start 的 Maven 項目

  1. pom 文件如下
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.1.0.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.6</version>
        <optional>true</optional>
        <scope>provided</scope>
    </dependency>

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

</dependencies>
  • spring-boot-autoconfigure springboot 自動配置的核心依賴
  • spring-boot-starter-test 測試包
  • lombok 省去 getter/setter 等簡化代碼
  1. 演示代碼

DemoService:

public interface DemoService {

    String getMessage();

    Integer getCode();
}

DemoServiceImpl:

public class DemoServiceImpl implements DemoService {

    @Override
    public String getMessage() {
        return "Hello!";
    }

    @Override
    public Integer getCode() {
        return 123;
    }
}

DemoAutoConfiguration:

@Configuration
public class DemoAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(DemoService.class)
    public DemoService demoService() {
        return new DemoServiceImpl();
    }
}
  • @Configuration 標注該類為一個配置類
  • ConditionalOnMissingBean(DemoService.class) 條件注解

spingboot 的自動注解主要還是用這些條件注解來實現(xiàn)的。請查看之前的文章:

Spring Boot 自動配置之條件注解

Spring Boot 自動配置之@Enable*與@Import注解

Spring Boot 自動配置之@EnableAutoConfiguration

  1. 讓springboot 識別自動自動配置的代碼

需要在resources下新建文件META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.jiuxian.DemoAutoConfiguration

SpringBoot 中的注解 @EnableAutoConfiguration 在項目啟動的時候會通過 SpringFactoriesLoader.loadFactoryNames 方法獲取 spring.factories 文件下的配置類

  1. 測試類
  • 首先需要再包下建 Application run 的main 方法
@SpringBootApplication
public class StartDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(StartDemoApplication.class, args);
    }
}

  • 測試類
@RunWith(SpringRunner.class)
@SpringBootTest
public class StartDemoApplicationTests {

    @Resource
    private DemoService demoService;

    @Test
    public void test() {
        String message = demoService.getMessage();
        System.out.println(message);
        Assert.assertEquals("Hello!", message);

        Integer code = demoService.getCode();
        System.out.println(code);
        Assert.assertEquals(123, (int) code);
    }
}

如果沒有 StartDemoApplication 這個類則測試類啟動的時候會報 @SpringBootApplication 找不到錯誤

二、新建 springboot 項目引入剛寫的start項目

  1. service
@Service
public class TestService {

    @Resource
    private DemoService demoService;

    public void message() {
        System.out.println("code:" + demoService.getCode());
        System.out.println("message:" + demoService.getMessage());
    }
}

  1. 測試
    @Resource
    private TestService testService;

    @Test
    public void test() {
        testService.message();
    }

結(jié)果:

code:123
message:Hello!
  1. 重寫DemoService方法
@Service
public class DemoServiceImpl implements DemoService {

    @Override
    public String getMessage() {
        return "Hello!";
    }

    @Override
    public Integer getCode() {
        return 123;
    }
}

  1. 測試結(jié)果
code:123
message:Hello!

之所以這樣的結(jié)果,是因為在start項目中的DemoService 實現(xiàn)類中有一個 @ConditionalOnMissingBean(DemoService.class) 的注解,如果不存在則使用默認的

三、Demo 源碼

GitHub源碼地址

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容