创建模式:
创建两个模块:一个是启动器模块,一个是自动配置模块,启动器依赖自动配置模块;别人只需要引入启动器(Starter)就可以了。
启动器命名规则:mybatis-spring-boot-starter;自定义启动器名-spring-boot-starter
步骤:
1). 启动器模块:对应的pom配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?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.newpart.starter</groupId> <artifactId>newpart-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version>
<!--自动配置--> <dependencies> <dependency> <groupId>com.newpart.starter</groupId> <artifactId>newpart-spring-boot-starter-autoconfigurer</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
</project> |
2). 自动配置模块的pom文件
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 | <?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.newpart.starter</groupId> <artifactId>newpart-spring-boot-starter-autoconfigurer</artifactId> <version>1.0-SNAPSHOT</version>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
<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> <!--引入spring‐boot‐starter;所有starter的基本配置--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies>
</project> |
3).创建自动配置类对应的Properties类,这个类中定义所有可以在配置文件中配置的属性
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 | package com.newpart.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
/** */ @ConfigurationProperties(prefix = "newpart.new") public class NewPartProperties {
private String firstName; private String lastName; private Integer age;
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; } } |
4).定义一个自定义测试的服务类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class NewPartService {
NewPartProperties newPartProperties;
public NewPartProperties getNewPartProperties() { return newPartProperties; }
public void setNewPartProperties(NewPartProperties newPartProperties) { this.newPartProperties = newPartProperties; }
public String sayName(String name){ return newPartProperties.getFirstName()+name+newPartProperties.getLastName()+" age: " + newPartProperties.getAge(); } } |
5).定义自定义自动配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @Configuration @EnableConfigurationProperties(NewPartProperties.class) //指明指定配置类的属性类并注册到IOC容器中 public class NewServiceAutoConfiguratioin {
@Autowired NewPartProperties newPartProperties;
@Bean public NewPartService newPartService(){ NewPartService partService = new NewPartService(); partService.setNewPartProperties(newPartProperties); return partService; }
} |
6). 在自动配置模块中创建spring.factories并把自动配置类配置到配置文件中
Starter启动器创建完成可以通过maven安装到本地库中在对应项目中的pom文件中引用就可以使用在自定义的定义的组件