自定义springboot-start
1.创建空项目
2.创建子模块xxx-spring-boot-starter
3.创建子模块xxx-spring-boot-autoconfigure
4.在自定义的starter pom中引入autoconfigure依赖
6.在autoconfigure中编写具体的configerProperties类与autoConfigure类
7.完成后创建META-INF/spring.factories文件,引入自动配置类
8.编写完成项目后对两个模块依次打包
9.创建新项目,引入自定义starter测试
例:
public class WebDriver {
private String version;
public void setVersion(String version) {
this.version = version;
}
public String info() {
return "version:" + version;
}
}
package org.springframework.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "spring.selenium")
public class DriverConfigProperties {
private String desc;
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
package org.springframework.config;
import org.springframework.Driver;
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;
@EnableConfigurationProperties(DriverConfigProperties.class)
@Configuration
public class DriverAutoConfigure {
@ConditionalOnMissingBean(Driver.class)
@Bean
public Driver driver(DriverConfigProperties properties){
Driver driver = new Driver();
driver.setDescription(properties.getDesc());
return driver;
}
}
- 创建META-INF/spring.factories并配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.config.DriverAutoConfigure
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>selenium-spring-boot-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>