SpringBoot integration SpringBoot source code analysis of the three framework source code analysis of SSM

Spring Boot source code analysis

A .Spring Boot project started inlet flow analysis

  There is a @SpringBootApplication the main entrance to start the thread Spring Boot project (@Configuration + @EnableAutoConfiguration + @ComponentScan) notes, indicating the main configuration class, let the program automatically identify and make the necessary configuration

  When the project starts, the first automatic configuration, the source code is as follows: 

--- has an interface class in the package org.springframework.boot.autoconfigure:   AutoConfigurationImportSelector

public class AutoConfigurationImportSelector
        implements ... {
         ...try {
            AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
                    .loadMetadata(this.beanClassLoader);
            AnnotationAttributes attributes = getAttributes(annotationMetadata);
            List<String> configurations = getCandidateConfigurations(annotationMetadata,
                    attributes);
            ...
        }
        catch (IOException ex) {
            throw new IllegalStateException(ex);
        }
    }
}

  By default resource SpringFactoriesLoader scanning path (source below), returns  List <String> the Configurations (my project, 97 configuration items), after a series of treatment methods, leaving the necessary configuration (23), followed by experience an important class method of the Spring framework, the gradual deployment projects that refresh () AbstractApplicationContext class method. 

public abstract class SpringFactoriesLoader {
  ...  
    public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
  ...    
}

ps: SpringBoot might find in your own projects of this kind, in Code Red (creating a configuration set of candidate) adding breakpoints, debug start of the follow-up to the loading process SpringBoot.

 Two .Spring Boot how to load the startup configuration class frameworks example:? Mybatis

  1. springboot start time, by scanning the annotation @EnableConfigurationProperties (MybatisProperties.class) loaded mybatis frame configuration classes: MybatisAutoConfiguration

  2. Check the configuration class mybatis framework

public class MybatisAutoConfiguration {

  private final MybatisProperties properties;
   ... 
     
  @Bean
  @ConditionalOnMissingBean
  public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
   ...
  }

  3. Review the configuration details of mybatis

@ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX)
public class MybatisProperties {

  public static final String MYBATIS_PREFIX = "mybatis";
  ...    
}

  4. See META-INF resource path in mybatis-spring-boot-autoconfigure.jar package

III. Mybatis-demo framework of self-development and the integration of Spring Boot

  1. Analysis: jar package from research framework, which required core classes?

Analogy Mybatis, the required core classes, there SqlSessionFactory,

  MybatisAutoConfiguration,

  SqlSessionFactory method..(),

  MybatisProperties.

  2. Create a project demo-mybatis, the project is structured as follows, publish maven install labeled jar package, simulation framework

  Project source address, see this post tails, please correct me!

   3. New SpringBoot project, add SpringMvc rely on that boot-web-starter

    ① the introduction of demo-mybatis our self-development framework to lay the jar package

    Dependent as follows:

<dependency>
    <groupId>com.demo</groupId>
    <artifactId>demo-mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

    ②  编辑Controller    

@RestController
public class DemoController {
    @Autowired
    SqlSessionFactory sessionFactory;
    
    @RequestMapping("/demo-mybatis")
    public String demo() {
        return sessionFactory.getSqlSession();
    }
}

    ③ 修改yml文件自定义 框架的配置属性

com:
  demo:
    mybatis:
      hello: hi!
      world: new world!

    ④ 启动项目, 会看到控制台输出了自研框架内的"假"信息:

    ⑤ 访问测试地址: http://localhost:8080/demo-mybatis

 

    访问成功!!!

  就这样, 我们剖析了SpringBoot的加载原理, 并仿造Mybatis框架, 自研了一个demo框架, 并整合到SpringBoot项目中!!!

源码地址, 可以参考我的GitHub: https://github.com/93LifeAfterLife/Demo-Mybatis

欢迎交流指正~

Guess you like

Origin www.cnblogs.com/sansheng93/p/11354033.html