This is the ninth day of my participation in the First Challenge 2022. For details: First Challenge 2022.

Define the starter

First we need to define our own starter.

Import dependency sumartifactIddefine

Spring-boot-starter -{name} spring-boot-starter-web It is recommended that the unofficial Starter be named in the {name}-spring-boot-starter format. For example: mybatis-spring-boot-starter, dubo-spring-boot-starter.

< modelVersion > 4.0.0 < / modelVersion > < groupId > com. The SSM < / groupId > < artifactId > example - spring - the boot - starter < / artifactId > 1.0 the SNAPSHOT < version > < / version > < dependencies > < the dependency > < groupId > org. Springframework. Boot < / groupId > <artifactId>spring-boot-autoconfigure</artifactId> </dependency> </dependencies>Copy the code

Add the configuration

Create a spring.factories file under Resources/meta-INF. For reference:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.ssm.xyz.config.ExampleAutoConfigure
Copy the code

Where do spring.factories read? You may have this question.

The @SpringBootApplication annotation defined on our spring-Boot startup class contains the @enableAutoConfiguration annotation and then the annotation internally contains

@ Import ({AutoConfigurationImportSelector. Class}) through the AutoConfigurationImportSelector read all dependent jar The annotation class configured in the spring.factories file.

The key is the @import annotation. This annotation can either import the Selector or import the Configruration class directly.

Business implementation

As an implementation, our custom ExampleService can wrap a string to add a fixed prefix, essentially replacing a StringUtil tool method.

public class StringUtil {
  public static String wrap(String str) {return "PrefixXXX" + str;}
}
Copy the code

ExampleAutoConfigure is used to initialize beans ExampleServiceProperties and ExampleService within the component: ExampleServiceProperties ExampleService

package com.ssm.xyz.config; @Configuration @ConditionalOnClass(ExampleService.class) @EnableConfigurationProperties(ExampleServiceProperties.class) public class ExampleAutoConfigure { @Autowired private ExampleServiceProperties properties; @Bean @ConditionalOnMissingBean @ConditionalOnProperty(prefix = "example.service",value = "enabled",havingValue = "true") ExampleService exampleService (){ return new ExampleService(properties.getPrefix()); }}Copy the code

The configuration file accepts, and we’re using the @ConfigurationProperties method to read the configuration, and it reads the configuration of the Example. service prefix and if you set it in the properties file, Example. Service. Prefix = zhangsan Hello

@ConfigurationProperties("example.service")
public class ExampleServiceProperties {

    private String prefix;
}
Copy the code

ExampleService#wrap() ExampleService#wrap() this method is the core code that implements the core logic of the component

public class ExampleService { private String prefix; public ExampleService(String prefix { this.prefix = prefix; } public String wrap(String word) { return prefix + word + suffix; }}Copy the code

Use the starter

Above we have finished the development of the custom starter, now we just need to import our starter and then our Spring-boot will automatically read the configuration when we start up, initialize it, and inject it into the place we use. We can use it directly during business development.

Importing component dependencies

Before using starter, you need to import dependencies (before importing components, you need to deploy the defined components in advance, otherwise you may not be able to rely on them, which is also a common mistake) :

<dependency> <groupId>com.ssm</groupId> <artifactId>example-spring-boot-starter</artifactId> < version > 1.0 - the SNAPSHOT < / version > < / dependency >Copy the code

Test class file

For convenience and simplicity, I will put the Controller and Application together and expose an /input interface to wrap the incoming Word. The code operation is as follows:

@SpringBootApplication @RestController public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Autowired private ExampleService exampleService; @GetMapping("/input") public String input(@RequestParam("word") String word){ return exampleService.wrap(word); }}Copy the code

Access the address: http://127.0.0.1:8080/input? word=word! The output

hello word!
Copy the code

Summary of Spring Boot custom components

Component definition tradeoffs if we are a locally running application such as reverse parsing through an IP library to address information, or sending an email message push, etc. I think we can encapsulate these capabilities into a service and provide them to the business side by encapsulating client-sarter. This will make service segmentation clearer, and ensure that providing components does not add to the running burden of the original service, keeping microservices micro.

There is also the issue of component granularity, which depends on specific business scenarios and requirements, which feels like a matter of opinion.

Reference documentation

  • spring.io
  • Blog.csdn.net/lubin_jiang…