After analyzing the source code and the process of automatic assembly, you can try to customize a starter to play!

The process of automatic assembly

SpringBoot- Static resource loading – source code

SpringBoot – Web – first

instructions

The launcher module is an empty JAR file that provides only ancillary dependency management, which may be used for autowire or other class libraries;

Nomenclature:

Official name:

  • The prefix: spring – the boot – starter – XXX
  • For example: spring – the boot – starter – web…

Custom naming:

  • xxx-spring-boot-starter
  • For example: mybatis – spring – the boot – the starter

Writing an initiator

1. Import autoconfigure dependencies into our starter!

<! -- Initiator -->
<dependencies>
    <! -- Introducing automatic configuration module -->
    <dependency>
        <groupId>com.kuang</groupId>
        <artifactId>kuang-spring-boot-starter-autoconfigure</artifactId>
        <version>0.0.1 - the SNAPSHOT</version>
    </dependency>
</dependencies>
Copy the code

2. Delete all the extra files from the Autoconfigure project, leaving only one starter in the Pom. This is the basic configuration of all the initiators!

3. We write our own service

public class HelloService {

    HelloProperties helloProperties;

    public HelloProperties getHelloProperties(a) {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    public String sayHello(String name){
        returnhelloProperties.getPrefix() + name + helloProperties.getSuffix(); }}Copy the code

4. Write the HelloProperties configuration class

/ / prefix ZWT. Hello
@ConfigurationProperties(prefix = "zwt.hello")
public class HelloProperties {

    private String prefix;
    private String suffix;

    public String getPrefix(a) {
        return prefix;
    }

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

    public String getSuffix(a) {
        return suffix;
    }

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

5. Write our auto-configuration class and inject beans, test!

@Configuration
@ConditionalOnWebApplication // The Web application takes effect
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService(a){
        HelloService service = new HelloService();
        service.setHelloProperties(helloProperties);
        returnservice; }}Copy the code

6, Write your own meta-INF \spring.factories in Resources

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zwt.HelloServiceAutoConfiguration
Copy the code

7. After writing, you can install it into the Maven repository! (install)

Create a new project to test our self-written launcher

1. Create a SpringBoot project

2, import our own written initiator

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

3, write a HelloController to test our own write interface!

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @RequestMapping("/hello")
    public String hello(a){
        return helloService.sayHello("zxc"); }}Copy the code

4. Write the configuration file application.properties

zwt.hello.prefix="ppp"
zwt.hello.suffix="sss"
Copy the code