编程知识 cdmana.com

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

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]所创,转载请带上原文链接,感谢

Scroll to Top