1.创建一个maven的项目,里面有两个模块
- hehe-spring-boot-starter 对外暴露的模块,方便外部引用
- hehe-spring-boot-autoconfigure 自动装载的类,starter的功能定义在这里面
- springboot-hehe-starter 父项目
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.创建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>
<!--‐导入配置文件处理器,配置文件进行绑定就会有提示-->
<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属性,里面也可以设置一些默认属性
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) // 当项目中有这个类的时候会加载这个自动配置模块
public class IndexAutoConfiguration {
@Autowired
IndexProperties indexProperties;
@Bean
public IndexController indexController(){
return new IndexController(indexProperties);
}
}
3.5 编写spring.factories
在resource下面建立META-INF包,创建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.执行maven的install,打包到本地仓库
6.其他springboot项目引入刚写的starter项目,并引入hutool工具类,否则不会自动装载进来
<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