Spring Boot: Custom starter

In Spring Boot, the most commonly used is the starter. Starter can be understood as a pluggable plug-in. For example, if you want to use jdbc plug-in, you can use Spring Boot Starter jdbc. As the version goes on, the starter family grows. In the traditional Maven project, some layers and components are usually divided into modules to manage, so as to rely on each other for reuse. In the Spring Boot project, we can create a custom Spring Boot Starter to achieve this purpose.

1, How starter works

1. Spring boot scans the jar packages that the project depends on at startup, looking for the jar package containing the search spring.factories file

2. Load autoconfigure class according to spring.factories configuration

3. According to the conditions of @ Conditional annotation, automatically configure and inject Bean into spring context

2, Custom starter

1. IDEA creates an empty project

2. Add two module s, one is automatic configuration (maven project) and the other is initiator (springboot project), which depends on automatic configuration.

3. Project structure

4. Internal code

(1)spring-boot-starter-autoconfigurer module

package com.springboot.starter;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
/**
 * @author:Su Xiao
 * @date:2020/2/9
 * @desc:
 */
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAuto {
    @Autowired
    HelloProperties helloProperties;
 
    @Bean
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}
package com.springboot.starter;
 
import org.springframework.boot.context.properties.ConfigurationProperties;
 
/**
 * @author:Su Xiao
 * @date:2020/2/9
 * @desc:
 */
@ConfigurationProperties(prefix = "springboot.hello")
public class HelloProperties {
    private String name;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
}
package com.springboot.starter;
 
/**
 * @author:Su Xiao
 * @date:2020/2/9
 * @desc:
 */
public class HelloService {
 
    HelloProperties helloProperties;
 
    public String hello() {
        return "hello" + helloProperties.getName();
    }
 
    public HelloProperties getHelloProperties() {
        return helloProperties;
    }
 
    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }
}

spring.factories file:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.springboot.starter.HelloAuto

POM file

<?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.springboot</groupId>
	<artifactId>spring-boot-starter-autoconfigurer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
 
	<name>spring-boot-starter-autoconfigurer</name>
	<description>Demo project for Spring Boot</description>
 
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
 
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>
 
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
 
	</dependencies>
 
</project>

(2) springbootstarter:

The initiator is relatively simple, only pom file, and the introduction of automatic configuration is enough.

<?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>wms</groupId>
    <artifactId>springboot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.springboot</groupId>
            <artifactId>spring-boot-starter-autoconfigurer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
 
</project>

5. Test, create a new springboot project, and introduce the initiator in the pom file

<dependency>
    <groupId>springboot</groupId>
    <artifactId>springboot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

application.properties add configuration item

springboot.hello.name=chenzz

Test controller:

package com.springboot.teststarter;
 
import com.wms.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @author:Su Xiao
 * @date:2020/2/9
 * @desc:
 */
@RestController
public class controller {
 
    @Autowired
    HelloService helloService;
 
    @GetMapping("hello")
    public String hello(){
        return helloService.hello();
    }
}

5. Browser access

Tags: Programming Spring SpringBoot Maven Apache

Posted on Sun, 09 Feb 2020 10:33:04 -0800 by allanonschick