Custom starter in SpringBoot project

Posted by Webxplosion on Tue, 04 Jan 2022 03:54:44 +0100

In the springboot project, we often see a lot of starter dependencies. These starters integrate relevant jar packages, especially the control of relevant jar versions, so as to avoid version conflicts. A starter can load the relevant dependencies, which provides great convenience for us to develop web projects.

SpringBoot starter mechanism

The starter in SpringBoot is a very important mechanism. It can abandon the previous complicated configuration and integrate it into the starter. The application only needs to introduce the starter dependency in maven, and SpringBoot can automatically scan the information to be loaded and start the corresponding default configuration. Starter lets us get rid of the trouble of processing various dependent libraries and configuring various information. SpringBoot will automatically find the required Bean through the class under the classpath path and register it into the IOC container. SpringBoot provides spring boot starter dependency modules for various scenarios of daily enterprise application development. All these dependent modules follow the conventional default configuration and allow us to adjust these configurations, that is, follow the concept of "Convention is greater than configuration".

For example, if we want to introduce redis into springboot, we need to introduce the following into pom:

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

This is actually a starter. In short, starter is an external project. We can introduce it into the current spring boot project when we need to use it.

Why customize the starter

In our daily development work, there are often some configuration modules that are independent of the business. We often put them under a specific package. Then, if another project needs to reuse this function, we need to hard copy the code to another project and re integrate it. It is extremely troublesome. If we package these function configuration modules that can be independent of the business code into starter s, we only need to reference the dependencies in the pom when reusing them, and then SpringBoot will complete the automatic assembly for us, which is very easy.

Case of customizing starter

The following cases are some of the scenarios encountered in development

1. Dynamic data source.

2. Login module.

3. Log facet is realized based on AOP technology.

........

Custom starter naming rules

The starter provided by SpringBoot is named spring boot starter XXX.

It is officially recommended to use XXX spring boot starter naming rules for custom starters. To distinguish the starters provided by the SpringBoot ecology.

Custom starter code implementation

The whole process is divided into two parts: Custom starter; Using starter

1. Custom starter

1) Create a new maven jar project named zdy spring boot starter. Import dependencies:

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

2) Writing JavaBeans

@EnableConfigurationProperties(SimpleBean.class)
@ConfigurationProperties(prefix = "simplebean")
public class SimpleBean {
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "SimpleBean{" +
            "id=" + id +
            ", name='" + name + '\'' +
        '}';
    }
}

3) Write configuration class MyAutoConfiguration

@Configuration
public class MyAutoConfiguration {
    static {
        System.out.println("MyAutoConfiguration init....");
    }
    @Bean
    public SimpleBean simpleBean(){
        return new SimpleBean();
    }
}

4) Under resources, create / meta-inf / spring factories

Note: META-INF is a manually created directory, spring Factories is also a manually created file in which you configure your own automatic configuration classes

In spring Add the following configuration content to the factories: that is, the full path of the configuration class, which can be modified according to your actual situation:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.lagou.config.MyAutoConfiguration

The above sentence means that when SpringBoot starts, it will load our simpleBean into the IOC container. This is actually a deformed SPI mechanism.

2. Create a new springboot project and use a custom starter,

1) Import dependencies for custom starter s

<dependency>
    <groupId>com.lagou</groupId>
    <artifactId>zdy-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

2) Configure attribute values in the global configuration file

simplebean.id=1
simplebean.name=custom starter

3) Writing test methods

//Test custom starter
@Autowired
private SimpleBean simpleBean;
@Test
public void zdyStarterTest(){
    System.out.println(simpleBean);
}

   

Topics: Java Maven Spring Spring Boot