SpringBoot原理-手寫starter

概念

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.
We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need minimal Spring configuration.

簡單的說
 1. 構建簡單
 2. 減少配置(減少了spring中的web.xml application.xml spring-mvc.xml)

特點

Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
Provide opinionated ‘starter’ dependencies to simplify your build configuration
Automatically configure Spring and 3rd party libraries whenever possible
Provide production-ready features such as metrics, health checks, and externalized configuration
Absolutely no code generation and no requirement for XML configuration

 1. 內嵌Tomcat,Jetty或者Undertow,所以不需要打成war包部署至Tomcat
 2. 提供starter依賴,這一節的重點 eg:spring-boot-starter-web
 3. 儘可能自動配置第三方依賴
 4. 提供健康檢查,可擴展配置
 4. 完全不需要配置代碼或xml文件

手寫starter

  1. 創建結構
    在這裏插入圖片描述
  2. 添加依賴
<!--開發自定義starter必須的依賴-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
  1. 創建SayHello接口以及實現類
public interface ISayHello {
    //方便測試只有一個方法
    void sayHello();
}
import org.springframework.beans.factory.annotation.Autowired;

public class SayHelloImpl implements ISayHello {

    @Autowired
    private SayHelloProperties sayHelloProperties;

    @Override
    public void sayHello() {
        //從application.xml獲取spring.gao.name的值
        String name = sayHelloProperties.getName();
        System.out.println("你好:" + name);
    }
}
  1. 創建讀取application.yml類,模擬SpringBoot從application.yml文件讀取配置
import org.springframework.boot.context.properties.ConfigurationProperties;

//自定義註解前綴爲 spring.gao
@ConfigurationProperties(prefix = "spring.gao")
public class SayHelloProperties {

    private String name = "xiaoming";

    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
}
  1. 創建自動配置類SayHelloAutoconfiguration,實現將SayHello注入SpringIOC
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//保證springboot啓時注入IOC容器
@Configuration
@ConditionalOnClass(ISayHello.class)
@EnableConfigurationProperties(SayHelloProperties.class)
public class SayHelloAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public ISayHello getSayHello(){
        return new SayHelloImpl();
    }
}
  1. 創建META-INF目錄下並創建spring.factories引入指定自動配置類
# 將加載指定的自動配置類
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com/gao/cn/SayHelloAutoConfiguration
  1. 將依賴打包,在另一個項目導入這個依賴
mvn clean install 
  1. 測試結果
    在這裏插入圖片描述
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章