SpringBoot HelloWorld

功能需求

​ 浏览器发送hello请求,服务器接收请求并处理,相应HelloWorld字符串

1.创建一个maven工程;(jar)

2.导入SpringBoot相关依赖

<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-parent</artifactId>
</dependency>
</dependencies>

3.编写一个主程序,启动SpringBoot应用

/**

 * @SpringBootApplication 标注一个主程序类,说明这是一个SpringBoot 应用
**/
@SpringBootApplication
public class HelloWorldMainApplication { public static void main(String[] args) { //启动Spring应用
SpringApplication.run(HelloWorldMainApplication.class,args);
} }

4.编写相关的Controller、Service

@Controller
public class HelloController { @ResponseBody
@RequestMapping("/hello")
public String hello(){
return "HelloWorld!";
}
}

5.运行主程序测试

http://localhost:8080/hello

6. 简化部署

<!--    这个插件可以将应用打包成一个可执行的jar包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

将这个应用打成jar包(自带tomcat),直接使用java -jar的命令进行执行

7.探究HalloWorld

1.POM文件

1.父项目

<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>
真正管理Spring Boot应用里面的所有依赖版本;

Spring Boot版本仲裁中心(spring-boot-dependencies);

以后我们导入依赖默认是不需要些版本的;(没有在dependencies里面管理的依赖自然需要声明)

2.启动器starter

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

spring-boot-starter-web:

​ spring-boot-starter:spring-boot场景启动器;

​ spring-boot-starter-web帮我们导入了web模块正常运行所依赖的组件;

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

2.主程序类,主入口类

/**
* @SpringBootApplication 标注一个主程序类,说明这是一个SpringBoot 应用
**/
@SpringBootApplication
public class HelloWorldMainApplication { public static void main(String[] args) { //启动Spring应用
SpringApplication.run(HelloWorldMainApplication.class,args);
} }

