Custom SpringBoot series of starter hands-on tutorials

Custom SpringBoot series of starter hands-on tutorials

Springboot have provided a lot of starter, starter translated scene can be understood as the starter, the so-called scene launcher equipped with automatic configuration, and so a corresponding service module of the project, when there is a need to introduce direct projects will be, such as the need to use rabbitMQ, introduced directly into the spring-boot-starter-activemq either, details can refer Springboot official document describes about the starters

Check the official documentation, can be found in the following naming convention:
Here Insert Picture Description

Which means SpringBoot named the official starter to be defined as a spring-boot-starter- *, a custom or third-party terribly named thirdpartyproject-spring-boot-starter

  • Official namespace
    • Prefix: "spring-boot-starter-"
    • Mode: spring-boot-starter- module name
    • 举例:spring-boot-starter-web、spring-boot-starter-actuator、spring-boot-starter-jdbc
  • Custom namespace
    • Suffix: "- spring-boot-starter"
    • Mode: -spring-boot-starter module
    • Example: mybatis-spring-boot-starter

ok, SpringBoot can customize some starter to use, it can be used to facilitate project development, the blog description by way of example:

Create a new project without excessive dependence SpringBoot, spring-boot-starter must be introduced, others can Junit can be removed, application type, and so are not recommended to create a new Empty project, and then introduced into the way of engineering to create the corresponding configuration

pom.xml Reference:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.springboot</groupId>
    <artifactId>custom-spring-boot-starter-autoconfigurer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>custom-spring-boot-starter-autoconfigurer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

    </dependencies>


</project>

Imitate other starter, the new configuration class Properties

package com.example.springboot.starter;

import org.springframework.boot.context.properties.ConfigurationProperties;


@ConfigurationProperties(prefix = "custom.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;
    }
}

Business writing test class:

package com.example.springboot.starter.service;

import com.example.springboot.starter.HelloProperties;

public class HelloService {

    HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

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

    public String sayHello(String name){
        return helloProperties.getPrefix()+"-"+name+helloProperties.getSuffix();
    }
}

Write to automatically configure custom classes:

package com.example.springboot.starter;

import com.example.springboot.starter.service.HelloService;
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;


/**
 * <pre>
 *  自定义的自动配置类
 * </pre>
 *
 * <pre>
 * @author nicky.ma
 * 修改记录
 *    修改后版本:     修改人:  修改日期: 2020/01/02 14:31  修改内容:
 * </pre>
 */
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}

Must create a new META-INF / spring.factories, and then add the following configuration, automatic configuration class can be scanned into the specific reasons can refer to my previous study notes Source: Springboot source learning columns

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.example.springboot.starter.HelloServiceAutoConfiguration

example is relatively simple, starter has been created, and then need to create a web project to practice, as maven configuration, the introduction of custom-spring-boot-starter-autoconfigurer dependent

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.example.springboot</groupId>
            <artifactId>custom-spring-boot-starter-autoconfigurer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>

Interface to create a new test:

@Autowired
    HelloService helloService;

@GetMapping(value = {"/sayHello/{name}"})
    @ResponseBody
    public String sayHello(@PathVariable("name")String name){
        return helloService.sayHello(name);
    }

After running the project, the return link: http: // localhost: 8082 / web / sayHello / testname

Download example code: GitHub download link

Guess you like

Origin www.cnblogs.com/mzq123/p/12141938.html