
1,背景
J2EE笨重的开发,繁多的配置,低下的开发效率,复杂的部署流程,第三方技术集成难度大。
2,解决
3,优点
4,Spring Boot官方建议项目结构
myproject
 +-src
    +- main
         +- java
              +- com.example.myproject
                    +- comm
                    +- model
                    +- repository
                    +- service
                    +- web
                    +- Application.java
         +- resources
              +- static
              +- templates
              +- application.properties
    +- test
 +-pom.xml1,com.example.myproject 目录下:
2,resources 目录下:
3,pom.xml介绍
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>按住ctrl点击spring-boot-starter-parent查看spring-boot-starter-parent,Maven 支持项目的父子结构,引入父级后会默认继承父级的配置;
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.1.9.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
  </parent><groupId>com.example</groupId>   <!--groupId,项目的包路径;-->
    <artifactId>demo01</artifactId>  <!--artifactId,项目名称;-->
    <version>0.0.1-SNAPSHOT</version>  <!--version,项目版本号-->
    <name>demo01</name>  <!--name,项目名称-->
    <description>Demo project for Spring Boot</description>  <!--description,项目描述-->packaging,一般有两个值:jar、war,表示使用 Maven 打包时构建成 Jar 包还是 War 包.
<properties>
        <java.version>1.8</java.version>
</properties>我们导入依赖默认是不需要写版本,没有在dependencies里面管理的依赖需要声明版本号。这是spring boot的web场景启动器,只要引入了它,就能帮我们导入了web模块正常运行所依赖的组件。
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId> <!--groupId组织的唯一标识-->
            <artifactId>spring-boot-starter-web</artifactId> <!--artifactId项目的唯一标识-->
        </dependency>
        <!--添加依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies><build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>4,主程序类
@SpringBootApplication: Spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用。
/**
 * @SpringBootApplication 来标注一个主程序类,说明这是一个 Spring Boot应用
 */
@SpringBootApplication
public class Demo01Application {
    public static void main(String[] args) {
        //Spring应用启动
        SpringApplication.run(Demo01Application.class, args);
    }
}@SpringBootConfiguration: Spring Boot的配置类:标注在某个类上,表示这是一个Spring Boot的配置类;
@Configuration:配置类上来标注这个注解; 配置类 ----- 配置文件;配置类也是容器中的一个组件;@Component
@EnableAutoConfiguration:开启自动配置功能;
以前我们需要配置的东西,Spring Boot帮我们自动配置;
@EnableAutoConfiguration告诉SpringBoot开启自动配置功能;这样自动配置才能生效;
5,快速测试
在src下创建一个controller包,新建一个HelloController类,之后运行springboot,便可以通过localhost:/8080/hello来访问hello页面。
@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "HelloWorld";
    }
}6,配置文件
SpringBoot使用一个全局的配置文件,配置文件名是固定的:
application.properties
application.yml
配置文件的作用:修改SpringBoot自动配置的默认值;SpringBoot在底层都给我们自动配置好。
spring boot启动会扫描以下位置的 application.properties或者application.yml文件作为Spring boot的默认配置文件。
–file:./config/
–file:./
–classpath:/config/
–classpath:/
优先级由高到底,高优先级的配置会覆盖低优先级的配置;SpringBoot会从这四个位置全部加载主配置文件,互补配置;
application.properties
person:
    lastName: hello
    age: 19
    boss: true
    birth: 2000/10/11
    maps: {k1: v1,k2: 12}
    lists:
      - lisi
      - zhaoliu/**
 *
 * 将配置文件中配置的每一个属性的值,映射到这个组件中
 * @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
 * prefix = "person":配置文件中哪个下面的所有属性进行一一映射
 *
 * 只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能;
 */
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>