SprinhgBoot2 ---- custom launcher

Posted by jerrylouise on Tue, 28 Dec 2021 13:27:28 +0100

preface

Note: springboot officially provides us with many initiators, such as elastic search, aop, redis, etc

However, in the actual development, the businesses of different companies may be different, and a general and exclusive starter needs to be customized to meet the internal use of the company and improve the development efficiency.

This article will introduce how to customize the demo process of implementing a launcher.

Project structure, mode.


Empty project: mystarter (used to put the launcher and autoconfiguration module project together)

There are two module s:

1: Launcher DHY Hello spring boot starter

Function: only used for dependency import (the initiator depends on the automatic configuration module, so that external projects can directly reference the initiator)

Naming conventions:

The official springboot initiator is spring boot starter XXX, such as spring boot starter JDBC

Our customized launcher: XXX spring boot starter, such as hello spring boot starter

2: Automatic configuration module DHY Hello spring boot starter autoconfigurer

Function: specifically implement the business logic of the initiator

Naming conventions:

XXX-spring-boot-starter-autoconfigurer

XXX should be consistent with XXX of the starter!

starter startup principle

Starter POM introduces autoconfigurer package

To customize Starter

First create an empty project

Create two modules in the empty project, an initiator module and an autoconfiguration module

The initiator module can directly create a maven project, and the automatic configuration module can create a spring project

Project configuration

1: Configure initiator dependency (add dependency on auto configuration module item in initiator configuration file)

    <dependencies>
        <dependency>
            <groupId>com.dhy</groupId>
            <artifactId>dhy-hello-spring-boot-starter-auotconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

2: Configure autoconfiguration module project dependencies


Note: Here we leave only the most basic springboot support for starter in the dependencies

Plug in references and web dependencies are removed

Then we remove the web dependency and need to clean up the directory structure


Delete the test directory, configuration files, and springboot startup classes, which are not required

3. Write the business logic in the automatic configuration module


service:

/*
* Don't put it in the container by default
* */
public class service
{
    @Autowired
    HelloProperties helloProperties;
    public String sayHello(String userName)
    {
        return helloProperties.getPrefix()+userName+helloProperties.getSuffix();
    }
}

HelloServiceAutoConfiguration:

@Configuration
//Put HelloProperties into the container and bind with the corresponding configuration file
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration
{
    //The container does not have this component before it needs to be placed
    @ConditionalOnMissingBean(service.class)
    //Put the return value into the container
    @Bean
    public service Myservice()
    {
        return new service();
    }
}

HelloProperties:

//Bound with the attribute starting with the corresponding prefix in the main configuration file
@ConfigurationProperties("dhy.hello")
public class HelloProperties
{
    private String prefix;
    private  String suffix;

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

4. Create a scan configuration for XXXAutoConfiguration

Because springboot will scan the project and all project dependent jar packages during project startup, and the scanning path is spring.inf in META-INF directory under the classpath factories

Configure and read all interceptors, filters, automatic configuration xxautoconfiguration, etc

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
 com.dhy.atuoConfiguration.HelloServiceAutoConfiguration   

technological process:

Other springboot projects refer to the initiator, because the initiator depends on the automatic configuration module, and then it will also scan the spring.inf directory in the META-INF directory under the classpath of the automatic configuration module factories

The HelloServiceAutoConfiguration configuration class will be obtained, and then the HelloService object returned by the helloService() method will be created and registered in the ioc container by the @ Bean annotation, so that the HelloService object can be used in the springboot project through the @ Autowired annotation.

The principle of root springBoot automatic configuration is the same. Only when the user does not put his own customized HelloService in the container will he put the corresponding HelloService object in the container

5. Package and introduce the starter on other items

Use maven's install to install the automatic configuration module project and the launcher project into your local maven warehouse in sequence

(it should be that the initiator depends on the automatic configuration module, so install the automatic configuration module first and then the initiator)

(if your initiator is for the development team, you'd better install the configuration module project and initiator project into the corresponding maven private server warehouse, so that other projects can reference the initiator directly.)


6. Test use

You only need to add initiator dependencies to the springboot project

  <dependency>
            <groupId>com.dhy</groupId>
            <artifactId>dhy-hello-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

Write a test login:

@RestController
public class UsrController
{

    @Autowired
    service s;
    @GetMapping("/show")
    public String show()
    {
        return s.sayHello("Huyou ");
    }
}

To configure related prefix attributes in the global configuration file:

dhy:
  hello:
   prefix: Hello
   suffix: And children