前几个月和隔壁组的老王闲聊,他说项目的供应商离职率居高不下,最近还有开发刚接手ESB订阅发布接口才两周就提出离职,而他能作的就只有苦笑和默默地接过这个烂摊子了。
而然幸福的家庭老是类似的,而不幸的我却因业务变革走上了和老王同样的道路。单单是接口的开发竟然能迫使一位开发毅然决然地离职,我既不相信是人性的扭曲,更不信是道德的沦丧。
抛开这个富有色彩的故事而言,我发现原来的项目存在以下问题:html
针对前两个问题,咱们只需梳理出必须的依赖项并加入Maven或Gradle管理,而后托管到Git便可。
然后二者则能够经过spring-boot-starter将必选依赖项和配置统一管理,并附上相关技术文档;而后经过模板模式和注解简化开发流程,提供Demo下降入门难度。
最后就能够把具体的业务功能开发交给供应商处理,咱们专心作好过程管理和验收便可。web
本文将着重分享spring-boot-starter开发的事项,请坐好扶稳!spring
在自定义starter前咱们总要思考如何命名咱们的starter,而官方提供以下的命名规范:apache
经过Spring Initializr或Spring Boot CLI建立项目结构后,将pom.xml的相关项目修改成以下内容json
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifacId> <version>2.3.1.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- 下面为自定义Starter的依赖项 --> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.4</version> <executions> <execution> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
在starter中咱们会定义SpringBean的注册配置和属性配置,如ESB订阅服务的配置项目为mybatis
@Configuration @EnableConfigurationProperties({EsbServerProperties.class}) public class EsbServerConfiguration { @Bean public SpringBus springBus(){ return new SpringBus(); } @Bean public LoggingFeature loggingFeature(){ return new LoggingFeature(); } @Bean public List<JMSConfigFeature> jmsConfigFeatures(EsbServerProperties props) throws JMSException { List<JMSConfigFeature> features = new ArrayList<>(); /** * 这里会使用EsbServerProperties的属性构建Bean实例 */ return features; } }
属性配置项app
// 从application.yml等配置文件中读取并绑定esb.server.destination等属性值 @Data @ConfigurationProperties("esb.server") public class EsbServerProperties { String destination; int currConsumers = 1; String channel; int ccsid = 1205; int transportType = 1; List<String> connectionNameLists; boolean replyError = false; String replySuccessText = "Success"; String replyErrorText = "Failure"; }
到这里咱们已经完成一个基本的starter的功能框架
@ConfigurationProperties
定义该starter注册bean时须要的属性集合@Configuration
定义该starter注册的bean但引用该starter的项目要如何启用配置呢?其实有两种方式,分别为手动和自动,其中咱们会着重讲解自动启用配置。maven
所谓手动启用配置其实就是在SpringBoot入口类上添加启用配置用的自定义注解,针对上面的EsbServerConfiguration咱们能够自定义EnableESBSrv注解spring-boot
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Import({EsbServerConfiguration.class}) public @interface EnableEsbSrv { }
而后入口类的@SpringBootApplication
注解先后添加@EnableEsbSrv
便可。
自动启用配置即只需在pom.xml中引入所依赖的starter,而后启用应用便可自动启用该starter的@Configuration
所注解的类从而注册Bean和读取属性配置。
而这一切都是由AutoConfigurationImportSelector
来操刀,而咱们能够经过@EnableAutoConfiguration
或@SpringBootApplication
等实例化AutoConfigurationImportSelector
类,配合菜谱resources/META-INF/spring.factories实现自动化配置的功能。
具体手法就是:将EsbServerConfiguration的全限类名称写在resources/META-INF/spring.factories的org.springframework.boot.autoconfigure.EnableAutoConfiguration
下, 若存在多个则用逗号分隔。
org.springframework.boot.autoconfigure.EnableAutoConfiguration = \ com.john.starter.EsbServerConfiguration,\ com.john.starter.OtherConfiguration
应用启动时会将application.yml中对应的配置项绑定到@ConfigurationProperties
标注的类实例上,那么对于应用开发人员而言平常工做就是修改application.yml的配置项。但IDE又缺乏配置项的智能提示,那就很低效了。幸好Spring Boot早就为咱们提供好解决方案,分为手工和自动两种。为了效率固然是能够自动就不用手动的了。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
mvn compile
时会生成target/classes/META-INF/spring-configuration-metadata.json;<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
spring-boot-starter很是适合用于团队的技术积累和沉淀,不过想恰到好处地应用起来,不只要须要深刻Spring内部原理还要梳理清楚业务逻辑。后续咱们再深刻探讨Spring内核的事情吧!
转载请注明来自:https://www.cnblogs.com/fsjoh... —— ^_^肥仔John