Pretty boy, is spring boot still in Baidu search configuration? The old driver taught you a trick!!!

Keywords: Java Spring Mybatis SpringBoot Redis

Guide reading

  • Recently, chen company is busy, in order to ensure the high quality of the article, it may take two days to change, here Chen said sorry first!!!

  • Yesterday, a friend asked me how to integrate Redis with SpringBoot. He said that Baidu Google search is not reliable. I was shocked. So you integrated spring boot with Internet search configuration?

  • Today, Chen let his little friends get rid of the problem of configuration. Don't go to the Internet to find it again. Try to configure it yourself, and you will have a stronger sense of achievement!!!

Open and close

1. How to configure data source parameters? How do you know what properties to set in the yml properties file?

  • Chen uses Alibaba's DruidDataSource. The first step of SpringBoot's integration of anything needs to start with automatic configuration classes, as follows:
@Configuration
@ConditionalOnClass(DruidDataSource.class)
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
@EnableConfigurationProperties({DruidStatProperties.class, DataSourceProperties.class})
@Import({DruidSpringAopConfiguration.class,
    DruidStatViewServletConfiguration.class,
    DruidWebStatFilterConfiguration.class,
    DruidFilterConfiguration.class})
public class DruidDataSourceAutoConfigure {}
  • @EnableConfigurationProperties in combination with @ ConfigurationProperties will make a property configuration class effective, which can be configured directly in application.properties. So how to configure Druid parameters must be in DruidStatProperties and DataSourceProperties. No more details here. Please check it by yourself.

  • Here's the first summary: spring boot integrates everything. Almost all the properties that need to be configured are the property classes specified in the auto configuration class or the @ EnableConfigurationProperties annotation on the @ Import injected configuration class.

2. Why do I need to re integrate Mybatis when setting up a dynamic data source? Isn't Mybatis spring boot starter a starter that integrates everything? Just configure it in the yml file?

  • For the same routine, all the answers are in the auto configuration class. The source code is as follows:
@org.springframework.context.annotation.Configuration
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
@ConditionalOnSingleCandidate(DataSource.class)
@EnableConfigurationProperties(MybatisProperties.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class MybatisAutoConfiguration implements InitializingBean {}
  • Obviously easy to see, what a striking annotation @ conditional on single candidate, what does it mean? This annotation means that there is a single specified candidate object in the IOC container, and the configuration class will take effect. That is to say, only one object of type DataSource in the container, MybatisAutoConfiguration, will take effect. However, when we integrate the dynamic data source, we specify multiple datasources and inject them into the IOC container, so all the contents of the MybatisAutoConfiguration class will not take effect Take effect. Of course, you need to reconfigure Mybatis.
  • There are many annotations similar to @ Conditionalxxx annotation in SpringBoot, all of which determine that the configuration will take effect under specified conditions. More notes are shown in the figure below. What do you mean? It's not the focus of this article. Look at the document yourself:

  • Sum up the second item: spring boot integrates everything. Be sure to pay attention to @ Conditionalxxxx annotation to determine the conditions under which various configurations in the configuration class take effect.

3. Why do you need to reconfigure the transaction manager in Java config mode? Didn't you use @ EnableTransactionManagement to open it directly? Can you use it without configuration?

  • The source code of the old routine is as follows:
@Configuration
@ConditionalOnClass({ JdbcTemplate.class, PlatformTransactionManager.class })
@AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)
@EnableConfigurationProperties(DataSourceProperties.class)
public class DataSourceTransactionManagerAutoConfiguration {
    
    //Static configuration class. Only if there is only one candidate object DataSource in the IOC container, this configuration class will take effect
    @Configuration
	@ConditionalOnSingleCandidate(DataSource.class)
	static class DataSourceTransactionManagerConfiguration {

		private final DataSource dataSource;

		private final TransactionManagerCustomizers transactionManagerCustomizers;

		DataSourceTransactionManagerConfiguration(DataSource dataSource,
				ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
			this.dataSource = dataSource;
			this.transactionManagerCustomizers = transactionManagerCustomizers.getIfAvailable();
		}

		@Bean
		@ConditionalOnMissingBean(PlatformTransactionManager.class)
		public DataSourceTransactionManager transactionManager(DataSourceProperties properties) {
			DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(this.dataSource);
			if (this.transactionManagerCustomizers != null) {
				this.transactionManagerCustomizers.customize(transactionManager);
			}
			return transactionManager;
		}

	}
}
  • From the source code of the auto configuration class, it is obvious that @ ConditionalOnSingleCandidate will not be explained much. Like the second problem, only one candidate object in the ioc container, DataSource, will take effect. Obviously, there must be many multiple data sources, not many explanations.
  • The summary of this problem is the same as the second one. Pay attention to the @ conditionalxxx annotation.

4. Why does the integration of transaction manager and Mybatis do not need to be rewritten after adding a @ Primary to the dynamic data source, and automatic configuration can be used directly?

  • The second and third questions have been answered, and the general solution ideas should be known by the small partners.

  • To solve this problem, you must understand the real function of @ ConditionalOnSingleCandidate annotation. The document is as follows:

Conditional that only matches when a bean of the specified class is already contained in the BeanFactory and a single candidate can be determined.
The condition will also match if multiple matching bean instances are already contained in the BeanFactory but a primary candidate has been defined; essentially, the condition match if auto-wiring a bean with the defined type will succeed.
The condition can only match the bean definitions that have been processed by the application context so far and, as such, it is strongly recommended to use this condition on auto-configuration classes only. If a candidate bean may be created by another auto-configuration, make sure that the one using this condition runs after.
  • If there is a single candidate in BeanFactory, the criteria match or there are multiple candidates, but there is a primary candidate, the criteria match.
  • OK, I see. The main candidate is the @ Primary annotation. It's so easy. As long as you add @ Primary annotation to the dynamic data source to specify the main candidate, all problems will be solved.

summary

  • Through the analysis of the above problems, I believe that the small partners roughly know how to integrate with spring boot and how to specify the correct configuration. Chen here to summarize.

    • Find the corresponding auto configuration class. What is the unknown configuration class? The names of approximate configuration classes are all xxxAutoConfiguration.
    • Note the @ Import annotation, which is often used to inject configuration classes or an object directly. See the usage of official documents for details.
    • Note the @ EnableConfigurationProperties annotation on the auto configuration class or the configuration class introduced by @ Import. Often, the property configuration class specified in the annotation is the configuration that needs to be specified in the application.properties file.
    • Note the @ Conditionalxxxx annotation, which explicitly specifies the configuration class or the conditions for Bean injection. Only under its specific conditions can the configuration class take effect and the injected Bean take effect (@ Bean).
    • Note @ autoconfiguraxxxx. This annotation specifies the order in which the autoconfiguration classes take effect. This is very important!!!!
  • If you think the author writes well, please pay attention to it!!!

Posted by wilburforce on Sun, 22 Mar 2020 01:17:30 -0700