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

結果:

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 實現(xiàn)類中有一個 @ConditionalOnMissingBean(DemoService.class) 的注解,如果不存在則使用默認的

三胚想、Demo 源碼

GitHub源碼地址

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末琐凭,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子浊服,更是在濱河造成了極大的恐慌统屈,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,383評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件牙躺,死亡現(xiàn)場離奇詭異愁憔,居然都是意外死亡,警方通過查閱死者的電腦和手機述呐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,522評論 3 385
  • 文/潘曉璐 我一進店門惩淳,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蕉毯,“玉大人,你說我怎么就攤上這事∽ぷ樱” “怎么了艇肴?”我有些...
    開封第一講書人閱讀 157,852評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長棉磨。 經(jīng)常有香客問我江掩,道長,這世上最難降的妖魔是什么乘瓤? 我笑而不...
    開封第一講書人閱讀 56,621評論 1 284
  • 正文 為了忘掉前任环形,我火速辦了婚禮,結果婚禮上衙傀,老公的妹妹穿的比我還像新娘抬吟。我一直安慰自己,他們只是感情好统抬,可當我...
    茶點故事閱讀 65,741評論 6 386
  • 文/花漫 我一把揭開白布火本。 她就那樣靜靜地躺著,像睡著了一般聪建。 火紅的嫁衣襯著肌膚如雪钙畔。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,929評論 1 290
  • 那天金麸,我揣著相機與錄音擎析,去河邊找鬼。 笑死挥下,一個胖子當著我的面吹牛揍魂,可吹牛的內(nèi)容都是我干的挪鹏。 我是一名探鬼主播,決...
    沈念sama閱讀 39,076評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼愉烙,長吁一口氣:“原來是場噩夢啊……” “哼讨盒!你這毒婦竟也來了?” 一聲冷哼從身側響起步责,我...
    開封第一講書人閱讀 37,803評論 0 268
  • 序言:老撾萬榮一對情侶失蹤返顺,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后蔓肯,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體遂鹊,經(jīng)...
    沈念sama閱讀 44,265評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,582評論 2 327
  • 正文 我和宋清朗相戀三年蔗包,在試婚紗的時候發(fā)現(xiàn)自己被綠了秉扑。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,716評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡调限,死狀恐怖舟陆,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情耻矮,我是刑警寧澤秦躯,帶...
    沈念sama閱讀 34,395評論 4 333
  • 正文 年R本政府宣布,位于F島的核電站裆装,受9級特大地震影響踱承,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜哨免,卻給世界環(huán)境...
    茶點故事閱讀 40,039評論 3 316
  • 文/蒙蒙 一茎活、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧琢唾,春花似錦载荔、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,798評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至芍碧,卻和暖如春煌珊,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背泌豆。 一陣腳步聲響...
    開封第一講書人閱讀 32,027評論 1 266
  • 我被黑心中介騙來泰國打工定庵, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 46,488評論 2 361
  • 正文 我出身青樓蔬浙,卻偏偏與公主長得像猪落,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子畴博,可洞房花燭夜當晚...
    茶點故事閱讀 43,612評論 2 350

推薦閱讀更多精彩內(nèi)容