美文网首页
SpringBoot学习整理(二)探究HelloWorld

SpringBoot学习整理(二)探究HelloWorld

作者: 啊_6424 | 来源:发表于2019-03-03 18:18 被阅读0次

    一、pom文件

    (一)父项目

     <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.6.RELEASE</version>
        </parent>
    //它的父项目是
    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.5.6.RELEASE</version>
            <relativePath>../../spring-boot-dependencies</relativePath>
        </parent>
    

    这个父项目还依赖一个父项目------Spring Boot的版本仲裁中心(spring-boot-dependencies),它是Spring Boot里边所有依赖管理,以后我们导入依赖默认是不要写版本的(除非导入依赖没有被管理)。


    image.png

    (二)导入的依赖

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

    Spring Boot场景启动器,帮我们导入了Web模块正常运行所需要的组件
    Spring Boot将所有场景都抽取出来,做成一个个的starter(启动器),只需在项目里引入这些starter,相关场景的所有依赖都会导入进来。
    要用什么功能就用什么场景的启动器
    starter地址:https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-build-systems.html#using-boot-starter

    二、主程序类,主入口类

    @SpringBootApplication
    public class HelloWorld {
        //快捷键是 psvm
        public static void main(String[] args) {
            //Spring应用启动起来
            SpringApplication.run(HelloWorld.class,args);
        }
    }
    

    (一)、@SpringBootApplication

    SpringBoot应用标注在某个类上说明这个类是SpringBoot的主配置类
    SpringBoot就应该运行这个类的main方法来启动SpringBoot应用
    \color{#ff0000}{ctrl + 点击 就可以查看源码}

    @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}
    )}
    )
    public @interface SpringBootApplication {
       ......
    }
    
    1.@SpringBootConfiguration

    Spring Boot的配置类,标注在某一个类上则表示这是一个SpringBoot配置类
    配置类----------配置文件

    (1).@Configuration

    让SpringBoot知道这是一个配置类,就要在该类上加@Configuration
    点进去查看代码就会发现它其实就是容器中的一个组件

    2.@EnableAutoConfiguration

    开启自动配置功能

    @AutoConfigurationPackage
    @Import({EnableAutoConfigurationImportSelector.class})
    public @interface EnableAutoConfiguration {
    ......
    }
    
    (1).@AutoConfigurationPackage

    将主配置类(@SpringBootConfiguration标注的类)的所在包以及下面的子包里的所有组件扫描到Spring容器
    由此可得\color{#ff0000}{主配置类必须在其他类的同级或者上级目录}

    (2).@Import({EnableAutoConfigurationImportSelector.class})

    将所有需要导入的组件已全类名的方式返回,这些组件就会添加到容器中
    会给容器中导入非常多的自动配置类,给容器导入并配置好这个场景需要的所有组件

    这些都是导入的自动配置类(XXXAutoConfiguration)
    SpringBoot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作。

    image.png
    \color{#ff0000}{J2EE的整体整合解决方案和自动配置都在spring-boot-autoconfigure-1.5.6.RELEASE.jar中}

    相关文章

      网友评论

          本文标题:SpringBoot学习整理(二)探究HelloWorld

          本文链接:https://www.haomeiwen.com/subject/cqrmuqtx.html