@SpringBootApplication:Spring Boot 应用标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的方法来启动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 {
1.@SpringBootConfiguration:Spring Boot的配置类;

​ 标注在某个类上,表示这是一个SpringBoot的配置类

​ 包含@Configuration:配置类上来标注这个注解;

​ 配置类就是以往的配置文件;将配置文件替换成配置类,使用该注解才能让springboot知道这是个配置类,配置类也是容器中的一个组件:@Component

2.@EnableAutoConfiguration:开启自动配置功能

​ 以前我们需要配置的东西,Springboot帮我们自动配置;

@AutoConfigurationPackage
@Import({EnableAutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {

​ @AutoConfigurationPackage:自动配置包

​ @Import({Registrar.class}):Spring的底层注解@Import,给容器中导入一个组件;导入的组件由{Registrar.class}来决定

​ 将主配置类(@SpringBootApplication标注的类)的所在包以及所有子包里面的所有组件扫描到Spring容器中

​ @Import({EnableAutoConfigurationImportSelector.class})

​ 给容器中导入组件

​ EnableAutoConfigurationImportSelector:导入哪些组件的选择器,将所有需要导入的组件以全类名返回,这些组件就会被添加到容器中

​ 会给容器中导入96个自动配置类(xxxAutoConfigration):就是容器中导入这个场景需要的所有组件,并配置好这些组件;有了自动配置类,免去了我们手动编写配置注入功能组件等的工作;

​ 获取Configurations主要使用了

​ SpringFactoriesLoader.loadFactoryNames(EnableAutoConfigurationr.class, classLoader);

​ 从类路径下获取 META-INF/spring.factories 资源文件(spring-boot-autoconfigure-1.5.9.RELEASE中),获取EnableAutoConfigurationr指定的值(如上图),将这些值作为自动配置类导入到容器中,自动配置类就生效了,即自动配置。

​ 以前我们需要自己配置的东西,自动配置类都帮我们完成了。(eg.WebMvcAutoConfiguration)

​ @bean添加组件

//WebMvcAutoConfiguration
@Bean //RequestMapping
@Primary
public RequestMappingHandlerMapping requestMappingHandlerMapping() { @Bean //视图解析器
@ConditionalOnMissingBean
public InternalResourceViewResolver defaultViewResolver() {
.....

​ J2EE的整体整合解决方案和自动配置都在spring-boot-autoconfigure-x.x.x.RELEASE.jar下的org.springframework.boot.autoconfigure中

SpringBoot的 HelloWorld的更多相关文章

  1. springboot之HelloWorld

    简介 为了简化开发Spring的复杂度,Spring提供了SpringBoot可以快速开发一个应用,这里就简单介绍下SpringBoot如何快速开发一个J2EE应用 HelloWorld 首先在gra ...

  2. SpringBoot学习helloworld

    这几天开始学习springBoot记录一下(Hello World) pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0 ...

  3. SpringBoot的HelloWorld 应用及解释

    参考链接: Spring Data JPA - Reference Documentation Spring Data JPA--参考文档 中文版 纯洁的微笑:http://www.ityouknow ...

  4. [一]SpringBoot 之 HelloWorld

    (1)新建一个Maven Java工程 (2)在pom.xml文件中添加Spring BootMaven依赖 2.1在pom.xml中引入spring-boot-start-parent spring ...

  5. SpringBoot入门学习(一): Idea 创建 SpringBoot 的 HelloWorld

    创建项目: 项目结构: 程序启动入口: 正式开始: package com.example.demo; import org.springframework.boot.SpringApplicatio ...

  6. redis整合springboot的helloworld

    引入依赖 compile 'org.springframework.boot:spring-boot-starter-data-redis' 使用redis有两种方法 1.Jedis Jedis je ...

  7. SpringBoot——探究HelloWorld【三】

    前言 前面我们写了helloworld的一个,这里我们对他进行分析 探究 那么下面就开始我们的探究之旅吧,首先从POM文件来,在POM文件中我们导入了项目所需要的依赖 POM文件 父项目 <pa ...

  8. spring-boot的helloWorld详解

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 3.2.5 2.Maven Plugin管理 pom.xml配置代码: <project xml ...

  9. eclipse springboot运行helloworld错误: 找不到或无法加载主类 xxx.xxx.xxx

    这个错误,在网上搜找了好久,说是什么jar包冲突,什么环境配置,我经过验证均是正确的,javac java java -version 都没问题,环境变量也OK,各种解释均没有能够解决我的问题,最后好 ...

随机推荐

  1. js+css制作简单的轮播图带有定时功能

    用纯css和JavaScript代码制作带有定时轮播功能的轮播图 <!DOCTYPE html> <html> <head> <meta charset=&q ...

  2. dp cf 20190615

    A. Timofey and a tree 这个不算是dp,就是一个思维题,好难想的思维题,看了题解才写出来的, 把点和边分开,如果一条边的两个点颜色不同就是特殊边,特殊边两边连的点就叫特殊点, 如果 ...

  3. 教你配置windows上的windbg,linux上的lldb,打入clr内部这一篇就够了

    一:背景 1. 讲故事 前几天公众号里有位兄弟看了几篇文章之后,也准备用windbg试试看,结果这一配就花了好几天,(づ╥﹏╥)づ,我想也有很多跃跃欲试的朋友在配置的时候肯定会遇到这样和那样的问题,所 ...

  4. Spring Cloud学习 之 Spring Cloud Hystrix(基础知识铺垫)

    Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 文章目录 前述: 快速入门: 命令模式: RxJava: 前述: ​ 在微服务架构中, ...

  5. python gdal 读取栅格数据

    1.gdal包简介 gdal是空间数据处理的开源包,其支持超过100种栅格数据类型,涵盖所有主流GIS与RS数据格式,包括Arc/Info ASCII Grid(asc),GeoTiff (tiff) ...

  6. 【HBase】带你了解一哈HBase的各种预分区

    目录 简单了解 概述 设置预分区 一.手动指定预分区 二.使用16进制算法生成预分区 三.将分区规则写在文本文件中 四.使用JavaAPI进行预分区 简单了解 概述 由上图可以看出,每一个表都有属于自 ...

  7. Java抽象类的学习体会与注意事项

    一.定义 抽象类:用abstract声明的class为抽象类. 抽象方法:用abstract声明的方法为抽象方法. 抽象方法特点:只有方法定义,没有方法的实现(函数体) 抽象类的子类都必须实现它的方法 ...

  8. 我的linux学习日记day7

    一.文件权限: r:read 读取文件列表的权限, 数字4表示 w:write 写入.删除.修改的权限,数字2表示 x:execute 进入目录的权限,数字1表示 权限分配:文件所有人.文件所属主.其 ...

  9. 「雕爷学编程」Arduino动手做(11)——金属触摸模块

    37款传感器和模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器与模块,依照实践出真知(动手试试)的理念,以学习和交流为目的,这里准备 ...

  10. 【python(deap库)实现】GEAP 遗传算法/遗传编程 genetic programming +

    目录 前言 1.优化问题的定义 单目标优化 多目标优化 2.个体编码 实数编码 二进制编码 序列编码(Permutation encoding) 粒子(Particles) 3 初始种群建立 一般族群 ...