Developing a Spring Boot Starter!

In the previous article, we have learned the basic process of automatic configuration of a starter, in this summary we will reproduce on a process to achieve a custom starter.

First to analyze the needs of starter:

  • Add custom in the project starter dependent, automatically loads the starter in the Bean in Spring;
  • Load the specified configuration from the application.properties

Create a project

  1. First create a project named the starter.
<?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>top.ninwoo</groupId>
    <artifactId>demo-starter</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
    </dependencies>
</project>
  1. Creating a META-INF directory in resources and create a directory in spring.factories. In this configuration, we have provided only a EnableAutoConfiguration item, and is provided corresponding to only one class DemoAutoConfig configuration.
org.springframework.boot.autoconfigure.EnableAutoConfiguration=top.ninwoo.config.DemoAutoConfig  
  1. Creating DemoAutoConfig configuration class

    package top.ninwoo.config;
    
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @EnableConfigurationProperties(DemoStarterProperties.class)
    public class DemoAutoConfig {
    
        @Bean
        DemoBean demoBean() {
            return new DemoBean();
        }
    }

    This configuration class, we mainly use the @Configuration and @EnableConfigurationProperties two notes. @EnableConfigurationProperties enable a ConfigurationProperties.

  2. Creating ConfigurationProperties corresponding DemoStarterProperties

    package top.ninwoo.config;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    @ConfigurationProperties(prefix = "top.ninwoo.demo")
    public class DemoStarterProperties {
    
        private String name = "default";
        private int age = 0;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }

    Create a ConfigurationProperties class. This principal is used to read from the configuration item application.properties and automatically set to the corresponding field.

  3. Create a test with Bean, and use the information ConfigurationProperties class.

    At first there is a confusion here, I do not know how to use this ConfigurationProperties class. But in the spring is the most common Bean, we can safely guess by @ConfigurationProperties annotation class will automatically automatically create a Bean in Spring container. And when we use, you can use the information ConfigurationProperties class by common bean injection method. So, we create such a test Bean

    package top.ninwoo;
    
    import javax.annotation.Resource;
    
    public class DemoBean {
        @Resource
        DemoStarterProperties properties;
    
        public String getName() {
            return properties.getName();
        }
    
        public String getAge() {
            return getAge();
        }
    }
    

    Bean while creating a @Bean use annotations in DemoAutoConfig in.

Here, our starter now created. By mvn package, or create different sub-projects of the same parent Module way, we can test the starter is in effect.

Create a test class

Test class uses a spring boot web project to complete, mainly to create a RestController, and get tested and starter Spring Bean bean names registered in the context through RestController.

pom.xml

<?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>top.ninwoo</groupId>
    <artifactId>springboot-demo</artifactId>
    <version>1.0.0</version>

    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>top.ninwoo</groupId>
            <artifactId>demo-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

In the pom file, we add a starter just realized.

RestController:

@RestController
public class IndexController implements ApplicationContextAware {

    ApplicationContext ctx = null;

    @Resource
    DemoBean demoBean;

    @RequestMapping("/getList")
    public String[] getBeanNames() {
        return ctx.getBeanDefinitionNames();
    }

    @RequestMapping("/getDemoBean")
    public String demoBean() {
        return demoBean.getName();
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ctx = applicationContext;
    }
}

SpringBoot startup class MainApp:

package top.ninwoo;

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

@SpringBootApplication
public class MainApp {

    public static void main(String[] args) {
        SpringApplication.run(MainApp.class, args);
    }
}

We can see that, compared to normal a web project, we just added a dependence, but did not modify the startup class.

test

Access 127.0.0.1:8080/getList interface, we can see the last few bean Names are:

...,"top.ninwoo.config.DemoAutoConfig","demoBean","top.ninwoo.demo-top.ninwoo.config.DemoStarterProperties"]

This proves that our starter by injecting dependence has been created starter configuration class Bean in the context of the Spring.

When not provided application.properties, direct access http://127.0.0.1:8080/getDemoBean, Bean instance can be acquired in a default test parameters default.

Add application.properties:

top.ninwoo.demo.name=joliu

Restart the project, which access the interface again, the parameters found in the test class instances used Bean attribute corresponding installation has been set, returned joliu.

summary

Here, we can say that have learned to develop a SpringBoot Starter most basic processes, we can try to develop such a starter in our daily projects.

Guess you like

Origin www.cnblogs.com/NinWoo/p/11305650.html