结合官网,使用Maven构建第一个Spring Boot(2.1版本)程序

本篇博客涉及代码已经上传到github上,需要可自行下载:
github项目链接
本篇博客涉及到的代码链接

本篇文章包含以下要点 :

一. Spring Boot简介

Spring Boot官网链接
在官网中,我们能够看到如下描述:

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".
We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

Features
Create stand-alone Spring applications
Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
Provide opinionated 'starter' dependencies to simplify your build configuration
Automatically configure Spring and 3rd party libraries whenever possible
Provide production-ready features such as metrics, health checks and externalized configuration
Absolutely no code generation and no requirement for XML configuration

从以上描述中,我们能够知道.

Spring Boot的设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。

Spring Boot具有如下特点 :
1. 创建独立的Spring应用程序
2. 嵌入的Tomcat,无需部署WAR文件
3. 简化Maven配置
4. 自动配置Spring
5. 提供生产就绪型功能,如指标,健康检查和外部配置
6. 绝对没有代码生成并且对XML也没有配置要求
二.Spring Boot开发环境准备
  • Maven环境
  • 文本编辑工具或是IDE
  • jdk1.8及以上版本
三. Spring Boot程序开发(使用版本2.1.1)
  • 首先在Ide里面新建 maven工程,在pom.xml文件里面导入Spring Boot依赖
    通过该网站来查询相关maven依赖,挑一个使用人数比较多的作为依赖即可
    pom.xml文件里面添加如下内容:

     <dependencies>
      <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>2.1.1.RELEASE</version>
          <type>pom</type>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
          <version>2.1.1.RELEASE</version>
      </dependency>
    
  • 依赖导入之后,正式进入开发环节:

    1. 在src/main/java目录下新建包 demo
      在demo包下创建controller包
    2. 开发controller类:
		package demo.controller;
		import org.springframework.web.bind.annotation.RequestMapping;
		import org.springframework.web.bind.annotation.RestController;
		
		/**
		 * @author xmr
		 * @date 2019/4/14 8:49
		 * @description
		 */
		@RestController
		public class HelloController {
		
		    @RequestMapping("/hello")
		    /**
		     * RequestMapping作用 : 提供路由信息,负责URL到Controller中的具体函数的映射
		     */
		    public String hello() {
		        return "Hello World";
		    }
		}
  1. 开发程序启动类,在demo的根目录下
    	package demo;
    
    	import org.springframework.boot.SpringApplication;
    	import org.springframework.boot.autoconfigure.SpringBootApplication;
    	
    	/**
    	 * @author xmr
    	 * @date 2019/4/14 8:55
    	 * @description
    	 */
    	/**
    	 *  @SpringBootApplication 用来标注一个主程序类,说明这是一个Spring Boot应用
    	 */
    	@SpringBootApplication
    	public class HelloWorldApplication {
    	    public static void main(String[] args) {
    	        SpringApplication.run(HelloWorldApplication.class, args);
    	    }
    	}
    
    开发之后的目录结构如下图所示 :
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NsWoLeCJ-1586354943628)(https://i.loli.net/2019/04/14/5cb2968498f7c.png)]

浏览器输入 : http://localhost:8080/hello
显示如下 :
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uuTW3qqf-1586354943635)(https://i.loli.net/2019/04/14/5cb297d4e835f.png)]
证明程序执行成功!
需要注意的问题 :

  1. 在src/main/java下面必须再新建一层目录,否则Spring Boot执行的时候会报错 :
  2. 所有的包和类都必须在程序运行类所在的目录或者其子目录下,否则相关的包或类将不会被自动装载!
四. Spring Boot程序运行原理解读

  从程序的入口开始分析,首先介绍程序入口类上面的注释 :
SpringBootApplication:
Spring Boot应用标注在某个类上说明这个类是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 {

其包含: @Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三个配置

@SpringBootConfiguration : Spring Boot的配置类,标注在某个类上,表示这是一个Spring Boot的配置类
@Configuration : 配置类上来标注这个注解,配置类也是容器中的一个组件@Component
@EnableAutoConfiguration:开启自动配置功能
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器;
调用了SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader);
Spring Boot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作;以前我们需要自己配置的东西,自动配置类都帮我们;
有了自动配置类,免去了我们手动编写配置注入功能组件等的工作;

自动配置原理

1) SpringBoot启动的时候加载主配置类,开启了自动配置功能 @EnableAutoConfiguration
2) @EnableAutoConfiguration 作用:

利用EnableAutoConfigurationImportSelector给容器中导入一些组件?
可以查看selectImports()方法的内容;

List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);获取候选的配置

SpringFactoriesLoader.loadFactoryNames()
扫描所有jar包类路径下  META-INF/spring.factories
把扫描到的这些文件的内容包装成properties对象
从properties中获取到EnableAutoConfiguration.class类(类名)对应的值,然后把他们添加在容器中
将类路径下  META-INF/spring.factories 里面配置的所有EnableAutoConfiguration的值加入到了容器中

精髓:
​ 1)SpringBoot启动会加载大量的自动配置类
​ 2)我们看我们需要的功能有没有SpringBoot默认写好的自动配置类;
​ 3)我们再来看这个自动配置类中到底配置了哪些组件;(只要我们要用的组件有,我们就不需要再来配置了)
​ 4)给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。我们就可以在配置文件中指定这些属性的值;
xxxxAutoConfigurartion:自动配置类(给容器中添加组件);
xxxxProperties:封装配置文件中相关属性;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值