第八章 Spring Boot 自定義 starters
自動配置類
@Configuration // 指定這個類是配置類
@Conditionalxxx // 指定條件成立的情況下自動配置類生效
@AutoConfigureAfter // 指定自動配置類的順序
@Bean // 給容器中添加元件
@ConfigurationProperties // 結合相關Properties類來綁定相關的配置
@EnableConfigurationProperties // 讓Properties生效加入到容器中
啟動器 Starter
啟動器子產品是一個空的 JAR 檔案,僅提供輔助性依賴管理,這些依賴可能用于自動裝配或者其他類庫
命名規約:
推薦使用一下命名規約
官方命名空間:
字首:spring-boot-starter-
模式:spring-boot-starter-子產品名
舉例:spring-boot-starter-web、spring-boot-starter-jdbc
自定義命名空間:
字首:-spring-boot-starter
模式:子產品名-spring-boot-starter
舉例:mybatis-spring-boot-starter
自定義 starter
1、Idea 建立空工程 Empty Project
2、建立 spring 自動配置子產品
pom.xml
<?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.3.1.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<groupId>com.mouday</groupId>
<artifactId>mouday-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mouday-spring-boot-starter-autoconfigurer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<!--所有starter的基本配置-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
屬性檔案 HelloProperties.java
package com.mouday.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "mouday.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;
}
}
HelloService.java
package com.mouday.starter;
public class HelloService {
HelloProperties properties;
public String sayHello(String name){
return properties.getPrefix() + name + properties.getSuffix();
}
public HelloProperties getProperties() {
return properties;
}
public void setProperties(HelloProperties properties) {
this.properties = properties;
}
}
配置類
package com.mouday.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)
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService service = new HelloService();
service.setProperties(helloProperties);
return service;
}
}
src/main/resources/META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.mouday.starter.HelloServiceAutoConfiguration
3、建立 maven 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.mouday</groupId>
<artifactId>mouday-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.mouday</groupId>
<artifactId>mouday-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
測試
引入依賴 pom.xml
<dependency>
<groupId>com.mouday</groupId>
<artifactId>mouday-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
修改配置檔案 application.properties
mouday.hello.prefix=hello
mouday.hello.suffix=hi
建立測試 Controller
package com.example.demo.controller;
import com.mouday.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
@ResponseBody
public String hello(String name){
return helloService.sayHello(name);
}
}
http://localhost:8080/hello?name=Tom
通路結果
helloTomhi
學習新技術:
一看文檔,二看源碼