以Spring boot 作为微服务开发的项目中会用到很多第三方,或者自建的一些依赖SDK,例如 分布式锁、文件管理、数据库连接、统一错误拦截等组件。为了达到通用的目的,都会创建自定义的spring boot stater 组件。以下介绍创建步骤: 一、创建Maven测试项目test-starter pom文件如下:二、创建自动装配类TestServiceConfiguration.javapackage com.test.spring.boot.starter.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.test.spring.boot.starter.TestService; import com.test.spring.boot.starter.TestServiceImpl; @Configuration @EnableConfigurationProperties(value = TestServiceProperties.class) public class TestServiceConfiguration { @Autowired private TestServiceProperties properties; @Bean public TestService getTestService() { return new TestServiceImpl(properties); } } 三、创建配置类TestServiceProperties.javapackage com.test.spring.boot.starter.config; import org.springframework.boot.context.properties.ConfigurationProperties; import lombok.Data; import lombok.ToString; @Data @ToString @ConfigurationProperties(prefix = "test.starter") public class TestServiceProperties { /** * 客户端ID */ private String clientId; /** * 客户端名称 */ private String clientName; } 四、创建提供服务的接口TestService.javapackage com.test.spring.boot.starter; public interface TestService { /** * 测试 * @param name * @return */ String sayHello(String name); } 五、创建TestService的实现类型TestServiceImpl.javapackage com.test.spring.boot.starter; import com.test.spring.boot.starter.config.TestServiceProperties; /** * 服务实现 * @author test * */ public class TestServiceImpl implements TestService { private TestServiceProperties properties; public TestServiceImpl(TestServiceProperties properties) { this.properties = properties; } @Override public String sayHello(String name) { return "clientId: "+properties.getClientId()+",clientName: " +properties.getClientName()+",welcome "+name+"!"; } } 六、创建自定义注解@EnableTestpackage com.test.spring.boot.starter.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.context.annotation.Import; import com.test.spring.boot.starter.config.TestServiceConfiguration; /** * 自定义自动装配启动注解 * @author test * */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Import({TestServiceConfiguration.class}) public @interface EnableTest { } 通过以上步骤就实现了自定义Spring boot starter test-spring-boot-starter 组件,下面创建测试项目去调用这个starter组件。 创建starter组件使用项目 一、创建maven测试项目test-service,pom文件如下 com.test.starter test-starter1.0.0 4.0.0 test-spring-boot-starterjar org.springframework.boot spring-boot-autoconfigure2.3.2.RELEASE test-spring-boot-starter 二、创建启动类Application.javapackage com.test.service; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import com.test.spring.boot.starter.annotation.EnableTest; @EnableTest //启动自定义组件 @ComponentScan(basePackages = { "com.test.service"}) @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 三、创建配置文件application.ymltest: starter: client-id: test-service-01 client-name: test-service 四、创建测试类TestController.javapackage com.test.service.controller; import javax.annotation.Resource; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.test.spring.boot.starter.TestService; @RestController @RequestMapping("/v1/test") public class TestController { @Resource private TestService testService; @GetMapping("/{name}") public String hello(@PathVariable("name")String name) { return testService.sayHello(name); } } 到这里整个自定义spring boot starter 创建就完成了,同时也依赖到项目中进行使用了。 项目代码:starter-test: 自定义starter组件 com.test.starter test-starter1.0.0 4.0.0 test-servicejar org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-testtest org.junit.vintage junit-vintage-enginecom.test.starter test-spring-boot-starter1.0.0 test-service org.springframework.boot spring-boot-maven-plugin${spring.boot.version} repackage