1.新建boot-customer-starer空项目

2.创建一个maven场景启动器module
不做任何处理

3.创建一个springboot module作为自动配置包

4.在场景启动器moduel中引入自动配置包
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <?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.yang</groupId> <artifactId>yang-hello-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version>
<properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties>
<dependencies> <dependency> <groupId>com.yang</groupId> <artifactId>yang-hello-spring-boot-starter-autoconfigure</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
</project>
|
5.自定义功能
例如现在有一个经常使用的功能是给人打招呼.我们可以将这个经常使用的功能抽取出来放到一个service中
5.1.在自动配置包中新建service

1 2 3 4 5 6 7 8 9
| public class HelloService { @Autowired HelloProperties helloProperties;
public String sayHello(String userName){ return helloProperties.getPrefix()+ ": "+userName+">>"+helloProperties.getSuffix(); } }
|
5.2.新建helloProperties

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.yang.hello.bean; import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("yang.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; } }
|
前缀后缀从属性类中获取
1 2 3 4 5 6 7 8 9
| public class HelloService { @Autowired HelloProperties helloProperties;
public String sayHello(String userName){ return helloProperties.getPrefix()+ ": "+userName+">>"+helloProperties.getSuffix(); } }
|
5.3.新建自动配置类

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.yang.hello.auto;
import com.yang.hello.bean.HelloProperties; import com.yang.hello.service.HelloService; 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;
@Configuration
@EnableConfigurationProperties(HelloProperties.class) public class HelloServiceAutoConfiguration {
@ConditionalOnMissingBean(HelloService.class) @Bean public HelloService helloService(){ HelloService helloService = new HelloService(); return helloService; } }
|
新建spring.factories文件

1 2 3
| org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.yang.hello.auto.HelloServiceAutoConfiguration
|
作用:让项目启动后加载此文件
5.4.打包打本地仓库供其他组件使用

6.创建一个项目测试功能

引入web开发场景,自定义starter的依赖
pow.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| <?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.7.14</version> <relativePath/> </parent> <groupId>com.yang</groupId> <artifactId>boot-customer-starter-hello-test</artifactId> <version>0.0.1-SNAPSHOT</version> <name>boot-customer-starter-hello-test</name> <description>boot-customer-starter-hello-test</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.yang</groupId> <artifactId>yang-hello-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build>
</project>
|
依赖结构

编写一个controller测试

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.yang.controller;
import com.yang.hello.service.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;
@RestController public class HelloController {
@Autowired HelloService helloService;
@GetMapping("hello") public String sayHello(){ String s = helloService.sayHello("张三"); return s; } }
|
在配置文件中配置前缀后缀
1 2
| yang.hello.prefix=YangAns yang.hello.suffix=666
|
运行测试

成功显示!!