SpringBoot入门(一)

什么是SpringBoot?
SpringBoot是Spring的一个子框架。

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.
SpringBoot可以很容易的创建一个单例,以Spring为基础的产品级的应用。你只需要运行就可以了。

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.
我们可以用固定的方式用spring的平台和第三方的库,开启应用。大多数的springboot的应用需要很少的配置。

SpringBoot依赖Spring的平台和第三方的库,可以快速的创建Spring的项目,且“零”配置。

为什么要学习SpringBoot?
我们之前创建Spring的项目,需要很多的配置,还需要使用很多的依赖。而我们springBoot从这两个角度进行了简化。

用两种方式创建SpringBoot项目
方式一:
1.创建一个maven项目
2.添加一个父依赖

org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE

3.添加web依赖


org.springframework.boot
spring-boot-starter-web


4.设定jdk的版本

<java.version>1.8</java.version>

5.创建一个Springboot的启动类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
6.写一个controller
@RestController
public class HelloController {

@GetMapping("/hello")
public String hello(){
    return "hello spring boot";
}

}

方式二:
1创建SpringInit项目,选择web依赖
2.创建Controller
@RestController
public class Hello02Controller {

@GetMapping("/hello02")
public String hello02(){
    return "hello springboot02";
}

}

SpringMVC和SpringBoot之间的关系
SpringBoot是Spring的升级?不是。SpringBoot只是改变了使用Spring的方式。

SpringBoot的特点
1.化繁为简(编码简单,配置简单,部署简单,监控简单)
2.备受关注的新一代框架
3.微服务的入门级框架(SSM–>SpringBoot–>SpringCloud)

使用SpringBoot获得参数,内置对象
//http://localhost:8080/bbb?cid=1
@GetMapping("/bbb")
public String getParam01(@RequestParam(“cid”) int cid){
System.out.println(“cid=”+cid);
return “getParam method”;
}

//使用restful风格访问
http://localhost:8080/bbb/1
@GetMapping("/ccc/{did}")
public String getParam02(@PathVariable int did){
System.out.println(“did=”+did);
return “getParam method”;
}

@GetMapping("/ddd")
public String getObject(HttpServletRequest request , HttpServletResponse response){
System.out.println(request+"----"+response);
return “getObject method”;
}

SpringBoot的属性文件
修改端口 server.port=8081

修改访问路径 server.servlet.context-path=/mypath

是推荐方式

总结
SpringBoot是Spring的一个子框架。SpringBoot并不是spring的升级,而是改变使用的方式。它化繁为简,是微服务入门框架,是springcloud的基础。它能够快速创建Spring项目,不需要很多的依赖,而使用启动器,不需要配置文件,基本“零”配置。
Springboot项目继承parent启动器,使用web支持的时候,使用web启动器。Springboot是基于jdk1.8及以上。它需要一个启动类,其他内容之前springmvc的一致。比如获得参数用到@getMapping @RequestParam @pathVariable。
Springboot支持使用yml的属性文件,可以配置参数和访问路径。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值