zoukankan      html  css  js  c++  java
  • springboot学习入门之三---启动原理

    3启动原理

    3.1启动类

    @SpringBootApplication

    public class Application {

        public static void main(String[] args) {

            SpringApplication.run(Application.class, args);

        }

    }

    关键:@SpringBootApplication注解和类定义(SpringApplication.run)

    3.2SpringBootApplication详解

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan(
        excludeFilters = {@Filter(
        type = FilterType.CUSTOM,
        classes = {TypeExcludeFilter.class}
    ), @Filter(
        type = FilterType.CUSTOM,
        classes = {AutoConfigurationExcludeFilter.class}
    )}
    )

    虽然使用了多个annotation进行原信息标注(使用@SpringBootApplication一个注解集成多个,简便),但实际上重要的有三个annotation。

    @Configuration(@SpringBootConfiguration注解源码应用为@Configuration)

    @EnableAutoConfiguration

    @ComponentScan

    3.2.1@Configuration

    Spring ioc容器配置类使用的即为@Configuration,springboot推荐使用基于javaconfig的配置形式。简单了解下xml和config配置方式区别:

    1) 表达形式层面

    基于xml配置方式:

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"

           default-lazy-init="true">

        <!--bean定义-->

    </beans>

    基于javaconfig配置方式:

    @Configuration

    public class MockConfiguration{

        //bean定义

    }

    任何一个标注了@Configuration的Java类定义都是一个JavaConfig配置类。

    2) 注册bean定义层面

    基于xml配置形式

    <bean id="mockService" class="..MockServiceImpl">

        ...

    </bean>

    基于javaconfig配置形式:

    @Configuration

    public class MockConfiguration{

        @Bean

        public MockService mockService(){

            return new MockServiceImpl();

        }

    }

    任何一个标注了@Bean的方法,其返回值将作为一个bean定义注册到Spring的IoC容器,方法名将默认成该bean定义的id。

    3) 表达依赖注入关系层面

    为了表达bean与bean之间的依赖关系,在XML形式中一般是这样:

    <bean id="mockService" class="..MockServiceImpl">

        <propery name ="dependencyService" ref="dependencyService" />

    </bean>

    <bean id="dependencyService" class="DependencyServiceImpl"></bean>

    基于javaconfig配置形式:

    @Configuration

    public class MockConfiguration{

        @Bean

        public MockService mockService(){

            return new MockServiceImpl(dependencyService());

        }

        @Bean

        public DependencyService dependencyService(){

            return new DependencyServiceImpl();

        }

    }

    如果一个bean的定义依赖其他bean,则直接调用对应的JavaConfig类中依赖bean的创建方法就可以了。

    3.2.2@ComponentScan

    1)自动扫描并加载符合条件的组件,或者bean定义,最终将这些bean定义加载到IoC容器中。

    2)可通过basePackages等属性细粒度定制@ComponentScan自动扫描范围,若不指定,默认从声明@ComponentScan 所在类的package进行扫描。

    3)注意:springboot启动类最好放在root package下,因为默认不指定basepackages。

    3.2.3@EnableAutoConfiguration

    1)@EnableAutoConfiguration同其他@Enable注解类似:借助@Import的支持,收集和注册特定场景相关的bean定义。如:

    @EnableScheduling是通过@Import将Spring调度框架相关的bean定义都加载到IoC容器。

    @EnableMBeanExport是通过@Import将JMX相关的bean定义加载到IoC容器。

    @EnableAutoConfiguration也是借助@Import的帮助,将所有符合自动配置条件的bean定义加载到IoC容器,仅此而已!

    1) 源码:

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @AutoConfigurationPackage
    @Import({AutoConfigurationImportSelector.class})
    public @interface EnableAutoConfiguration {

    2) 关键:@Import(EnableAutoConfigurationImportSelector.class)

    借助AutoConfigurationImportSelector,@EnableAutoConfiguration可以帮助SpringBoot应用将所有符合条件的@Configuration配置都加载到当前SpringBoot创建并使用的IoC容器。就像一只“八爪鱼”一样。

    借助于Spring框架原有的一个工具类:SpringFactoriesLoader的支持,@EnableAutoConfiguration可以智能的自动配置功效才得以大功告成!

     

    3.1)SpringFactoriesLoader详解(自动配置关键)

    SpringFactoriesLoader属于Spring框架私有的一种扩展方案,其主要功能就是从指定的配置文件META-INF/spring.factories加载配置。

    public abstract class SpringFactoriesLoader {
        public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
        private static final Log logger = LogFactory.getLog(SpringFactoriesLoader.class);
        private static final Map<ClassLoader, MultiValueMap<String, String>> cache = new ConcurrentReferenceHashMap();

        public SpringFactoriesLoader() {
        }

     

    配合@EnableAutoConfiguration使用的话,它更多是提供一种配置查找的功能支持,即根据@EnableAutoConfiguration的完整类名org.springframework.boot.autoconfigure.EnableAutoConfiguration作为查找的Key,获取对应的一组@Configuration类。

    所以,@EnableAutoConfiguration自动配置的魔法骑士就变成了:从classpath中搜寻所有的META-INF/spring.factories配置文件,并将其中org.springframework.boot.autoconfigure.EnableutoConfiguration对应的配置项通过反射(Java Refletion)实例化为对应的标注了@Configuration的JavaConfig形式的IoC容器配置类,然后汇总为一个并加载到IoC容器。

     

  • 相关阅读:
    vector order by function and object
    C++ Tree node ,when g++ compile multiple cpp files should list them respectively
    Node.1
    asp遍历xml节点
    redis扩展安装以及在tp5中的操作
    JS中在循环内实现休眠(for循环内setTimeout延时问题)
    一种开发软件的新思路,给Web页面穿个马甲,用web页面做软件UI,用C#(或者C++等其它语言)代码做功能 碧血黄沙
    高仿QQMusic播放器,浅谈WinForm关于UI的制作 碧血黄沙
    我的站(艾网城市生活新门户)重新上线了 碧血黄沙
    用WPF开发仿QQ概念版之MessageWindow开发以及Demo下载 碧血黄沙
  • 原文地址:https://www.cnblogs.com/cslj2013/p/9141081.html
Copyright © 2011-2022 走看看