SpringBoot自定義starter

SpringBoot 提供了衆多的starter簡化咱們開發,爲了更好地使用這些starter,瞭解其原理是很重要的,今天就來自定義一個starter。html

1,如何自定義 

①,咱們首先要有一個意識,springboot本身適配的通常以:spring-boot-starter-xxx命名,好比:spring-boot-starter-amqpjava

②,本身去適配springboot通常以:xxx-spring-boot-starter命名,好比:mybatis-spring-boot-starterweb

③,springboot自動適配的在依賴時通常不用加版本號,自動適配springboot 的須要版本號。由於sprinboot根本就不知道它的版本spring

③,經過觀察mybatis-spring-boot-starter,咱們發現mybatis-spring-boot-starter並無java代碼,經過觀察其pom.xml咱們發現,它引入了mybatis-spring-boot-autoconfigurespringboot

④,因此咱們能夠仿照mybatis自定義咱們的startermybatis

2,建立工程

①,建立一個空的mystart工程,新建兩個模塊,1,mystart;2,autoconfigurableapp

3,mystart模塊配置

①,mystart依賴於autoconfigurable模塊spring-boot

<groupId>com.spring.start</groupId>
    <artifactId>mystart</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.spring.start.autoconfigurable</groupId>
            <artifactId>autoconfigurable</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

4,autoconfigurable工程代碼

①,pom.xml測試

<groupId>com.spring.start.autoconfigurable</groupId>
	<artifactId>autoconfigurable</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>autoconfigurable</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
	</parent>

	<dependencies>
        <--依賴於springboot starter-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
	</dependencies>

②,編寫xxxProperties類,相似於ServerPropertiesthis

import org.springframework.boot.context.properties.ConfigurationProperties;

//這樣才能讀取到application.properties 中的相應配置
@ConfigurationProperties("spring.su")
public class SuProperties {

    private String prefix;
    private String suffix;

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

③,編寫功能類

public class SuService {
    //這裏不用@Autowired注入,會在自動配置類注入時給設置,因此纔有setSuProperties 方法
    private SuProperties suProperties;

    public String hello(String name){
        return suProperties.getPrefix()+"--"+name+"=="+suProperties.getSuffix();
    }


    public SuProperties getSuProperties() {
        return suProperties;
    }

    public void setSuProperties(SuProperties suProperties) {
        this.suProperties = suProperties;
    }
}

④,編寫自動配置類

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//是個配置類
@Configuration
//在web應用中起效
@ConditionalOnWebApplication
//指明配置類,這樣SuProperties 才能被注入
@EnableConfigurationProperties({SuProperties.class})
public class SuAutoConfiguration {

    @Autowired
    private SuProperties suProperties;

    @Bean
    public SuService suService(){
        SuService suService = new SuService();
        suService.setSuProperties(suProperties);
        return suService;
    }
}

⑤,添加對應的 META-INF/spring.factories

⑥,spring.factories 內容,這樣springboot在啓動時會就掃描對應的AutoConfiguration,爲何會這樣你們知道嗎?參考

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.spring.start.autoconfigurable.SuAutoConfiguration

5,mystart工程代碼

①,pom.xml 依賴於autoConfiguration 工程便可

<groupId>com.spring.start</groupId>
    <artifactId>mystart</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.spring.start.autoconfigurable</groupId>
            <artifactId>autoconfigurable</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

6,測試工程代碼編寫

①,pom.xml

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
        <--依賴mystart 便可,mystart 會依賴autoconfigurable 工程,注意這裏要指定版本號-->
		<dependency>
			<groupId>com.spring.start</groupId>
			<artifactId>mystart</artifactId>
			<version>1.0-SNAPSHOT</version>
		</dependency>

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

②,application.properties

spring.su.prefix=小美女
spring.su.suffix=錯過了

③,測試類

import com.spring.start.autoconfigurable.SuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

    @Autowired
    private SuService suService;

    @RequestMapping("/")
    public String index(){
        return suService.hello("小蘇");
    }
}

④,啓動工程,而後測試便可,

⑤,遺留問題,在application.properties 寫的中文會亂碼,暫時沒有找到解決的辦法,知道如何的同窗歡迎留言,感謝!!!

相關文章
相關標籤/搜索