Spring boot magic Hall: talking about spring boot starter with intelligent tips

Cubic meter ^Fat John 2020-11-11 17:33:34
spring boot magic hall talking


Preface

A few months ago, I chatted with Lao Wang from the next group , He said there was a high turnover rate among suppliers of the project , Recently, we have just taken over the development ESB Only two weeks after the subscription publishing interface proposed to quit , And all he could do was smile bitterly and take over the mess in silence .
But happy families are always similar , Unfortunately, I was on the same road as Lao Wang because of the business transformation . The development of interface alone can force a developer to leave his job resolutely , I don't believe in the distortion of human nature , Not to believe is the decline of morality .
Put aside this colorful story , I found that the original project had the following problems :

  1. No modern dependency management and build tools are used ( Such as Maven, Gradle), Put directly what you depend on Jar The package is stored in the project directory lib Directory , Over time leads to lib There are a lot of useless in the directory Jar package ;
  2. No code version management tools are used to manage code ;
  3. Lack of technical documentation , All rely on the master with apprentice way to teach the use of the framework and development process ;
  4. There are many mechanical configuration items , And later developers can only follow the example to add configuration , It is easy to make mistakes and increase the difficulty of troubleshooting .
    For the first two questions , We just need to tease out the necessary dependencies and add Maven or Gradle management , And then escrow to Git that will do .
    The latter two can be done through spring-boot-starter Unified management of required dependencies and configurations , Relevant technical documents are attached ; Then simplify the development process through template patterns and annotations , Provide Demo Make it easier to get started .
    Finally, the specific business function development can be handed over to the supplier , We can concentrate on process management and acceptance .

This article will focus on sharing spring-boot-starter Development matters , Please sit down and hold on !

Naming specification

In the custom starter Before that, we have to think about how to name our starter, The official nomenclature is as follows :

  1. Official starter With spring-boot-starter Name the item as a prefix
    Such as :spring-boot-starter-web
  2. The unofficial ones are spring-boot-starter Name items as suffixes
    Such as :mybatis-spring-boot-starter

Project structure

adopt Spring Initializr or Spring Boot CLI After creating the project structure , take pom.xml The relevant items of the project are revised as follows

<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>
<!-- Here is the custom Starter The dependencies of -->
</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>

stay starter We will define SpringBean Registration configuration and property configuration of , Such as ESB The configuration item of subscription service is

@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<>();
/**
* I'm going to use EsbServerProperties Attribute construction of Bean example
*/
return features;
}
}

Property configuration item

// from application.yml Read and bind from the configuration file esb.server.destination Attribute values such as
@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";
}

So far we have completed a basic starter The function of

  1. adopt @ConfigurationProperties Define the starter register bean Set of attributes required when
  2. adopt @Configuration Define the starter Registered bean

But quote the starter How to enable configuration for your project ? There are actually two ways , They are manual and automatic , We will focus on the auto enable configuration .

Manually enable configuration

The so-called manual configuration is actually in SpringBoot Add a custom annotation to the entry class to enable configuration , For the top EsbServerConfiguration We can customize it EnableESBSrv annotation

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({EsbServerConfiguration.class})
public @interface EnableEsbSrv {
}

And then the entry class @SpringBootApplication Add... Before and after the annotation @EnableEsbSrv that will do .

It's a labor-saving automatic startup configuration

To enable configuration automatically, you just need to pom.xml Introduce the dependent starter, Then enable the application to automatically enable the starter Of @Configuration Annotated classes to register Bean And read property configuration .
And it's all about AutoConfigurationImportSelector Let's do it , And we can @EnableAutoConfiguration or @SpringBootApplication And so on AutoConfigurationImportSelector class , Matching recipes resources/META-INF/spring.factories Realize the function of automatic configuration .
The specific method is : take EsbServerConfiguration The fully restricted class name of is written in resources/META-INF/spring.factories Of org.springframework.boot.autoconfigure.EnableAutoConfiguration Next , If there are more than one, separate them with commas .

org.springframework.boot.autoconfigure.EnableAutoConfiguration = \
com.john.starter.EsbServerConfiguration,\
com.john.starter.OtherConfiguration

Good and better —— Integrate IDE Smart tips

When the app starts, it will application.yml The corresponding configuration item in is bound to @ConfigurationProperties On the annotated class instance , So for application developers, the daily job is to modify application.yml Configuration item for . but IDE There is also a lack of intelligent tips for configuration items , That would be inefficient . Luckily Spring Boot We have long been provided with good solutions , It is divided into manual and automatic . For the sake of efficiency, it can be automatic, not manual .

Starter Project work

  1. introduce spring-boot-configuration-processor Dependencies ;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
  1. if src/resources/META-INF/spring-configuration-metadata.json non-existent , Then perform mvn compile Will generate target/classes/META-INF/spring-configuration-metadata.json;
  2. Copy target/classes/META-INF/spring-configuration-metadata.json To src/resources/META-INF/spring-configuration-metadata.json that will do .

Business system project work

  1. introduce spring-boot-configuration-processor Dependencies ;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
  1. IDEA install Spring Assistant plug-in unit , And enable the Enable annotation processing( Check Settings/Build, Execution & Deployment/Compiles/Annotation Processors/Enable annotation processing).

summary

spring-boot-starter It is very suitable for technology accumulation and precipitation of the team , But I want to apply it properly , Not only does it need to go deep Spring Internal principles also need to sort out the business logic . We'll go into it later Spring The kernel thing !

Please quote from :https://www.cnblogs.com/fsjohnhuang/p/13956039.html —— ^_^ Fat boy John

版权声明
本文为[Cubic meter ^Fat John]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云