定義springboot starter的兩種方式

springboot starter 命令規則

首先了解一下starter的命名規範
如果是springboot官方的starter,命令格式爲spring-boot-starter-xxxxx
如果是我們開發starter,命令格式爲xxxxx-spring-boot-starter

1.自動注入式,引入依賴後就可直接注入

第一步:創建pom項目,引入springboot依賴

<properties>
        <spring-boot.version>2.1.9.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

第二步:編寫代碼
創建一個類TestProperties.java,接受配置文件中的配置內容。在application.properties文件中以這種格式配置:spring.test.id=1;就能將配置的值注入到id字段中。

@ConfigurationProperties(prefix = TestProperties.PREFIX)
public class TestProperties {

	public static final String PREFIX = "spring.test";

	private String id;
	private String name;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	@Override
	public String toString() {
		return "TestProperties [id=" + id + ", name=" + name + "]";
	}

}

創建類TestPrinter,模擬用於注入的執行類

public class TestPrinter {
	private TestProperties testProperties;
	public TestPrinter (TestProperties testProperties) {
		this.testProperties = testProperties;
	}
	
	public void print() {
		System.out.println("test TestPrinter:"+testProperties.toString());
	}

}

創建類TestConfiguration,用於springboot自動注入

@Configuration
@ConditionalOnClass(TestPrinter.class)
@EnableConfigurationProperties(TestProperties.class)
public class TestConfiguration {
	
	@Autowired
	private TestProperties testProperties;
	
	@Bean
	@ConditionalOnProperty(name = "test.enable", havingValue = "true", matchIfMissing = true)
	public TestPrinter testPrinter() {
		return new TestPrinter(testProperties);
	}

}

最後:springboot的自動注入規則是會加載jar包中resource目錄下META-INF/spring.factories文件

在這裏插入圖片描述
文件內容如下(裏面的類路徑需要換成你自己的):

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
springboot.starter.test.TestConfiguration

測試:新建一個maven項目,引入starter依賴,注入TestPrinter,調用testPrinter.print()方法,驗證是否成功。

2.開關式,引入依賴後,還需要開啓(@EnableXXX)

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