第一章 SpringBoot 介绍-最小配置

1. spring-boot web应用的最小配置

1.1 pom配置两个依赖

  	
        org.springframework.boot
        spring-boot-starter-parent
        2.3.4.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
    

1.2 新建一个启动类

@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}

1.3 新建一个controller

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String handle01(){
        return "Hello, Spring Boot 2!";
    }
}

1.4 请求地址:localhost:8080/hello

1.5 扩展

1、maven命令:mvn install,会将项目打包成原始的jar,不能直接启动
2、如果需要将带main方法的maven项目打包成可运行的jar包,需要pom引入组件
	
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
3、项目根目录下打包命令 mvn install
4、运行命令 java -jar target/springbootinitial-1.0-SNAPSHOT.jar

5、注意:如果当前项目配置了spring-boot-starter-parent,在这个parent包里面配置了对应的goal配置
只需要简单配置如上即可,如果没有配置spring-boot-starter-parent,那么还需要如下配置:
(If you are using spring-boot-starter-parent, such execution is already pre-configured 
with a repackage execution ID so that only the plugin definition should be added.)

    
        
            org.springframework.boot
            spring-boot-maven-plugin
            
                
                    
                        repackage
                    
                
            
        
    

2. 两个优点

2.1 依赖管理

		

     org.springframework.boot
     spring-boot-starter-parent
     2.3.4.RELEASE
 



    org.springframework.boot
    spring-boot-dependencies
    2.3.4.RELEASE

 
  • spring-boot starter场景启动器

场景启动器,只要引用了一个场景启动器,这个场景的所有依赖都被引入。

1、spring-boot-starter-* : spring官方提供启动器
2、只要引入starter,这个场景的所有常规需要的依赖我们都自动引入
3、SpringBoot所有场景的支持:
https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.build-systems.starters
4、*-spring-boot-starter:第三方提供的启动器
5、所有场景启动器最底层的依赖:
 
      org.springframework.boot
      spring-boot-starter
      2.3.4.RELEASE
      compile
    
  • 无需关注版本号,自动版本仲裁
1、引入依赖默认都可以不写版本
2、引入非版本仲裁的jar(没有在spring-boot-dependencies里面定义)需要申明版本
  • 可以修改版本号
1、查看spring-boot-dependencies里面规定当前依赖的版本用的key(mysql.version)
2、在当前项目里面重写配置

        5.1.43

2.2 自动配置

  • 自动配好Tomcat
    • 引入Tomcat依赖
    • 配置Tomcat

      org.springframework.boot
      spring-boot-starter-tomcat
      2.3.4.RELEASE
      compile
    
  • 自动配好SpringMVC
    • 引入SpringMVC全套组件
    • 自动配好SpringMVC常用组件(功能)
  • 自动配好Web常用功能,如:字符编码问题
    • SpringBoot帮我们配置好了所有web开发的常用场景
  • 默认的包结构
    • 主程序所在包及其所有子包里面的组件都会被默认扫面进来
    • 无需以前的包扫描配置
    • 想要改变扫描路径,@SpringBootApplication(scanBasePackages="com.koral")
      • 或者@ComponentScan指定扫描路径
@SpringBootApplication
等同于
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
  • 各种配置拥有默认值
    • 默认配置最终都是映射到MultipartProperties
    • 配置文件的值最终会绑定到每个类上,这个类会在容器中创建对象
  • 按需加载所有自动配置项
    • 非常多的starter
    • 引入了哪些场景这个场景的自动配置才会开启
    • SpringBoot的所有自动配置功能都在spring-boot-autoconfigure里面

你可能感兴趣的:(java,spring,boot,后端,java)