springboot如何创建starter?
1、引入maven依赖
<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.0modelVersion>
<groupId>com.zengqingfagroupId>
<artifactId>hello-springboot-starterartifactId>
<version>0.0.1-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-autoconfigureartifactId>
<version>2.0.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<version>2.0.0.RELEASEversion>
<optional>trueoptional>
dependency>
dependencies>
project>
2、创建properties属性类,用于读取属性。
import org.springframework.boot.context.properties.ConfigurationProperties;
//实体类映射配置信息
@ConfigurationProperties(prefix = "com.zengqingfa")
public class HelloProperties {
private String name="zengqingfa11";
private String hobby="coding11";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "HelloProperties{" +
"name='" + name + '\'' +
", hobby='" + hobby + '\'' +
'}';
}
}
@ConfigurationProperties配置此注解可以自动导入application.properties配置文件中的属性,前提需要指定属性前缀prefix。如果application.properties文件中未指定相应属性,便使用默认的,如上name=“zengqingfa11”,hobby=“coding11”.
3、创建service
public class HelloService {
private HelloProperties properties;
public HelloService() {
}
public HelloService(HelloProperties properties) {
this.properties = properties;
}
public String sayHello() {
System.out.println("大家好,我叫: " + properties.getName() + ", 我喜欢" + properties.getHobby());
return "大家好,我叫: " + properties.getName() + ", 我喜欢" + properties.getHobby();
}
}
4、自动配置类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "com.zengqingfa", value = "enabled", matchIfMissing = true)
public class HelloServiceAutoConfiguration {
@Autowired
private HelloProperties properties;
@Bean
@ConditionalOnMissingBean(HelloService.class)
public HelloService helloService() {
HelloService helloService = new HelloService(properties);
return helloService;
}
}
@Configuration:表明此类是一个配置类,将变为一个bean被spring进行管理。
@EnableConfigurationProperties:启用属性配置,将读取HelloServiceProperties里面的属性。
@ConditionalOnClass:当类路径下面有HelloService此类时,自动配置。
@ConditionalOnProperty:判断指定的属性是否具备指定的值。
@ConditionalOnMissingBean:当容器中没有指定bean是,创建此bean。
spring框架还提供了很多@Condition给我们用,总结:
@ConditionalOnBean(仅仅在当前上下文中存在某个对象时,才会实例化一个Bean)
@ConditionalOnClass(某个class位于类路径上,才会实例化一个Bean)
@ConditionalOnExpression(当表达式为true的时候,才会实例化一个Bean)
@ConditionalOnMissingBean(仅仅在当前上下文中不存在某个对象时,才会实例化一个Bean)
@ConditionalOnMissingClass(某个class类路径上不存在的时候,才会实例化一个Bean)
@ConditionalOnNotWebApplication(不是web应用)
6、在resources文件夹下面新建一个META-INF文件,并在下面创建spring.factories文件,将4中的配置类进行注册。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.zqf.hello.HelloServiceAutoConfiguration
7、maven install到仓库
8、创建一个测试工程引入上面工程
<dependency>
<groupId>com.zengqingfagroupId>
<artifactId>hello-springboot-starterartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
9、application.properties
com.zengqingfa.name=zengqingfa22222
com.zengqingfa.hobby=coding2222
10、创建controller
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping("/hello")
public String hello() {
return helloService.sayHello();
}
}
11、测试
http://localhost:8080/hello
测试结果:大家好,我叫: zengqingfa22222, 我喜欢coding2222
12、注意:
SpringBoot提供的starter以spring-boot-starter-xxx的方式命名的。官方建议自定义的starter使用xxx-spring-boot-starter命名规则。以区分SpringBoot生态提供的starter。