SpringBoot_第一個starter的編寫
阿新 • • 發佈:2020-12-12
技術標籤:SpringBoot
準備工作:
1、建立兩個模組
mavne:atlgq-spring-boot-starter //starter,負責引入自動裝配
spring:atlgq-spring-boot-starter-autoconfigurer //負責自動裝配
2、把不需要的檔案和依賴都刪除
atlgq-spring-boot-starter-autoconfigurer
pom只留下starter,把主啟動都刪掉
1)、啟動器模組
atlgq-spring-boot-starter
只負責引入自動配置模組
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId >com.atlgq</groupId>
<artifactId>atlgq-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<!--starter,只需要引用這個starter就可以了-->
<dependencies>
<dependency>
<!--從atlgq-spring-boot-starter-autoconfigurer的pom檔案引入-->
<groupId>com.atlgq</groupId>
<artifactId>atlgq-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
2)、自動配置模組
atlgq-spring-boot-starter-autoconfigurer
<?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>
<!--這是我們配置的自動裝配-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.atlgq</groupId>
<artifactId>atlgq-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>atlgq-spring-boot-starter-autoconfigurer</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--引入spring-boot-starter;所有的starter的基本配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
3)、配置properties檔案內容
package com.atlgq.atlgq.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
//宣告這是一個properties類,而且要配置它的話得以atlgq.hello開頭
@ConfigurationProperties(prefix = "atlgq.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public String getPrefix() {
return prefix;
}
public String getSuffix() {
return suffix;
}
}
4)、配置service檔案
package com.atlgq.atlgq.starter;
public class HelloService {
HelloProperties helloProperties;
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public HelloProperties getHelloProperties() {
return helloProperties;
}
public String sayHello(String name){
return helloProperties.getPrefix() + "_" + name + "_" + helloProperties.getSuffix();
}
}
5)、配置autoConfiguration檔案
package com.atlgq.atlgq.starter;
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 //宣告這是一個配置檔案
@ConditionalOnWebApplication //是否是web應用模組
@EnableConfigurationProperties(HelloProperties.class) //所引入的Properties檔案是哪一個
public class HelloAutoConfiguration {
//引入配置檔案
@Autowired
HelloProperties helloProperties;
//配置進去IOC容器
@Bean
public HelloService helloService(){
//裝載服務
HelloService helloService = new HelloService();
//配置檔案
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
然後把這個包install出去,我們就可以在maven倉庫中引用了,引用的程式碼是
<!--引入自定義的模組-->
<dependency>
<groupId>com.atlgq</groupId>
<artifactId>atlgq-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
我們可以在需要用到這個starter的地方引入
新建一個springboot專案,新建一個controller,使用這個我們自己的starter
@Autowired
HelloService helloService;
@GetMapping(value = "/hello")
public String hello(){
String atlgq = helloService.sayHello("atlgq");
return atlgq;
}
如果要配置starter裡面的東西,我們可以在application.properties檔案裡面配置,這個atlgq.hello取決於我們編寫starter裡面properties檔案的設定
atlgq.hello.prefix="hello"
atlgq.hello.suffix=",are you ok?"
結果展示:
視訊教學:尚矽谷雷豐陽老師