Spring Boot 基础(一)

一. Spring Boot 简介

Spring Boot 非常流行的微服务框架,它倡导“约定优于配置”的原则,对Spring框架进行了封装,使用很多默认的配置大大简化了 Spring 项目的初始化过程。Spring Boot 提供了很多starters来整合其他开源项目,简化maven的配置。除此以外,Spring Boot 还提供了内嵌servlet容器,健康检查,外部化配置(yml)等功能。

Spring Boot 2.x 的开发环境要求:

  • Java:jdk 1.8+
  • maven:3.2+
  • Spring:5.0+

Spring Boot 项目的初始化:使用IDE创建一个maven工程,然后在pom.xml中添加 Spring Boot 依赖,通常是继承自父工程spring-boot-starter-parent,然后根据需要添加依赖组件,点击父工程spring-boot-starter-parent可以看到Spring Boot项目默认编码格式是UTF-8,默认的编译版本是jdk1.8,并且添加了很多maven插件。继续点击spring-boot-dependencies父工程可以看到所有依赖项目的版本,所以在pom.xml无需指定version标签。

Hello World

  1. 在IDE中创建一个maven项目(或者Module),并在pom.xml中添加Spring Boot的父工程依赖坐标,然后为项目添加web依赖:

    
    <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.0modelVersion>
    
        <groupId>com.znkergroupId>
        <artifactId>boot_demoartifactId>
        <version>1.0-SNAPSHOTversion>
    
        
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.0.7.RELEASEversion>
        parent>
    
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
        dependencies>
    
        <build>
            <plugins>
                
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
            plugins>
        build>
        
    project>
    
  2. 与Spring项目一样,在web项目中添加一个controller

    package com.znker.boot.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    // 这是一个组合注解,这个类被注册为容器中的一个Bean,
    // 同时这个类中的所有方法的返回值自动序列化为JSON格式的数据返回给客户端
    @RestController
    public class BaseController {
    	// 映射请求的URL
        @RequestMapping("/demo")
        public String home(){
            return "Hello Spring Boot!";
        }
    }
    
  3. 创建一个项目的"入口"类用于启动项目,注意因为自动的注解扫描注册Beans,所以Main class一定要位于项目的root package中:

    package com.znker.boot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    // 非常重要的组合注解,后面详解
    @SpringBootApplication
    public class BootDemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(BootDemoApplication.class, args);
        }
    }
    
  4. 在resources目录中添加项目的配置文件application.properties或者application.yml,IDEA会智能识别这些配置文件:

    server:
      port: 8080
    
  5. 回到BootDemoApplication启动类中运行项目,此时由于Spring Boot项目中web依赖中内嵌了servlet容器,所以项目可以直接运行,在postman中输入http://127.0.0.1:8080/demo ,服务端返回"Hello Spring Boot!"

二. Spring Boot 基础详解

项目构建:

Spring Boot 使用maven或者gradle构建项目和管理依赖,使用maven时可以继承spring-boot-starter-parent,该项目提供了很多默认的项目配置:

  • Java 1.8 为默认的编译级别
  • UTF-8 为源码的编码格式
  • 从父工程spring-boot-dependencies中锁定了很多可能需要的依赖项的版本,所以在pom中引入相关依赖的时候无需指定version标签
  • 智能感知项目资源文件,使用resources插件在打包jar的时候一同打包项目所有资源文件

Starters:

Starters 是一系列的依赖描述的pom文件,该描述文件规定了引入该模块时所需要的依赖项(jar),例如在pom.xml中添加spring-boot-starter-web模块时,引入的依赖项如下,这些依赖有的是另一个项目,有的则直接是jar包。Starters 模块化了项目依赖的引入,简化了pom.xml的配置,Spring Boot 有几十个starter,对于JPA,redis,thymeleaf等常见模块都提供了starter,具体可查阅配文档

<dependencies>
	<dependency>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starterartifactId>
		<version>2.0.7.RELEASEversion>
		<scope>compilescope>
	dependency>
    
	<dependency>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-jsonartifactId>
		<version>2.0.7.RELEASEversion>
		<scope>compilescope>
	dependency>
    
	<dependency>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-tomcatartifactId>
		<version>2.0.7.RELEASEversion>
		<scope>compilescope>
	dependency>
	<dependency>
		<groupId>org.hibernate.validatorgroupId>
		<artifactId>hibernate-validatorartifactId>
		<version>6.0.13.Finalversion>
		<scope>compilescope>
	dependency>
    
	<dependency>
		<groupId>org.springframeworkgroupId>
		<artifactId>spring-webartifactId>
		<version>5.0.11.RELEASEversion>
		<scope>compilescope>
	dependency>
	<dependency>
		<groupId>org.springframeworkgroupId>
		<artifactId>spring-webmvcartifactId>
		<version>5.0.11.RELEASEversion>
		<scope>compilescope>
	dependency>
dependencies>

项目入口的 Main application class:

Spring Boot 对项目中源码的包结构有特定的要求,如果classpath下没有声明任何包,那么所有的源码就会位于一个"default"的包下,但这是极其不推荐的做法,推荐的做法是和Spring一样,使用域名反写com.example.project。项目入口类Application要位于项目的root package下,Application上会添加@SpringBootApplication注解,该注解隐式地开启注解扫描注册Bean,默认的搜索路径就是当前文件夹,建议源码的目录结构如下:

com
 +- example
     +- myapplication
         +- Application.java	// @SpringBootApplication
         |
         +- customer
         |   +- Customer.java
         |   +- CustomerController.java
         |   +- CustomerService.java
         |   +- CustomerRepository.java
         |
         +- order
             +- Order.java
             +- OrderController.java
             +- OrderService.java
             +- OrderRepository.java

配置类 Configuration class:

Spring Boot 推荐使用 Java-based Configuration 配置类配置项目中的组件,@Import注解用于导入额外的配置类,如果使用了@ComponentScan注解则会自动注册所有Spring组件,包括使用@Configuration注解标识的配置类。如果确实要使用xml配置第三方组件,那么还是建议使用一个@Configuration配置类,然后使用@ImportResource注解导入配置文件。这是 Spring IOC 部分的知识,具体细节可查阅Spring文档

依赖注入:

Spring Boot 中可以使用Spring中Bean定义和依赖注入技术,一般来说@ComponentScan@Autowired是非常好的组合。如果项目中的包结构按推荐的做法,那么@ComponentScan注解无需任何参数,Spring Boot 会将项目中添加了@Component@Bean@Controller@Service@Repository注解的组件全部注册为容器中的Bean

@SpringBootApplication Annotation:

@SpringBootApplication是一个组合注解,它能够实现项目自动化配置、组件扫描与注册,该注解是三个注解的组合:

  • @EnableAutoConfiguration:这个注解能够实现Spring Boot 项目的自动化配置
  • @ComponentScan:从 Main application class 所在包下开始注解扫描,注册项目中的所有Bean
  • @Configuration:注册上下文中的其他Bean,获取导入其他配置类

你可能感兴趣的:(Spring,Boot)