5

Spring Boot starter principle

Spring Boot divides common development functions into starters, so that when we develop functions, we only need to introduce the corresponding starter, instead of introducing a bunch of dependencies! The starter can be understood as a dependency group, and its main function is to complete the import dependency and initialization configuration . The official starter naming standard provided by Spring is spring-boot-starter-xxx , and the starter naming standard provided by the third party is xxx-spring-boot-starter .

Here we RocketMQ dependent rocketmq-spring-boot-starter to learn the principles of starter.

After the introduction of rocketmq-spring-boot-starter in the project, some related dependencies rocketmq were actually introduced.

In rocketmq-spring-boot there is a class of automatic assembly RocketMQAutoConfiguration , of which I intercepted piece of code, a look.

@Configuration
@EnableConfigurationProperties(RocketMQProperties.class)
@ConditionalOnClass({MQAdmin.class})
@ConditionalOnProperty(prefix = "rocketmq", value = "name-server", matchIfMissing = true)
@Import({MessageConverterConfiguration.class, ListenerContainerConfiguration.class, ExtProducerResetConfiguration.class, RocketMQTransactionConfiguration.class})
@AutoConfigureAfter({MessageConverterConfiguration.class})
@AutoConfigureBefore({RocketMQTransactionConfiguration.class})

public class RocketMQAutoConfiguration {
    private static final Logger log = LoggerFactory.getLogger(RocketMQAutoConfiguration.class);

    public static final String ROCKETMQ_TEMPLATE_DEFAULT_GLOBAL_NAME =
        "rocketMQTemplate";

    @Autowired
    private Environment environment;

    @Bean(destroyMethod = "destroy")
    @ConditionalOnBean(DefaultMQProducer.class)
    @ConditionalOnMissingBean(name = ROCKETMQ_TEMPLATE_DEFAULT_GLOBAL_NAME)
    public RocketMQTemplate rocketMQTemplate(DefaultMQProducer mqProducer,
        RocketMQMessageConverter rocketMQMessageConverter) {
        RocketMQTemplate rocketMQTemplate = new RocketMQTemplate();
        rocketMQTemplate.setProducer(mqProducer);
        rocketMQTemplate.setMessageConverter(rocketMQMessageConverter.getMessageConverter());
        return rocketMQTemplate;
    }
}
  • @Configuration indicates that this is a configuration class. The method annotated by @Bean in the class is a bean of spring, such as rocketMQTemplate .
  • @EnableConfigurationProperties, enable beans that are @ConfigurationProperties, here is RocketMQProperties .

RocketMQProperties is the attribute that needs to be written in the yml file.

@ConfigurationProperties(prefix = "rocketmq")
public class RocketMQProperties {

    private String nameServer;

    private String accessChannel;

    private Producer producer;

    private Consumer consumer = new Consumer();
}

When the Spring Boot project is started, by default, only @Configuration annotations in the lower-level directories will be scanned, so how does RocketMQAutoConfiguration mentioned in this article scan? In fact, when the project starts, it will load all the spring.factories files in the project, and then load the corresponding configuration classes, so we need to specify only the classes that need to be scanned spring.factories

The principle is understood, and then we simply implement a starter of our own! The main function of this starter is to splice a string to the end of an object!

1. New project

Create a new javatip-spring-boot-starter and introduce the following dependencies

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

2. New configuration class

The properties in the properties file corresponding to the configuration class are javatip.name ;

@ConfigurationProperties(prefix = "javatip")
public class JavatipPorperties {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Three, add a new method of splicing strings

This method is mainly to splice a fixed string for the object

public class StrUt {

    private String name;

    public String strTo(Object object){

        return object +"---"+ getName();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Four, new automatic configuration class

Use the annotation @EnableConfigurationProperties enable the JavatipProperties configuration class

Use the annotation @Configuration with @Bean register a bean object with a concatenated string.

@Configuration
@EnableConfigurationProperties(JavatipPorperties.class)
public class JavatipAutoConfiguration {

    @Autowired
    private JavatipPorperties javatipPorperties;

    @Bean
    public StrUt strut(){
        StrUt strut = new StrUt();
        strut.setName(javatipPorperties.getName());
        return strut;
    }
}

5. New configuration discovery file

New in the resources folder META-INF folder, META-INF create a new configuration file found in the folder spring.factories , and automatically configures the class written to a file.

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.javatip.str.configuration.JavatipAutoConfiguration

Six, package test

Use the mvn install command to package and push the project to the local maven warehouse, then create a new test project and introduce the packaged dependencies.

<dependency>
    <groupId>com.javatip</groupId>
    <artifactId>javatip-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

javatip.name corresponding to the automatically spliced character string in the application.yml file.

javatip:
  name: Java旅途

Then write a test class:

@RestController
public class Test {
    
    @Autowired
    private StrUt strUt;

    @GetMapping("test")
    public String test(){

        String str = strUt.strTo("who are you?");
        return str;
    }
}

After running the test class, the page returned

who are you?---Java旅途

In this way, a simple starter is written. As long as you understand the principle of the starter, it is very simple to implement. The first point is that the starter is equivalent to a dependency group, and the other is that the starter can complete the initial configuration.


Java旅途
1.1k 声望6.1k 粉丝