SpringBoot 入门(一)
1. 什么是SpringBoot?
SpringBoot是一个快速开发框架,能够帮我们快速整合第三方框架(Maven依赖关系##Maven继承),完全采用注解化,简化XML配置,内置嵌入Http服务器(Tomcat、Jetty),默认嵌入Tomcat服务器。最终以java应用程序进行执行。(SpringBoot项目中没有web.xml)
2. SpringBoot与SpringCloud的关系
SpringCloud是一套目前比较完整的微服务框架,功能非常强大。注册中心、客户端调用工具、服务治理(负载均衡、断路器、分布配置中心、网关、服务链路、消息总线等)
关系:SpringBoot + SpringCloud 实现微服务开发,使用Springmvc实现微服务接口
SpringBoot 实现快速开发
3. SpringBoot与SpringMVC的关系
SpringBoot web组件集成了SpringMVC框架。
4. 用SpringBoot写一个 HelloWorld
- 新建一个Maven工程

- 在pom.xml中添加依赖信息
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wanwei</groupId>
<artifactId>springboot2.0-helloword</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- spring-boot-starter-parent 整合第三方常用框架依赖信息(各种依赖信息) -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<!-- spring-boot-starter-web是springboot整合SpringMVC 实现原理:Maven依赖继承关系 -->
<!-- 相当于把第三方常用maven依赖信息在parent项目中已经封装好了,使用springboot提供依赖信息关联整合的jar包 -->
<!-- springboot 中快速处理原理(Maven子父依赖关系),springboot对常用的依赖信息进行封装 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 因为parent里面已经封装了版本号,所以这里不需要再写版本号 -->
</dependency>
</dependencies>
</project>
- 创建一个Controller
package com.wanwei.index.controller; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*SpringBoot启动原理:SpringMVC注解方式启动,内嵌http服务器(默认是Tomcat)
@EnableAutoConfiguration注解作用:开启自动配置,默认只扫描当前类
*
*/
@RestController
@EnableAutoConfiguration
public class MemberController { //RestController注解表示该类的所有方法都返回json格式 @RequestMapping("/memberIndex")
public String memberIndex(){
return "SpringBoot的第一个案例";
} public static void main(String[] args) {
//整个程序的入口,启动SpringBoot
SpringApplication.run(MemberController.class, args);
}
}
这样一个简单的SpringBoot Holloworld就写好了,运行后在浏览器访问:http://localhost:8080/memeberIndex
5. @ComponentScan 注解
上面的项目中只有一个Controller,但我们实际开发的项目中往往是有多个Controller,而 @EnableAutoConfiguration 注解只扫描当前类,这时候我们可以新建一个类将启动代码抽取出来,并使用 @ComponentScan 注解设置扫包范围。

package com.wanwei.index.controller; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RestController;
/**
*
* 将启动代码单独出来运行
* @ComponentScan注解作用:包扫描器,设置扫包范围
*/
@RestController
@EnableAutoConfiguration
@ComponentScan("com.wanwei.index.controller")
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
这时候我们再增加一个Controller
packagecom.wanwei.index.controller; import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@RequestMapping("/index")
public String index(){
return "SpringBoot2.0 index";
}
public static void main(String[] args) {
SpringApplication.run(IndexController.class, args);
}
}
运行后在浏览器访问:http://localhost:8080/memeberIndex 和 http://localhost:8080/index 都可以成功。
6.@SpringBootApplication 注解
在实际项目中不同的Controller一般会有不同的包,那我们在设置扫包范围时就需要这么写:@ComponentScan(basePackage = {"com.wanwei.index.controller","com.wanwei.order.controller"})但是如果包太多这样写又很麻烦,这时候我们就可以使用@SpringBootApplication注解,(它的作用其实就等同于@EnableAutoConfiguration + @ComponentScan),默认扫描当前包和当前包的同级包、子包。
- 新建一个包,在此包下创建一个Controller,然后把启动类App放在com.wanwei包下(保证了启动类和其他Controller类都在同级包下)

