Spring 框架就像一个家族,有众多衍生产品例如 boot、security、jpa等等。但他们的基础都是Spring 的 ioc和 aop。
ioc 提供了依赖注入的容器 。aop 解决了面向横切面的编程,然后在此两者的基础上实现了其他延伸产品的高级功能。
Spring MVC是基于 Servlet 的一个 MVC 框架 主要解决 WEB 开发的问题,因为 Spring 的配置非常复杂,各种XML、 JavaConfig处理起来比较繁琐。
于是为了简化开发者的使用,从而创造性地推出了Spring boot,约定优于配置,简化了spring的配置流程。
说得更简便一些:Spring 最初利用“工厂模式”(DI)和“代理模式”(AOP)解耦应用组件。
大家觉得挺好用,于是按照这种模式搞了一个 MVC框架(一些用Spring 解耦的组件),用于开发 web 应用( SpringMVC )。
然后又发现每次开发都写很多样板代码,为了简化工作流程,于是开发出了一些“懒人整合包”(starter),这套就是 Spring Boot。
springBoot一些特征
创建独立的Spring应用程序
直接嵌入Tomcat,Jetty或Undertow(无需部署WAR文件)
提供自以为是的“入门”依赖项,以简化构建配置
尽可能自动配置Spring和3rd Party库
提供可用于生产的功能,例如指标,运行状况检查和外部化配置
完全没有代码生成,也不需要XML配置
简单入门案例
https://spring.io/quickstart
注意不要选快照
package com.example.demo
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResController;
@SpringBootAppliction
@ResController
public class DemoAppliction{
public statid void main(String[] args){
SpringApplication.run(DemoAppliction.calss,args)
}
@GetMapping("/hello")
public String hello(@RequestParam(value="name",defaultValue="world")String name){
return String.format("Hello %s!",name)
}
}
hello()我们添加的方法旨在采用名为的String参数name,然后将此参数与"Hello"代码中的单词组合。这意味着,如果您“Amy”在请求中将姓名设置为,则响应为“Hello Amy”。
该@RestController注解告诉Spring,这个代码描述应该可在网上的端点。该@GetMapping(“/hello”)告诉Spring使用我们的hello()方法来回答这个问题被发送到请求http://localhost:8080/hello的地址。最后,@RequestParam告诉Spring name在请求中期望一个值,但是如果不存在,默认情况下它将使用单词“ World”。
步骤三在window上用cmd 运行springboot
后两行告诉我们Spring已经开始。Spring Boot的嵌入式Apache Tomcat服务器充当Web服务器,并在localhostport 上侦听请求8080。打开浏览器,然后在顶部的地址栏中输入http:// localhost:8080 /你好。您应该得到一个很好的友好响应,