1 先建立一个空项目(本身起名字)java
2 在1步骤中建立的空项目中建立两个模块,建立方式以下图所示 web
1)建立启动器模块用maven项目个人启动器名称为 mao-spring-boot-starterspring
pom.xml以下所示 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"apache
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.mao.starter</groupId> <artifactId>mao-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> <!-- 启动器--> <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> <!-- 引入自动配置模块--> <dependency> <groupId>com.mao-starter</groupId> <artifactId>mao-spring-boot-starter-autoconfigurer</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
</project>app
2) 建立自动配置模块,我建立的名字为:mao-spring-boot-starter-autoconfigurer以下图所示  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"maven
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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.21.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.mao-starter</groupId> <artifactId>mao-spring-boot-starter-autoconfigurer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mao-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>
<!-- 全部要引入的基本配置-->spring-boot
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies>
</project>测试
3 建立以下图的3个类ui
helloProperties.java //属性类this
package com.maostarter.mao;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "map.hello")
public class helloProperties {
public String prefix; 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; } private String suffix;
}
HelloService.java //向外提供服务的类
public class HelloService {
helloProperties helloPropertie; public helloProperties getHelloPropertie() { return helloPropertie; } public void setHelloPropertie(helloProperties helloPropertie) { this.helloPropertie = helloPropertie; } public String sayHelloMao(String name){ return helloPropertie.getPrefix()+'*'+name+helloPropertie.getSuffix(); }
}
helloServiceAutoConfiguration.java //服务自动注入类
@Configuration
@ConditionalOnWebApplication //web应用才生效
@EnableConfigurationProperties(helloProperties.class)
public class helloServiceAutoConfiguration {
@Autowired helloProperties helloPropertie; @Bean public HelloService helloService(){ HelloService service = new HelloService(); service.setHelloPropertie(helloPropertie); return service; }
}
4 配置spring.factories文件,以下图建立方式
spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.maostarter.mao.helloServiceAutoConfiguration
5 如何生成starter,在这个过程有肯能出现各个问题,通常是jdk版本不对,maven版本问题,出现问题首先仔细阅读控制台的信息
不要一出现问题就百度
6 测试刚刚建立的自定义starter建立个springBoot项目
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 http://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.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.autostart.test</groupId> <artifactId>autostart</artifactId> <version>0.0.1-SNAPSHOT</version> <name>autostart</name> <description>Demo project for Spring Boot</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.mao.starter</groupId> <artifactId>mao-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </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> </plugin> </plugins> </build>
</project>
6 给以前配置的属性文件复制 以下图所示
7 建立一个测试用例
@Controller
public class HelloController {
@Autowired HelloService helloService; @ResponseBody @RequestMapping("/hello") public String Hello(){ return helloService.sayHelloMao("haha"); }
}
8 测试结果以下图所示