package com.wanwei.order.controller; import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
@RequestMapping("/order")
public String order(){
return "使用@SpringApplication注解";
}
public static void main(String[] args) {
SpringApplication.run(OrderController.class, args);
}
}
- 在启动Controller的类中注释掉@EnableAutoConfiguration 和 @ComponentScan,添加@SpringBootApplication注解
package com.wanwei; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
*
* 将启动代码单独出来运行
* @ComponentScan注解作用:设置扫包范围
*/
//@EnableAutoConfiguration
//@ComponentScan("com.wanwei.index.controller")
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
运行代码,所有页面都能访问成功。
SpringBoot 入门(一)的更多相关文章
- SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1
在之前的博文<详解intellij idea搭建SpringBoot>介绍了idea搭建SpringBoot的详细过程, 并在<CentOS安装Tomcat>中介绍了Tomca ...
- SpringBoot入门基础
目录 SpringBoot入门 (一) HelloWorld. 2 一 什么是springboot 1 二 入门实例... 1 SpringBoot入门 (二) 属性文件读取... 16 一 自定义属 ...
- SpringBoot入门示例
SpringBoot入门Demo SpringBoot可以说是Spring的简化版.配置简单.使用方便.主要有以下几种特点: 创建独立的Spring应用程序 嵌入的Tomcat,无需部署WAR文件 简 ...
- Spring全家桶系列–[SpringBoot入门到跑路]
//本文作者:cuifuan Spring全家桶————[SpringBoot入门到跑路] 对于之前的Spring框架的使用,各种配置文件XML.properties一旦出错之后错误难寻,这也是为什么 ...
- springboot入门之一:环境搭建(续)
在上篇博客中从springboot的入门到运行一个springboot项目进行了简单讲述,详情请查看“springboot入门之一”.下面继续对springboot做讲述. 开发springboot测 ...
- 【Java】SpringBoot入门学习及基本使用
SpringBoot入门及基本使用 SpringBoot的介绍我就不多说了,核心的就是"约定大于配置",接下来直接上干货吧! 本文的实例: github-LPCloud,欢迎sta ...
- SpringBoot入门(三)——入口类解析
本文来自网易云社区 上一篇介绍了起步依赖,这篇我们先来看下SpringBoot项目是如何启动的. 入口类 再次观察工程的Maven配置文件,可以看到工程的默认打包方式是jar格式的. <pack ...
- SpringBoot入门(五)——自定义配置
本文来自网易云社区 大部分比萨店也提供某种形式的自动配置.你可以点荤比萨.素比萨.香辣意大利比萨,或者是自动配置比萨中的极品--至尊比萨.在下单时,你并没有指定具体的辅料,你所点的比萨种类决定了所用的 ...
- SpringBoot入门(四)——自动配置
本文来自网易云社区 SpringBoot之所以能够快速构建项目,得益于它的2个新特性,一个是起步依赖前面已经介绍过,另外一个则是自动配置.起步依赖用于降低项目依赖的复杂度,自动配置负责减少人工配置的工 ...
- SpringBoot入门(二)——起步依赖
本文来自网易云社区 在前一篇我们通过简单几步操作就生成了一个可以直接运行的Web程序,这是因为SpringBoot代替我们做了许多工作,概括来讲可以分为起步依赖和自动配置.这一篇先来看看起步依赖. 项 ...
随机推荐
- 给XCode安装Alcatraz(包管理工具)!!
Alcatraz官方描述: Alcatraz is an open-source package manager for Xcode. It lets you discover and instal ...
- MySQL数据库中的字段类型varchar和char的主要区别是什么?哪种字段查找效率要高?
1,varchar与char的区别?(1)区别一,定长和变长,char表示定长,长度固定:varchar表示变长,长度可变.当插入字符串超出长度时,视情况来处理,如果是严格模式,则会拒绝插入并提示错误 ...
- Storm Trident详解
Trident是基于Storm进行实时留处理的高级抽象,提供了对实时流4的聚集,投影,过滤等操作,从而大大减少了开发Storm程序的工作量.Trident还提供了针对数据库或则其他持久化存储的有状态的 ...
- POJ 1050
#include <stdio.h> #include <string.h> #define mt 101 int main() { int a[mt][mt]; int st ...
- jQuery过滤选择器:first和:first-child的区别,CSS伪类:first-child
最近项目中遇到需求:只在第一列不能删除,不显示小叉号:点击可添加一列,后面的列右上角显示小叉号,可以点击删除. 我是使用以下方法解决这个小需求 :CSS伪类选择器:first-child设置所有小叉号 ...
- 搭建Nuget.Server push时,"Failed to process request. 'Method Not Allowed'"
环境: windows server 2012,已经安装web dev工具. nuget.server版本2.11 输入网站地址正常访问: VS也能正常添加,nuget服务,在项目打包后上传服务器时报 ...
- Java之集合(十七)ConcurrentLinkedQueue
转载请注明源出处:http://www.cnblogs.com/lighten/p/7491057.html 1.前言 ConcurrentLinkedQueue是一个无界的线程安全队列,遵循FIFO ...
- Java之集合(四)Vector和Stack
转载请注明源出处:http://www.cnblogs.com/lighten/p/7296023.html 1.前言 本章介绍Java集合List中的Vector和其子类Stack.Vector类是 ...
- JavacProcessingEnvironment类解读
JavacProcessingEnvironment类的继承体系如下: 其中含有很多内部类,最重要的是迭代注解处理器相关的类,如下:
- JPA主键生成策略
@GeneratedValue: 为一个实体类生成一个唯一标识的主键(JPA要求每一个实体Entity,必须有且只有一个主键).它有两个属性,分别是strategy和generator. genera ...
://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">