Sring Boot starter analysis

Here we use mybatis-spring-boot-starter to analyze

Search for spring.factories under mybatis-spring-boot-autoconfigure with Ctrl+Shift+N

Here are the three files circled below:

The core is these three files:

1, spring. Factories

You can see that the parameter is the MybatisAutoConfiguration class circled above.

The Spring. factories file is used to record the name of the bean class that needs to be registered outside the project package and boot springBoot which is the auto-configuration class.

If you don’t understand how to load it, you can read this article](juejin.cn/post/684490…)

2, MybatisProperties properties file to load class, through @ EnableConfigurationProperties ({MybatisProperties. Class}) injected into the spring container, Enables configuration properties to be injected directly into MybatisAutoConfiguration

In MybatisProperties class @ ConfigurationProperties (prefix = MybatisProperties. MYBATIS_PREFIX) defines the prefix.

If you want to configure the mapperLocations attribute of Mybatis, you will add it to application.properties

mybatis.mapperLocations=classpath:mybatis/*.xml
Copy the code

This will specify the location of the MYbatis XML file, Mybatis is the prefix of this MybatisProperties configuration and mapperLocations is one of our MybatisProperties. Class member variables.

MybatisAutoConfiguration automatic configuration class

@ EnableConfigurationProperties (MybatisProperties. Class) introduced MybatisProperties configuration file

You can then instantiate an object using the parameters in the configuration file to complete the creation of myBatis.

The entire starter loading process

“> < p style =” box-sizing: initial; word-wrap: inherit! Important

Hand-write your own starter

1. Create a Properties class

@ConfigurationProperties(prefix=HelloProperties.HELLO_FORMAT_PREFIX)
public class HelloProperties {

    public static final String HELLO_FORMAT_PREFIX="xiaoxin.hello";
    private Map<String,Object> info;

    public Map<String, Object> getInfo(a) {
        return info;
    }

    public void setInfo(Map<String, Object> info) {
        this.info = info; }}Copy the code

Create a service class HelloFormatTemplate

public class HelloFormatTemplate {

    private HelloProperties helloProperties;

    public HelloFormatTemplate(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    public <T> String doFormat(a) {
        returnJSON.toJSONString(helloProperties.getInfo()); }}Copy the code

3. Create the configuration class HelloAutoConfiguration

@EnableConfigurationProperties(HelloProperties.class)
@Configuration
public class HelloAutoConfiguration {

    @Bean
    public HelloFormatTemplate helloFormatTemplate(HelloProperties helloProperties) {
        return newHelloFormatTemplate(helloProperties); }}Copy the code

4. Write the Spring. factories file

Create a meta-INF /spring.factories entry file in the resources directory with the following content

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.xiaoxin.starter.autoconfiguration.HelloAutoConfiguration
Copy the code

5. Deploy it to the local repository

Run the MVN install command to install the vm on the local PC

The test project

Let’s create a New SpringBoot Web project and add dependencies to the POM

<dependency>
    <groupId>com.xiaoxin.starter</groupId>
    <artifactId>format-spring-boot-starter</artifactId>
    <version>1.0.0 - the SNAPSHOT</version>
</dependency>
Copy the code

Then write some configuration information to the application.properties file

xiaoxin.hello.info.name=xiaoxin
xiaoxin.hello.info.learn=SpringBoot Starter
Copy the code

Hello is the starter prefix, info is the starter attribute, and name and learn can be defined as you like

Finally, let’s write a simple controller test

@RestController
public class HelloController {

    @Autowired
    HelloFormatTemplate helloFormatTemplate;

    @GetMapping("/hello")
    public String format(a) {
        returnhelloFormatTemplate.doFormat(); }}Copy the code

HelloFormatTemplate is a custom starter HelloFormatTemplate

Test results:

So we have a custom Spring Boot starter

Source code address: github.com/LuDarwin/xi…