Custom Starter for SpringBoot Source

**

Write in front:

**

As a developer, compared with the development of Spring and Spring MVC, the biggest feeling of using SpringBook micro framework is that we can finally stop writing so many configuration files. Think back carefully, in our original spring + spring MVC development model, we need to configure data sources in xml files, build sqlSessionFactory factories, create dao implementation class objects, and so on. By SpringBook, we said that most of these configurations are not written manually.

Is SpringBoot powerful enough not to use these things?

Quote a sentence: There is no good time, but someone to carry your weight forward.

For some repetitive configurations, SpringBoot just encapsulates itself. For operations like configuring data sources, we just need to write parameters in application.yml. Springboot uses the parameters in YML to construct data sources through the starter.

Business requirements:

The browser enters a string and the startup class adds prefix and suffix that we write in the configuration file. As follows:

1.application.yml configuration file parameters

server.port=8989
server.context-path=/springboot

com.baizhi.prefix=I Like You---->
com.baizhi.suffix="Do you like me ?

2. Send the request and pass in the name

http://localhost:8989/springboot/test/test?name=bxr

3. Expected results, completion of splicing business

I Like You----> bxr Do you like me ?

Auto sense starter development process:

1. Create development moudle s and introduce dependencies

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.baizhi</groupId>
  <artifactId>spring-boot-starter</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>spring-boot-starter</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  <!--inherit springboot Father Project Arbitration Center: Management jar Version number of-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
  </parent>
  <dependencies>
    <!--Basic dependencies of all starters-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.1.2.RELEASE</version>
    </dependency>
  </dependencies>
  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
    </pluginManagement>
  </build>
</project>

2. Specify the corresponding xxxProperties to define configurable values

package com.baizhi.service;
import org.springframework.boot.context.properties.ConfigurationProperties;
//Add a prefix to the annotation specifying the configuration
@ConfigurationProperties(prefix= "com.baizhi")
public class WelProperties {
    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;
    }
}

3. Define business classes to obtain user configuration information and implement our business scenarios.

package com.baizhi.service;
import org.springframework.beans.factory.annotation.Autowired;
//Define business classes to capture user configuration information and implement our application scenarios
public class WelService {
    @Autowired
    private WelProperties welProperties;
    public WelProperties getWelProperties() {
        return welProperties;
    }
    public void setWelProperties(WelProperties welProperties) {
        this.welProperties = welProperties;
    }
    /*Implementing our business scenario*/
    public String  wel(String name){
        return welProperties.getPrefix()+name+welProperties.getSuffix();
    }
}

4. Define automatic configuration classes and need to place our business class instantiation in containers

package com.baizhi.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* Define automatic assembly classes and place our business class instantiation in containers
* */
@Configuration    //Specify that this class is a configuration class
@ConditionalOnWebApplication     //web applications only take effect
@EnableConfigurationProperties(value = WelProperties.class)  //Let User Properties come into effect and join the container
public class WelConf {
    @Autowired
    WelProperties welProperties;
    @Bean
    public WelService getWelService(){

        WelService welService = new WelService();
        welService.setWelProperties(welProperties);
        return welService;
    }
}

5. Set up the configuration class to make the configuration class effective.

Configuration location: resources - - > META - INF - - > spring. factories

# Auto Configure
#com.baizhi.service.UserConf is our configuration class
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.baizhi.service.WelConf

6. Install the current project into the local warehouse (install means to type the current project into a jar package and import it into the local warehouse), until the end of the custom starter development.

Custom starter test flow:

1. Creating new moudle s and introducing dependencies (web support and custom starters)

    <dependencies>
        <!--Introduce web Support-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!--Introducing a custom starter-->
        <dependency>
            <groupId>com.baizhi</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

2. Write application.yml

server.port=8989
server.context-path=/springboot

com.baizhi.prefix=I Like You---->
com.baizhi.suffix="Do you like me ?

3. Writing Controller

import com.baizhi.service.WelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("test")
public class WelController {

    @Autowired
    private WelService welService;

    @RequestMapping("test")
    public String  wel(String name){

        return welService.wel(name);
    }
}

4. Configure the startup class and start the test through the plug-in

package com.baizhi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootPluginApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringbootPluginApplication.class, args);
    }

}

Final results:

I Like You---->bxr"Do you like me ?

Write it later:

(1) The starter is only used for dependency import, that is, the whole module only needs to import its dependency jar;

(2) Write an automatic configuration module and package it in jar mode.

(3) Starter relies on automatic configuration module. Users only need to introduce starter to realize automatic configuration.

(4) Naming rules for starter modules: XXX (custom starter name) - spring-boot-starter (e.g. mybatis-spring-boot-starter;)

My name is flawed. Don't learn from me.~~~

Keywords: Spring Maven SpringBoot Apache

Added by mothermugger on Tue, 30 Jul 2019 17:19:36 +0300