The process of automatically configuring RocketMQ-Spring-boot is introduced by SpringBoot. Determine the component version first

<dependency>
    <groupId>org.apache.rocketmq</groupId>
    <artifactId>rocketmq-spring-boot</artifactId>
    <version>2.0.2</version>
    <scope>compile</scope>
</dependency>
Copy the code

Note: For SpringBoot AutoConfiguration to take effect, you need to enable it on the boot class via @enableAutoConfiguration

1. Process analysis

  1. SpringBoot scans all the meta-INF /spring.factories in the classpath when it starts. SpringBoot scans the meta-INF /spring.factories file in the rocketmq-spring-boot.jar.
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.apache.rocketmq.spring.autoconfigure.RocketMQAutoConfiguration
Copy the code
  1. SpringBoot. According to the spring in the factories, I’ll load RocketMQAutoConfiguration class, the class of the specific content is as follows:
package org.apache.rocketmq.spring.autoconfigure; @Configuration @EnableConfigurationProperties(RocketMQProperties.class) @ConditionalOnClass({ MQAdmin.class, ObjectMapper.class }) @ConditionalOnProperty(prefix = "rocketmq", value = "name-server") @Import({ JacksonFallbackConfiguration.class, ListenerContainerConfiguration.class }) @AutoConfigureAfter(JacksonAutoConfiguration.class) public class RocketMQAutoConfiguration { @Bean @ConditionalOnMissingBean(DefaultMQProducer.class) @ConditionalOnProperty(prefix = "rocketmq", value = {"name-server", "Producer.group "}) public DefaultMQProducer DefaultMQProducer (RocketMQProperties RocketMQProperties) { @Bean(destroyMethod = "destroy") @ConditionalOnBean(DefaultMQProducer.class) @ConditionalOnMissingBean(RocketMQTemplate.class) public RocketMQTemplate rocketMQTemplate(DefaultMQProducer mqProducer, ObjectMapper rocketMQMessageObjectMapper)} {/ / specific logic @ Bean @ ConditionalOnBean (RocketMQTemplate. Class) @ConditionalOnMissingBean(TransactionHandlerRegistry.class) public TransactionHandlerRegistry TransactionHandlerRegistry (RocketMQTemplate template)} {/ / specific logic @ Bean (name = RocketMQConfigUtils.ROCKETMQ_TRANSACTION_ANNOTATION_PROCESSOR_BEAN_NAME) @ConditionalOnBean(TransactionHandlerRegistry.class) @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public static RocketMQTransactionAnnotationProcessor transactionAnnotationProcessor specific logic (/ /}}Copy the code
  1. RocketMQAutoConfiguration classes automatically configure the key analysis
  • Configuration: Indicates that this is a Configuration class
  • @ EnableConfigurationProperties (RocketMQProperties. Class) : automatic loading RocketMQProperties. Class class, specific see section 4 for details.
  • @bean decorates multiple methods in a class, or creates beans automatically. Including: DefaultMQProducer RocketMQTemplate, TransactionHandlerRegistry RocketMQTransactionAnnotationProcessor.
  1. The RocketMQProperties class looks like this after removing the methods:
@SuppressWarnings("WeakerAccess") @ConfigurationProperties(prefix = "rocketmq") public class RocketMQProperties { private String nameServer; private Producer producer; public static class Producer { private String group; private int sendMessageTimeout = 3000; private int compressMessageBodyThreshold = 1024 * 4; private int retryTimesWhenSendFailed = 2; private int retryTimesWhenSendAsyncFailed = 2; private boolean retryNextServer = false; private int maxMessageSize = 1024 * 1024 * 4; private String accessKey; private String secretKey; private boolean enableMsgTrace = true; private String customizedTraceTopic = MixAll.RMQ_SYS_TRACE_TOPIC; }}Copy the code

RocketMQProperties is a configuration file for RocketMQ. This file also tells you all the supported configuration items and defaults for RocketMQ. In the third quarter @ EnableConfigurationProperties (RocketMQProperties. Class) role is to make SpringBoot automatically generate the RocketMQProperties objects of a class, And read the contents of the configuration file to the class object for property replication. As can be inferred from the RocketMQProperties content, the format of the configuration file is:

rocketmq: nameServer: nameServer producer: group: sendMessageTimeout:3000 compressMessageBodyThreshold:1024 * 4 retryTimesWhenSendFailed:2 retryTimesWhenSendAsyncFailed:2  retryNextServer:false maxMessageSize:1024 * 1024 * 4 accessKey: accessKey secretKey: secretKey enableMsgTrace:true customizedTraceTopic:RMQ_SYS_TRACE_TOPICCopy the code
  1. Based on the above analysis, we can generally know the general process of SpringBoot automatic configuration. Take Rocketmq-Spring-boot as an example, we do not need to create beans when using rocketmQ-Spring-boot, SpringBoot has automatically generated and configured for us.
@Resource
RocketMQProperties rocketMQProperties;
@Resource
DefaultMQProducer defaultMQProducer;
@Resource
RocketMQTemplate rocketMQTemplate;
@Resource
TransactionHandlerRegistry transactionHandlerRegistry;
@Resource
RocketMQTransactionAnnotationProcessor rocketMQTransactionAnnotationProcessor;
Copy the code

2. To summarize

From the above analysis, you can understand the entire process of SpringBoot automatic configuration. If we need to implement this feature, we need to complete the following steps:

  • Add meta-INF /spring.factories to your jar package
  • Add the path to the file for the classes you want to automatically configure
  • This class is decorated with the @Configuration annotation, and @Bean creates the beans that need to be created
  • If there is configuration file loaded in advance, need to use @ EnableConfigurationProperties (configuration classes. The class).