SpringBoot的 HelloWorld
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.运行主程序测试
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的更多相关文章
- springboot之HelloWorld
简介 为了简化开发Spring的复杂度,Spring提供了SpringBoot可以快速开发一个应用,这里就简单介绍下SpringBoot如何快速开发一个J2EE应用 HelloWorld 首先在gra ...
- SpringBoot学习helloworld
这几天开始学习springBoot记录一下(Hello World) pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0 ...
- SpringBoot的HelloWorld 应用及解释
参考链接: Spring Data JPA - Reference Documentation Spring Data JPA--参考文档 中文版 纯洁的微笑:http://www.ityouknow ...
- [一]SpringBoot 之 HelloWorld
(1)新建一个Maven Java工程 (2)在pom.xml文件中添加Spring BootMaven依赖 2.1在pom.xml中引入spring-boot-start-parent spring ...
- SpringBoot入门学习(一): Idea 创建 SpringBoot 的 HelloWorld
创建项目: 项目结构: 程序启动入口: 正式开始: package com.example.demo; import org.springframework.boot.SpringApplicatio ...
- redis整合springboot的helloworld
引入依赖 compile 'org.springframework.boot:spring-boot-starter-data-redis' 使用redis有两种方法 1.Jedis Jedis je ...
- SpringBoot——探究HelloWorld【三】
前言 前面我们写了helloworld的一个,这里我们对他进行分析 探究 那么下面就开始我们的探究之旅吧,首先从POM文件来,在POM文件中我们导入了项目所需要的依赖 POM文件 父项目 <pa ...
- spring-boot的helloWorld详解
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 3.2.5 2.Maven Plugin管理 pom.xml配置代码: <project xml ...
- eclipse springboot运行helloworld错误: 找不到或无法加载主类 xxx.xxx.xxx
这个错误,在网上搜找了好久,说是什么jar包冲突,什么环境配置,我经过验证均是正确的,javac java java -version 都没问题,环境变量也OK,各种解释均没有能够解决我的问题,最后好 ...
随机推荐
- 经典卷积神经网络算法(2):AlexNet
.caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...
- uniapp自定义简单省市区联动组件
又双叒一个uniapp组件 最近有一个选择地址的需求,就写了一个省市区联动选择器. 选择日期使用的picker,就照着它简单的整了一个,使用网络请求城市数据,还用到了vuex组件数据共享. 本来自己整 ...
- 036_python的大文件下载以及进度条展示
复习 1.黏包现象 粘包现象的成因: tcp协议的特点,面向流的,为了保证可靠传输,所以有很多优化的机制. 无边界 所有在连接建立的基础上传递的数据之间没有界限. 收发消息很有可能不完全相等. 缓存机 ...
- 一分钟明白MySQL聚簇索引和非聚簇索引
MySQL的InnoDB索引数据结构是B+树,主键索引叶子节点的值存储的就是MySQL的数据行,普通索引的叶子节点的值存储的是主键值,这是了解聚簇索引和非聚簇索引的前提 什么是聚簇索引? 很简单记住一 ...
- Day_11【集合】扩展案例1_遍历打印学生信息,获取学生成绩的最高分,获取成绩最高的学员,获取学生成绩的平均值,获取不及格的学员数量
分析以下需求,并用代码实现: 1.按照以下描述完成类的定义 学生类 属性: 姓名name 年龄age 成绩score 行为: 吃饭eat() study(String content)(content ...
- 用ArcGIS?37个Arcmap常用操作技巧可能帮到您
1. 要素的剪切与延伸 实用工具 TASK 任务栏 Extend/Trim feature 剪切所得内容与你画线的方向有关. 2. 自动捕捉跟踪工具 点击Editor工具栏中Snapping来打开Sn ...
- python--制作微信好友照片墙
知识来源:https://zhuanlan.zhihu.com/p/73975013 1.环境 os:MAC tool:python 3.7 ,pip3.7 2.前提: 使用pip3.7 instal ...
- search(14)- elastic4s-统计范围:global, filter,post-filter bucket
聚合一般作用在query范围内.不带query的aggregation请求实际上是在match_all{}查询范围内进行统计的: GET /cartxns/_search { "aggs&q ...
- 「雕爷学编程」Arduino动手做(33)——ESP-01S无线WIFI模块
37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里 ...
- mybatis 分页失败 始终pageSize = 2147483647
是在使用分页查询时 this.jobReadMapper.beginPager(pageParam);XXXXXXXXXXXXXXXXXXXXXXXXthis.xxxReadMapper.queryB ...