spring-boot-starter原理及自定義

前言

在我們做項目時會用到許多場景啓動器,我們只需要引入對應的starter就可以使用相應的功能,我們可以在配置文件中根據需要修改一些屬性,我們也可以自定義一些starter來使用,首先我們需要直到starter的原理
在這裏插入圖片描述

原理

1.我們以WebMvcAutoConfiguration爲例,看這個類上標註的註解

@Configuration  //指定這個類是一個配置類
@ConditionalOnWebApplication //在指定條件成立的情況下自動配置類生效,這裏值在web模式下生效
@AutoConfigureAfter  //指定自動配置類的順序

2.觀察這個類中的信息
如果這個自動配置類生效,就可以往容器中添加各種組件,比如:

		@Bean
        public FormattingConversionService mvcConversionService() {
            WebConversionService conversionService = new WebConversionService(this.mvcProperties.getDateFormat());
            this.addFormatters(conversionService);
            return conversionService;
        }

往容器中添加組件時可能會用上一些屬性
在這裏插入圖片描述
如上情況都配置好的情況下,我們要想讓這個自動配置類生效,必須加載這些自動配置類,我之前的文章爺談到了spring boot是如何加載這些自動配置類的點擊跳轉

將需要啓動就加載的自動配置類,配置在META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\

模式

在這裏插入圖片描述
我們用web模塊來舉例
在這裏插入圖片描述

自定義配置

1.工程目錄結構
在這裏插入圖片描述
如圖中所示
在這裏插入圖片描述
2.自動配置

在這裏插入圖片描述
我們新建幾個類,並且往容器中添加
在這裏插入圖片描述

/**
屬性類
*/
@ConfigurationProperties(prefix = "rwh.hello")
public class HelloProperties {
    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 HelloService {

    HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    public String sayHello(String name){
        return helloProperties.getPrefix() +"-"+ name + helloProperties.getSuffix();
    }
}


/**
配置類
*/
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)//往容器中添加HelloProperties,使之生效
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService(){
        HelloService service = new HelloService();
        service.setHelloProperties(helloProperties);
        return service;
    }
}


光有這幾個類還不夠,我們還要將這個自動配置包設置爲程序啓動時啓動,也就是之前說的將需要啓動就加載的自動配置類,配置在META-INF/spring.factories

在這裏插入圖片描述
然後再maven中點擊install,將我們自定義的包導入maven倉庫
在這裏插入圖片描述
到此爲止我們自定義的配置包就寫好了,我們新建一個springboot應用來測試

		<dependency>
            <groupId>org.example</groupId>
            <artifactId>rwhspringboot</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

在新建的springboot項目的pom導入我們的場景啓動器starter(空實現的那一個)
在這裏插入圖片描述
我們可以看到我們的包被導入進來了,我們寫一個controller

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String hello(){
        return helloService.sayHello("hello");
    }
}

修改我們的屬性
在這裏插入圖片描述
我們打開瀏覽器
在這裏插入圖片描述
測試成功

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