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 的自動註解主要還是用這些條件註解來實現的。請查看之前的文章:

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

結果:

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. 測試結果
code:123
message:Hello!

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

三、Demo 源碼

GitHub源碼地址

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章