spring boot custom starter

spring boot uses starter to solve many configuration problems, but how does it solve these problems

Here is a simple example to see how starter sets the default configuration

1, Build starter project

The project naming specification is: custom name spring boot starter

Let's take a look at my last directory structure

 

1. Modification pom.xml file

<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>org.elvin</groupId>
  <artifactId>my-spring-boot-starter</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>my-spring-boot-starter</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
      <version>1.5.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

It's just spring boot autoconfigure

I commented out the main method in the App file, which is not used here

2. Configure the receiving file corresponding to the attribute

package org.elvin;

import org.springframework.boot.context.properties.ConfigurationProperties;/**
 * author: Elvin
 * Date: 2017/12/12 14:51
 * Description:
 */
@ConfigurationProperties(prefix = "hello")
public class HelloServiceProperties {
    //Default configuration content
    private static final String MSG = "world";

    private String msg = MSG;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

3. External Service

package org.elvin;

/**
 * author: Elvin
 * Date: 2017/12/12 14:55
 * Description:
 */
public class HelloService {

    private String msg;

    public String sayHello(){
        return "Hello " + msg;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

4. Association between external service and configuration corresponding file

package org.elvin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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: Elvin
 * Date: 2017/12/12 14:59
 * Description:
 */
@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "hello", value="enabled", matchIfMissing =true )
public class HelloServiceAutoConfiguration {

    @Autowired
    private HelloServiceProperties helloServiceProperties;

    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setMsg(helloServiceProperties.getMsg());
        return helloService;
    }
}

5. starter configuration: spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.elvin.HelloServiceAutoConfiguration

After that, pack it into maven library through mvn clean install

 

2, spring boot project use

Create a new spring boot project and select web

Directory structure:

Let's take a look at the references pom.xml

        <dependency>
            <groupId>org.elvin</groupId>
            <artifactId>my-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

Take a look at HelloController again

package org.elvin.learn.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import  org.elvin.*;

/**
 * author: Elvin
 * Date: 2017/12/12 15:34
 * Description:
 */
@RestController
@RequestMapping("hello")
public class HelloController {

    @Autowired
    private HelloService helloService;

    @RequestMapping("index")
    public String index(){
        return helloService.sayHello();
    }

}

The HelloService here is the one in the custom starter

1. Result: if not configured, it should output hello world

2. In the configuration file, add hello.msg=hahahahahah

This example is very simple. It just shows the main process. The other is the logic judgment of each plug-in

reference material:

Spring Boot, the subverter of Java EE development

Tags: Java Spring Maven Apache xml

Posted on Sun, 31 May 2020 12:31:13 -0400 by tibberous