Spring Boot 入门案例 深入探究

pom. xml 探究

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>www.wmx.com</groupId>
    <artifactId>helloWorld</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 为了减少引用,于是Spring将必须的引用都封装在了spring-boot-starter-parent    只需要继承下来即可,这些配置都可以去Spring官网找到-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <dependencies>
        <!-- 因为应用会是一个web应用,所以引用web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- spring-boot-maven-plugin插件:可以将应用打包成一个可执行的jar包;-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

spring-boot-starter-parent

  • 自己的 Spring Boot 项目继承 spring-boot-starter-parent 项目
<!-- 为了减少引用,于是Spring将必须的引用都封装在了spring-boot-starter-parent只需要继承下来即可,这些配置都可以去Spring官网找到-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>
  • 而 spring-boot-starter-parent 项目继承了 spring‐boot‐dependencies
<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-dependencies</artifactId>
   <version>1.5.9.RELEASE</version>
   <relativePath>../../spring-boot-dependencies</relativePath>
</parent>
  • spring‐boot‐dependencies 真正管理 Spring Boot 应用里面的所有版本依赖关系,它的pom.xml文件也是最大的,有2999行,是 Spring Boot 的版本仲裁中心;

spring-boot-starter-web

<dependencies>
    <!-- 因为应用会是一个web应用,所以引用web-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

  • 这些starters几乎涵盖了javaee所有常用场景,Spring Boot对这些场景依赖的jar也做了严格的测试与版本控制

主程序类

package main;

/**
 * Created by Administrator on 2018/7/9 0009.
 * 应用入口
 */

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @SpringBootApplication :表示当前类为主程序类,这是一个Spring Boot应用
 */
@SpringBootApplication
public class MainApp {

    public static void main(String[] args) {
        /** 启动Spring应用
         * 第一个参数"MainApp.class"类上面必须是"@SpringBootApplication"注解修饰的类
         * 写法是约定的,就像启动Java FX应用一样*/
        SpringApplication.run(MainApp.class, args);
    }
}

@SpringBootApplication

  • @SpringBootApplication:标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot 就应该运行这个类的main方法来启动SpringBoot应用;
  • @SpringBootApplication 注解内容如下所示

@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 {

@SpringBootConfiguration

  • @SpringBootConfiguration:是 Spring Boot的配置类,标注在某个类上,表示这是一个Spring Boot的配置类;
  • @SpringBootConfiguration 内容如下所示

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}
  • @Configuration :标识配置类、配置文件;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
    String value() default "";
}

@EnableAutoConfiguration

  • @EnableAutoConfiguration:开启自动配置功能,以前需要手动配置的东西,现在Spring Boot帮我们自动进行配置
  • @EnableAutoConfiguration 是核心注解;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({EnableAutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
  • @Import 导入所有的自动配置场景
  • @AutoConfigurationPackage 定义默认的包扫描规则,程序启动扫描加载主程序类所在的包以及下面所有子包的组件;
  • EnableAutoConfigurationImportSelector:导入哪些组件的选择器,将所有需要导入的组件以全类名的方式返回,这些组件就会被添加到容器中;会给容器中导入非常多的自动配置类(xxxAutoConfiguration);就是给容器中导入这个场景需要的所有组件,并配置好这些组件;
  • Spring Boot 在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration 指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作;以前我们需要手动配置的东西,自动配置类都帮我们配置好
  • J2EE的整体整合解决方案和自动配置都在spring-boot-autoconfigure-1.5.9.RELEASE.jar;




扫描二维码关注公众号,回复: 2109918 查看本文章


猜你喜欢

转载自blog.csdn.net/wangmx1993328/article/details/80990098