1.創(chuàng)建一個maven的項目鳖目,里面有兩個模塊
- hehe-spring-boot-starter 對外暴露的模塊癣朗,方便外部引用
- hehe-spring-boot-autoconfigure 自動裝載的類涧黄,starter的功能定義在這里面
- springboot-hehe-starter 父項目
maven項目.png
2.父項目的pom文件如下:
<?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>
<!-- 編寫那個版本的spring-boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>springboot-hehe-starter</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>hehe-spring-boot-starter</module>
<module>hehe-spring-boot-autoconfigure</module>
</modules>
<!-- 需要使用的依賴讼昆,autoconfigure會使用到 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!--提供source-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3.編寫hehe-spring-boot-autoconfigure啟動項目
3.1.創(chuàng)建pom文件
<?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">
<parent>
<artifactId>springboot-hehe-starter</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hehe-spring-boot-autoconfigure</artifactId>
<dependencies>
<!-- 需要用到web編寫controller -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--‐導(dǎo)入配置文件處理器会喝,配置文件進(jìn)行綁定就會有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- condition類 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.0</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
3.2 編寫properties類澡罚,支持用戶配置properties屬性,里面也可以設(shè)置一些默認(rèn)屬性
package com.starter.hehe;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("hehe")
public class IndexProperties {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.3 編寫bean敬尺,這里寫了一個controller
···
package com.starter.hehe;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
IndexProperties indexProperties;
public IndexController(IndexProperties indexProperties) {
this.indexProperties=indexProperties;
}
@RequestMapping("/index")
public String index(){
return indexProperties.getName()+"歡迎您";
}
}
···
3.4 編寫自動配置類
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration // 配置類
@ConditionalOnProperty(value = "hehe.name") //application.properties有這個配置才會使用這個自動配置類
@EnableConfigurationProperties(IndexProperties.class) // 讓IndexProperties類注入配置屬性并被spring管理枚尼,變成一個bean
@ConditionalOnClass(StrUtil.class) // 當(dāng)項目中有這個類的時候會加載這個自動配置模塊
public class IndexAutoConfiguration {
@Autowired
IndexProperties indexProperties;
@Bean
public IndexController indexController(){
return new IndexController(indexProperties);
}
}
3.5 編寫spring.factories
在resource下面建立META-INF包,創(chuàng)建spring.factories文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.starter.hehe.IndexAutoConfitguration
4.編寫hehe-spring-boot-starter pom文件
<?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">
<parent>
<artifactId>springboot-hehe-starter</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hehe-spring-boot-starter</artifactId>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>hehe-spring-boot-autoconfigure</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
5.執(zhí)行maven的install砂吞,打包到本地倉庫
6.其他springboot項目引入剛寫的starter項目,并引入hutool工具類署恍,否則不會自動裝載進(jìn)來
<dependency>
<groupId>org.example</groupId>
<artifactId>hehe-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.0</version>
</dependency>
7.properties文件添加配置hehe.name=[hehe]
8.啟動項目,訪問http://localhost:8082/index
1637675149(1).png