目录
自定义starter
新建工程
新建springboot工程.pring 官方定义的Starter通常命名遵循的格式为spring-boot-starter-{name},例如 spring-boot-starter-web。Spring官方建议,
非官方Starter命名应遵循{name}-spring-boot-starter的格式,例如,dubbo-spring-boot-starter
假设该starter的主要功能:是给入参加上前缀和后缀.
引入jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--需要这个jar 才会自动加载enableautoconfiguration-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
启动类不需要做任何修改
编写主要功能实现类
import lombok.Data;
/**
* @Date :Created in 2020-08-03 11:53
* @Description:
*/
@Data
public class SomeService {
private String prefix;
private String suffix;
public SomeService(String prefix, String suffix) {
this.prefix = prefix;
this.suffix = suffix;
}
/**
* @Description: 整个starter的主要功能
* @Date: 2020-08-03 15:36
* @Return java.lang.String
* @Param word
*/
public String wrap(String word) {
return prefix + word + suffix;
}
}
编写属性配置类 用于从yaml中读取配置
读取前缀是some.service的前缀,这些属性的配置是配置在要使用该starter的应用上的时候才会执行上面的方法,
@Data
@ConfigurationProperties(prefix = "some.service")
public class SomeServiceProperties {
private String before;
private String after;
private String enable;
}
编写自动配置类
当且仅当 some.service.enable的值有配置且为true时
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;
/**
* @Date :Created in 2020-08-03 11:57
* @Description: 注入配置,根据不同的配置实现不同的功能
*/
@Configuration
@ConditionalOnClass(SomeService.class)
@EnableConfigurationProperties(SomeServiceProperties.class)
public class SomeServiceAutoConfiguration {
@Autowired
SomeServiceProperties someServiceProperties;
@Bean
@ConditionalOnProperty(name = "some.service.enable", havingValue = "true", matchIfMissing = true)
public SomeService someService() {
return new SomeService(someServiceProperties.getBefore(), someServiceProperties.getAfter());
}
@Bean
@ConditionalOnMissingBean
public SomeService someService2() {
return new SomeService("", "");
}
}
配置spring.factories
在resource文件夹下新建META-INF文件夹,然后再META-INF文件夹里边新建spring.factories文件,文件内容如下,值是自动配置类的全路径
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.com.zsw.SomeServiceAutoConfiguration
安装到本地maven仓库
执行mvn install 方法安装要本地仓库
使用自定义starter
在上面的步骤中,我们创建了我们自己的starter,接下来在我们自己的项目使用这个starter
引入jar包
<dependency>
<groupId>cn.com.zsw</groupId>
<artifactId>wrap-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
方法调用
@RestController
public class DemoTestController {
@Autowired
SomeService someService;
@GetMapping("test")
public String test(String name) {
return someService.wrap(name);
}
}