springboot Make a custom starter

Custom starter

SpringBoot The starter is a very important mechanism to abandon the previous complex configuration, which is a unified and integrated into the starter, the application only needs to introduce starter depend on the maven, SpringBoot will automatically scan the information to load and start the corresponding the default configuration. Let's get rid of the starter handle a variety of dependent libraries, you need to configure plagued by a variety of information. SpringBoot automatically discovers Bean class required by the classpath path, and registered into the vessel IOC. SpringBoot provide a spring-boot-starter module for everyday business applications depend on the development of various scenarios. All of these modules are dependent agreed to follow the custom default configuration, and allows us to adjust these configurations, namely to follow "convention than the configured" concept.

Why Customize starter

In the daily development work, often there will be some independent service configuration module, we often put it into one particular package, and then if another project needs multiplex this function when the code needs to be copied to the hard another project, reintegrated again, more trouble. If we will be independent of the service code module package arranged to work one starter, time multiplexed in dependent and need to be referenced to the pom, SpringBoot us to complete the automatic assembly.

Custom of naming starter

starter SpringBoot provided to spring-boot-starter-xxx named manner. Official recommendations starter customized using xxx-spring-boot-starter naming rules. To distinguish starter SpringBoot eco provided.

 

Steps

1. Create a new spring project

This should Needless to say, using the idea to build in Spring Initializr

The project is structured as follows

 

2.pom Configuration

<?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.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>demo-spring-boot-starter</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

3. Define a mapping entity class configuration information

Package demospringbootstart.demo.properties; 

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

/ ** 
 * @author: VIC 
 * @date: 2020/04/09 15:04 in the Created 
 * @Description: configuration information entity class 
 * / 
// this annotation can be the same prefix configuration information mapped to the entity classes through the configuration item name, here we specify the prefix "demo",
 // you can will "demo" as a prefix to get information about configuration items 
@ConfigurationProperties ( = prefix "Demo" )
 public  class DemoProperties { 

    Private String sayWhat;
     Private String toWho; 

    public String getSayWhat () {
         return sayWhat; 
    } 

    public  void setSayWhat(String sayWhat) {
        this.sayWhat = sayWhat;
    }

    public String getToWho() {
        return toWho;
    }

    public void setToWho(String toWho) {
        this.toWho = toWho;
    }
}

 

4. Define configuration class

package demospringbootstart.demo.config;

import demospringbootstart.demo.properties.DemoProperties;
import demospringbootstart.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author:VIC
 * @Date:Created in 15:35 2020/04/09
 * @Description:配置类
 */
//@Configuration declared as a configuration class, to say nothing of 
@Configuration
 // The annotation to enable support above step @ConfigurationProperties annotation configuration bean,
 // it is to tell springboot support @ConfigurationProperties,
 // or you can also comment on @ConfigurationProperties Add @Configuration and @Component on class identity, the same effect 
@EnableConfigurationProperties (DemoProperties. class )
 // @ConditionalOnProperty @Configuration used to control whether the entry into force, in short, is what we can control @Configuration by yml or properties profile Notes configuration class is in effect. 
@ConditionalOnProperty (prefix = "Demo", name = "isOpen", havingValue = "to true" )
 public  class DemoConfig { 

    @Autowired 
    Private DemoProperties Properties; 

    @Bean 
    public DemoService demoService() {
        return new DemoService(properties.getSayWhat(), properties.getToWho());
    }
}

 

5. Provide a service class

Package Penalty for demospringbootstart.demo.service; 

/ ** 
 * @author: VIC 
 * @date: the Created in 15:28 2020/04/09 
 * @Description: casual implement a service, there's really nothing here, this service is provided to the integrated starter business party calls 
 * / 
public  class the DemoService { 

    public String sayWhat;
     public String toWho; 

    public the DemoService (sayWhat String, String toWho) {
         the this .sayWhat = sayWhat;
         the this .toWho = toWho; 
    } 

    public String the sayHello () {
         return  the this . + sayWhat "!!!" + the this .toWho; 
    } 
}

 

6. New spring.factories file (important)

The spring.factories into the META-INF folder below, spring.factories document reads as follows

# Automated assembly, as to why this configuration file, springboot will go to the assembly Democonfig class configured here, readers can go to look at the implementation springboot SPI mechanism (have time to engage in a blog) 
org.springframework.boot.autoconfigure.EnableAutoConfiguration = demospringbootstart.demo.config.DemoConfig

 

7. Test Use

After the above steps are completed, then simply mvn clean install, customize a starter production is complete

====================== ====================== dividing line

Use custom starter

1. The introduction of starter dependent

<dependency>
    <groupId>demo-spring-boot-starter</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

2. Fill the profile contents

demo.isopen=true
demo.to-who=JACK
demo.say-what=HI

3. Write code to call it and see paragraph

package com.zhaowa.course.design.controller;

import demospringbootstart.demo.service.DemoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

/**
 * @Author:VIC
 * @Date:Created in 15:53 2020/04/09
 * @Description:
 */
@RestController
public class TestController {

    @Resource
    private DemoService demoService;

    @RequestMapping("/")
    public String test(){
        return demoService.sayHello();
    }
}

Guess you like

Origin www.cnblogs.com/vicF/p/12667934.html