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代替我们做了许多工作,概括来讲可以分为起步依赖和自动配置.这一篇先来看看起步依赖. 项 ...
随机推荐
- java程序中获取kerberos登陆hadoop
本文由作者周梁伟授权网易云社区发布. 一般我们在使用kbs登陆hadoop服务时都直接在shell中调用kinit命令来获取凭证,这种方式简单直接,只要获取一次凭证之后都可以在该会话过程中重复访问.但 ...
- C++实现-特征码遍历
#include <stdio.h> #include <stdlib.h> #include <windows.h> union Base { DWORD add ...
- 1.python的一些规范
Python的一些规范 1.标识符 定义:允许作为名字的有效字符串集合 名字必须有实际意义,可读性好 首字母必须是字母或下划线(_) 剩下的字符可以是字母和数字或者下划线 大小写敏感 两种风格:con ...
- webstorm keymap
http://www.jetbrains.com/webstorm/documentation/WebStorm_ReferenceCard.pdf
- SpringBoot入门之分散配置
springboot默认支持两种格式的配置文件:.properties和.yml.其中.properties是属性文件,也是最常用的一种:.yml是yaml格式的文件,yaml是一种简洁的标记语言.例 ...
- U8API——向U8数据库表导入数据
一.打开API资源管理器 替换两个引用 打开应用实例,选择相应的功能 复制相应的封装类到自己的目录下 在数据库新建临时表,与目标表相同 数据导入: 思路:先将要导入的数据导入到与U8目标表相同的临时表 ...
- 获取列表数据时,getList 设置默认参数:getList(page = 1),点击分页及前往时,传page参数,其他使用page的默认值:1
获取列表数据时,getList 设置默认参数:getList(page = 1),点击分页及前往时,传page参数,其他使用page的默认值:1
- python中del函数的垃圾回收
今天学习面向对象里的类被del函数的垃圾回收过程搞的晕头转向,经过了老师的讲解还是是懂非懂,然后看了很多博客慢慢的心里才有了个大概的了解. 刚刚看到一篇博客,觉得讲的很好,转载过来以供参考.以下转自笨 ...
- Postman—测试脚本
前言 对于Postman中的每个请求,我们都可以使用JavaScript语言来开发测试脚本.这也就好比单元测试.我们先看看Postman的相关界面: 编写测试脚本 Postman测试脚本本质上是在发送 ...
- js with 语句的用法
with 语句 为语句设定默认对象. with (object) statements 参数 object 新的默认对象. statements 一个或多个语句,object 是该语句的默认对象 ...