1. Spring Boot 简介

简化Spring应用开发的一个框架;
整个Spring技术栈的一个大整合;
J2EE开发的一站式解决方案

2.微服务

微服务:架构风格(微服务化)
一个应用应该是一组小型服务;可以通过HTTP的方式进行互通
每一个元素最终都是一个可独立替换和独立升级的软件单元

3.入门环境准备
  • jdk1.8:java version “1.8.0_112”
  • maven3.x:maven3.3以上版本;Apache Maven 3.6.1
  • IDEA 2017
  • SpringBoot 1.5.9 RELEASE
4.SpringBoot入门案例-HelloWorld

功能要求:
浏览器发送hello请求,服务器接收请求并处理,响应HelloWorld字符串

  1. 创建普通maven工程(不需要骨架)
  2. 导入相关依赖
<parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>1.5.9.RELEASE</version>
     </parent>
     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
 
     </dependencies>
  1. 编写一个主程序,启动SpringBoot应用
  2. idea springboot profile不生效 idea 没有springboot_ci

  3. 编写相关Controller、Service等等
  4. idea springboot profile不生效 idea 没有springboot_jar包_02

  5. 运行主程序测试
    运行main方法,打开浏览器输入localhost:8080/hello显示成功
  6. idea springboot profile不生效 idea 没有springboot_ci_03

  7. 简化部署
    不再需要将应用打成war包,只需要引入依赖即可将应用打包成一个可执行的jar包
<!-- 这个插件的作用是将应用打包成一个可执行的jar包 -->
     <build>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
         </plugins>
     </build>
 
 </project>
  1. 直接运行打包好的jar包
    (1)打包
  2. idea springboot profile不生效 idea 没有springboot_spring_04

  3. 注意:主方法不能放在同级包内,必须如下图放置,否则会报错
  4. idea springboot profile不生效 idea 没有springboot_jar包_05

  5. (2) 将jar复制到桌面
  6. idea springboot profile不生效 idea 没有springboot_spring_06

  7. (3)cmd输入代码执行
  8. idea springboot profile不生效 idea 没有springboot_spring_07

(4)运行成功

idea springboot profile不生效 idea 没有springboot_jar包_08

5 案例细节
  1. pom.xml文件
  • 父项目
<parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.9.RELEASE</version>
  </parent>

它的父项目是

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-dependencies</artifactId>
		<version>1.5.9.RELEASE</version>
		<relativePath>../../spring-boot-dependencies</relativePath>
	</parent>

真正管理SpringBoot应用的所有依赖版本。

一般情况下导入依赖默认不需要写版本,但是没有在dependencies里面管理的依赖需要声明版本号

  • starters 场景启动器
<dependencies>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

spring-boot-starter:spring-boot场景启动器,帮我们导入了web模块相关的依赖

SpringBoot将所有的功能场景都抽取出来,做成一个个starters(启动器),只需要在这些项目里面引入这些starters,相关场景的所有依赖都会导入进来。需要什么功能就导入该功能的场景启动器

  1. 主程序类,主入口类
//使用注解标注主程序,说明这是一个SpringBoot运用
 @SpringBootApplication
 public class HelloWorld {
     public static void main(String[] args) {
 	    
         SpringApplication.run(HelloWorld.class,args);
     }
 }

SpringBootApplication:使用注解标注主程序,说明这是一个SpringBoot的主配置类,springboot就会运行这个类的main方法来启动SpringBoot运用

@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:表示这是一个SpringBoot的配置类
  • @EnableAutoConfiguration:开启自动配置功能。
    其下底层注解:
    @Import({EnableAutoConfigurationImportSelector.class}):给容器中导入一个组件;导入的组件由Auto.ConfigurationPackages.Registrar.class将主配置类(@SpringBootConfiguration所标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器
    AutoConfigurationImportSelector:导入哪些组件的选择器。将所有需要导入的组件以全类名的方式返回,这些组件就会被添加到容器中。有了自动配置类,就免去手动编写配置注入功能主键等功能

    SpringBoot在启动的时候从类路径选的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作;以前我们需要配置的东西,SpringBoot全帮我们配置好了。

J2EE的整体整合解决方案和自动配置文件都在spring-boot-autoconfigure-1.5.9.RELEASE.jar

idea springboot profile不生效 idea 没有springboot_ci_09

6.使用Spring Initializer快速创建SpringBoot项目

new project→

idea springboot profile不生效 idea 没有springboot_spring_10


填写项目名报名等后next→

idea springboot profile不生效 idea 没有springboot_spring_11


根据需要勾选启动器点击next到finish

idea springboot profile不生效 idea 没有springboot_spring_12


默认生成的SpringBoot项目:

  • 主程序已经生成好了,我们只需要按照自己的业务逻辑编写代码
  • resources文件夹中的目录结构
  • static:保存所有静态资源(js 、css、 images)
  • templates:保存所有模板页面(Spring Boot 默认jar包使用jian嵌入式的Tomcat,默认不支持JSP页面);可以使用模板引擎(freemarker、thymeleaf )
  • applica.properties:Spring Boot应用的配置文件;可以通过它修改一些默认配置