SpringBoot 自定義starter

我認爲Spring之因此流行,是由於spring starter模式,這能夠讓模塊開發更加獨立,相互依賴更加鬆散以及更加方便的繼承。
複製代碼

話很少說,先寫一個starter。java

1. 建立一個maven工程,pom以下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.studyspring</groupId>
    <artifactId>study-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>study-starter</name>
    <description>Demo project for Spring Boot</description>

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

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>
複製代碼

2. 定義一個接口

package com.studyspring.studystarter;

public interface HelloService {
    String sayHello();
}
複製代碼

3. 對接口作一個簡單的實現,實際開發中確定比這複雜

package com.studyspring.studystarter;

import org.springframework.stereotype.Component;

@Component
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello() {
        return "test starter...";
    }
}
複製代碼

4. 建立一個配置類HelloServiceAutoConfiguration

package com.studyspring.studystarter;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.studyspring.studystarter")
public class HelloServiceAutoConfiguration {

}
複製代碼

這裏,你會發現HelloServiceAutoConfiguration類中沒有邏輯實現,它存在的目的僅僅是經過註解進行配置聲明和掃描路徑。到了這裏,一個starter還剩最後一步,那就是聲明這個配置類的路徑,在Spring的根路徑下創建META-INF\spring.factories文件,配置以下:web

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.studyspring.studystarter.HelloServiceAutoConfiguration
複製代碼

到此,一個標準的starter已經完成了。 使用方法,只須要打包mvn clean install,而後在其餘的web項目引入該依賴:spring

<groupId>com.studyspring</groupId>
    <artifactId>study-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
複製代碼

同時,添加Controller邏輯,以下:apache

import com.studyspring.studystarter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping("hellostart")
    public String sayHello() {
        return helloService.sayHello();
    }
}
複製代碼

開發的starter對使用者來講很是的方便,除了在pom中引入依賴,什麼都不須要作就能夠直接在新項目中的接口中注入:bash

@Autowired
    private HelloService helloService;
複製代碼

那麼Spring Boot是怎麼作到的呢?下一篇starter源碼解析app

參考書籍 《Spring源碼深度分析》maven

相關文章
相關標籤/搜索