文章目录
SpringBoot目录结构
01、构建springboot的项目的准备工作
- maven3.6/gradle
- jdk1.8+
- idea
02、使用idea快速构建springboot项目
-
SpringBoot的底层还是spring的ioc的机制来完成的bean的注入
- 你遵循规则
- SpringBoot设计的初衷:简化开发流程,并且去掉传统基于xml的方式的注入bean机制
- 一句话:约定大于配置,其实底层还是spring的机制。
-
什么是约定大于配置
-
如果你要完成springboot项目的开发,就必须所有的包和所有的类都必须放在启动类的包的下面即可。
-
springboot如何去除xml配置呢?
-
我们知道在传统的spring开发中,注入bean的方式是xml+
applicationContext.xml
<import file="application-UserConfiguration.xml"/> <import file="application-CurseConfigurtation.xml"/>
application-UserConfiguration.xml如下:
<beans xxxx> <bean id="userService" class="com.lya.service.UserServiceImpl"/> </beans>
-
用面向对象的方式来进行替代传统的xml+的方式,就是:@Configuration+@Bean的方式
package com.lya.config; import com.service.UserServiceImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author lyayykk * @title: UserConfiguration * @projectName springboot-music * @description: TODO */ @Configuration public class ApplicationContextUserConfiguration { X }
-
-
-
问题1:它如何把bean注入springioc的容器的呢?是怎么做到的?
-
问题2:如果我不遵循规则,能不能完成注入springioc容器中呢?@Import机制
SpringMvc的路由和参数的注入方式
01、路由
路由:其实就是访问资源的一个路径,而这个路径其实是让你自定去定义的。
其实你访问每天在电脑中访问的磁盘目录和文件它其实就是一个路由定义和访问资源的方式,只不过这个遵循的协议是:file文件协议:
- D:\xingqiublog\lya-test\src === files:// D:\xingqiublog\lya-test\src\a.txt
sprignmvc的路由认识:
定义的方法,路由方法,这个方法作用访问服务器资源:
资源分为两种:
1: 磁盘文件资源
2: 返回服务内存数据资源
底层:两者都是通过io流进行返回:response.getWriter进行返回 response是一个输出流OutPutStream
遵循的协议是:http协议,访问的时候就是:http://ip:port/路径(路由)
@GetMapping("/a/b")
public String index(){
}
http://ip:port/a/b ----- z自动去查找/templates/index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
request.setAttribute("name","yykk")
<h1>我是首页</h1> ${name}
</body>
</html>
+
freemaker
response.setContentType("content-type","text/html;charset=utf-8")
response.getWirter.println("<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>我是首页 </h1> yykk
</body>
</html>")
我是首页 yykk
02、关于注解的说明
@RequestMapping
它是路由定义的映射规则注解,这个注解包含了:http协议中所有的请求都可以访问。比如:
GET,
POST,
PUT,
DELETE,
HEAD,
PATCH,
OPTIONS,
TRACE;
以上的所有请求方式都可以进入此方法。
Restful规则
它是对springmvc路由请求一种规范和约束。主要是为了让开发更加明确的知道你请求的路由方法,到底是做什么样子的功能,restful规则的提供其实就起到一个约束和明确需求功能一个作用:
查询 --- get
添加 --- put
修改 --- post
删除 --- delete
package com.lya.controller;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
/**
* @author lyayykk
* @title: IndexController
* @projectName springboot-music
* @description: TODO
*/
@Controller
public class IndexController {
//@RequestMapping(value = "/a/c",method = RequestMethod.PUT)
@PutMapping("/a/c")
public String save(){
System.out.println("添加功能");
return "save";
}
//@RequestMapping(value = "/a/f",method = RequestMethod.DELETE)
@DeleteMapping("/a/f")
public String delete(){
System.out.println("删除功能");
return "save";
}
//@RequestMapping(value = "/a/d",method = RequestMethod.POST)
@PostMapping("/a/d")
public String update(){
System.out.println("更新功能");
return "save";
}
// 定义的方法,路由方法,这个方法作用访问服务器资源:
// 资源分为两种:
// 1: 磁盘文件资源 规则就是必须放在:/resources/templates/index.html
// 2: 返回服务内存数据资源
// 底层:两者都是通过io流进行返回:response.getWriter进行返回 response是一个输出流OutPutStream
// 遵循的协议是:http协议,访问的时候自然:http://ip:port/路径(路由)
//@GetMapping("/a/b")
//@RequestMapping(value = "/a/b",method = RequestMethod.GET)
@GetMapping("/a/b")
public String index(){
System.out.println("查询功能");
return "index";
}
}
02、参数注入
bean 单参数,多参数,list,数组,map ,混合
a/b",method = RequestMethod.GET)
@GetMapping("/a/b")
public String index(){
System.out.println(“查询功能”);
return “index”;
}
}
## 02、参数注入
bean 单参数,多参数,list,数组,map ,混合