定位:SpringBoot框架是框架的框架,简化常规框架的配置的方式,只需要很少的代码,即可实现大量的功能。体现了开箱即用的思想。
2.pom.xml文件说明 pom.xml基本信息3.开箱即用原理说明(面试必问) 1.介绍com.jt springboot_demo1 0.0.1-SNAPSHOT springboot_demo1 Demo project for Spring Boot 1.8 UTF-8 UTF-8 2.3.7.RELEASE
SpringBoot将繁琐的配置封装到某些jar包中,该jar包中文件已经完成配置,引入即可,只需少量配置就可以获取其功能的方式叫做”开箱即用“。
2.开箱即用规则启动项:spring-boot-starter-web
说明:包中已经将框架进行了整合,用户直接用
org.springframework.boot spring-boot-starter-web
@EnableAutoConfiguration 启动自动化的配置
规则: 该注解的作用:用来加载SpringBoot-start-xxx的启动项,当主启动类执行时,则会开始加载启动项中的配置
主启动类的说明:@ComponentScan(“包路径”)
规则:当前包扫描路径默认就是主启动类所在的包路径
注意事项:写代码必须在主启动类所在包路径的 同包及子包中编辑
说明:SpringBoot项目中有多个配置文件,如果大量的重复的配置项都写到其中,则用户体验不好,如需要优化,则引入YML文件。
2.编辑application.yml说明:将application.properties改为application.yml
#基本语法 # 1.数据结构 key-value结构 # 2.写法 key: value 中间有空格 # 3.有层级结构 注意缩进 server: port: 80805.SpringBoot入门案例 1.入门案例
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello"+msg;
}
}
2.编辑properties文件
msg=张三3.编辑helloController类
@RestController
@PropertySource(value = "classpath:/msg.properties",encoding = "charset=utf-8")
public class HelloController {
@Value("${msg}")
private String msg;
@GetMapping("/hello")
public String hello(){
return "hello"+msg;
}
}
6.lombok插件介绍
1.lombok插件的介绍
常规开发中POJO类必须手写get/set/toString/构造…等方法,这类操作写起来鸡肋,不得不写,开发效率低。
所以可以引入lombok插件,自动生成上述方法
2.安装插件 2.准备User对象org.projectlombok lombok
package com.jt.pojo;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
@Data //get/set....方法
@Accessors(chain = true) //重写set方法
public class User implements Serializable {
private Integer id;
private String name;
}
3.编辑helloController类
@RestController
@PropertySource(value = "classpath:/msg.properties",encoding = "UTF-8")
public class HelloController {
@Value("${msg}")
private String msg;
@GetMapping("/hello")
public String hello(){
User user2 = new User();
//链式调用
User user = user2.setId(100).setName("xxxx");
System.out.println(user);
return "hello"+msg;
}
}
7.SpringBoot用法
1.Lombok常用注解
写POJO的四个注解
@Data //getter/setter/toString/Equal/RequiredArgsConstructor
@Accessors(chain = true) //重写set方法,可以实现链式加载
@NoArgsConstructor //自动添加无参构造方法
@AllArgsConstructor //全参构造方法
项目在Linux服务器部署,是否需要安装lombok插件? 不需要
lombok编译期有效,xxx.java文件编译xxx.class文件
SpringBoot为了简化测试过程,SpringBoot针对测试方法,开发一个注解@SpringBootTest
规则说明:
1.当运行@Test注解时,SpringBoot程序启动
2.SpringBoot启动,内部Spring容器启动,基于IOC管理对象,DI注入对象
3.可以在任意的测试类中获取想要的对象
注意事项:测试类的包路径,必须在主启动类的同包及子包中
axios.defaults.baseURL = "http://localhost:8080"
axios.get("/web/hello")
.then(promise => {
this.msg = promise.data
})
简化方式3:async await简化调用 重点
async function getHello(){
let promise = await axios.get("/web/hello")
alert(promise.data)
this.msg=promise.data
}
getHello()
继续简化
let {} 定义一个空对象
async function getHello(){
//let promise = await axios.get("/web/hello")
let {data:result} = await axios.get("/web/hello")
//alert(promise.data)
alert(result)
this.msg=result
}
getHello()
2.跨域问题
1.同源策略
要素:
1.浏览器URL中的地址 http://127.0.0.1:8848/webDemo/demo/3-axios.html
2.Ajax请求URL地址 http://localhost:8080/web/hello
要求:上述要素,首先必须满足协议/域名/端口号都相同时,满足同源策略
说明:
如果满足同源策略,则称之为 同域访问,反之称之为跨域访问,跨域访问浏览器一般都会报错
http协议默认端口号为80
http协议默认端口号为443
案例1:
浏览器地址:http://localhost:8080/xx/xxx
Ajax地址:https://localhost:8080/yy/yyy 跨域请求:协议不同
案例2:
前提:域名与IP地址对应
浏览器地址:http://www.jt.com:8080/xx/xxx
Ajax地址:http://10.4.5.0:8080/yy/yyy 跨域请求:域名不同
案例3:
浏览器地址:http://www.jt.com/xx/xxx
Ajax地址:http://www.jt.com:80/yy/yyy 同域请求
案例4:
浏览器地址:https://www.jt.com:443/xx/xxx
Ajax地址:https://www.jt.com/yy/yyy 同域请求
案例5:
浏览器地址:https://192.168.10.2:80/xx/xxx
Ajax地址:https:///192.168.11.2:80/yy/yyy 跨域请求
CORS要求在服务器端标识哪个网址可以访问我
解决:后端(服务器端)可以加@CrossOrigin注解 允许跨域没有限定
@CrossOrigin(value = “http://www.baidu.com”)



