SSM框架虽然简化了我们的开发流程,但是它还是需要用spring去整合springMVC,myBatis,总体上配置还是比较复杂,于是springBoot出现了。
一,学习路线
二,基本了解:
1,什么是spring:为了解决企业级应用开发过程太复杂,进行简化开发的框架
2,Spring如何简化开发:
- 基于POJO的轻量级和最小侵入性编程
- 通过IOC,依赖注入和面向接口实现解耦
- 基于切面(AOP)和惯例进行切面式编程
- 通过切面和模板减少样式代码
3,springBoot不是一新的框架,它可以看成是spring的升级版,我们知道Maven整合了所有jar包,那么springBoot就是整合了所有框架。
4,springBoot的优点
- 使得所有的spring开发者更快入门
- 开箱即用,提供各种默认的配置来简化项目部署
- 内嵌式容器简化web项目
- 没有冗余代码生成和xml配置需求
5,什么是微服务:他是一种架构风格,将原来MVC三层架构进行更加细致的拆分。
三,SpringBoot入门
1,环境搭建
直接next后完成
访问:http://localhost:8080/ 如果有一个页面出现就成功了
四,自动配置原理
pom.xml文件
-
spring-boot-dependencies中配置了所有的jar包依赖,使得过程十分简单
- 启动器,想要开启响应的应用场景就配置对应的启动器(例如开启web服务)
-
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
主程序:
@SpringBootApplication//标志这个类是springBoot应用
public class AlipayApplication {
public static void main(String[] args) {
//启动springBoot应用
SpringApplication.run(AlipayApplication.class, args);
}
}
注解:
@SpringBootConfiguration:springBoot的配置
@Configuration:spring的配置
@Component:表明是一个组件
@EnableAutoConfiguration:springBoot自动配置
@AutoConfigurationPackage:自动配置包,
@Import({AutoConfigurationImportSelector.class}):导入选择器
spring.factories是自动配置的核心
结论: springboot所有自动配置都是在启动的时候扫描并加载: spring. factori es所有的自动配置类都在这里
面,但是不一定生效,要判断条件是否成立,只要导入了对应的start,就有对应的启动器了,有了启动器,我们
自动装配就会生效,然后就配置成功!
- springboot在启动的时候,从类路径下/META-INF/ spring. factories获取指定的值;
- 将这些自动配置的类导入容器,自动配置就会生效,帮我进行自动配置!
- 以前我们需要自动配置的东西,现在springboot帮我们做了!
- 整合javaEE,解决方案和自动配置的东西都在spring boot-autoconfigure-2.2.0.RELEASE.jar这个包下
- 它会把所有需要导入的组件,以类名的方式返回,这些组件就会被添加到容器;
- 容器中也会存在非常多的xxAutoConfiguration的文件(@Bean),就是这些类给容器中导入了这个场景需要的所有组件;并自动配置,@Configuration ,JavaConfig!
- 有了自动配置类,免去了我们手动编写配置文件的工作
五,yaml配置文件如何编写(实现数据注入POJO对象中)
application.yml文件——》People.java文件——》测试文件
#存普通的key-value
name: jianjiang
#存people对象
people:
name: jianjiang
age: 23
happy: false
birthDay: 1998/04/03
maps: {k1: v1,k2: v2}
lists: [code,music,basketball]
dog:
name: 大黄
age: 2
#存数组
names: [jianjiang,xiaomin,lili]
@Component
@ConfigurationProperties(prefix = "people")//实现注入的的关键
public class People {
private String name;
private Integer age;
private Boolean happy;
private Date birthDay;
private Map<String,String> maps;
private List<String> lists;
private Dog dog;
public People(){ }
public People(String name, Integer age, Boolean happy, Date birthDay, Map<String, String> maps, List<String> lists, Dog dog) {
this.name = name;
this.age = age;
this.happy = happy;
this.birthDay = birthDay;
this.maps = maps;
this.lists = lists;
this.dog = dog;
}
//get和set方法,toString方法,,,
public class TestAnimal {
@Autowired
private People people;
@Test
void testPeople(){
System.out.println(people);
}
}
六,自动转配原理
省略
七,web
- springBoot到底帮我们配置了什么?我们能不能进行修改?能不能进行一些扩展?
- XXXAutoConfiguration:向容器中自动配置组件
- XXXProperities:自动配置;类,装配配置文件中自定义的一些内容
- 要解决的问题:导入静态资源,首页,模板引擎Thymeleaf,装配扩展SpringMVC,增删改查,拦截器,国际化
1, 导入静态资源:
- webjars localhost:8080/webjars/
- public,static,/**,resources的目录可以配置静态资源,访问静态资源的路径是:localhost:8080/
- 优先级是:resources>static>public
2,thymeleaf模板引擎配置:
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>
<html xmlns:th="http://www.thymeleaf.org">
3,模板的使用:
//获取model中的文本值
<h1 th:text="${aaa}"></h1>
//遍历users
<h1 th:each="user:${users}">[[${user}]]</br></h1>
//css文件的引入
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
//图片文件的引入
<img th:src="@{/img/bootstrap-solid.svg}" >
//如果msg不为空,则显示他的内容
<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
八,提取公共页面:
- 将提取出来的模块,放在一个刚刚的地方,使用标识,表示他是可以引用的
-
<nav class="col-md-2 d-none d-md-block bg-light sidebar" th:fragment="sideBar">
引用的时候使用div加上一些表示,表示是这是相应代码的引用,可以用()来传参数
-
<div th:replace="~{commons/common::sideBar(active='main.html')}"></div>
循环遍历session域中的内容
-
<tbody> <tr th:each="emp:${emps}"> <td th:text="${emp.getId()}"></td> <td th:text="${emp.getLastName()}"></td> <td th:text="${emp.getEmail()}"></td> <td th:text="${emp.getGender()==0?'女':'男'}"></td> <td th:text="${emp.getDepartment()}"></td> <td th:text="${#dates.format(emp.getBirth(),'yyyy-MM-dd HH:mm:ss')}"></td> <td> <button class="btn btn-sm btn-primary">编辑</button> <button class="btn btn-sm btn-danger">删除</button> </td> </tr> </tbody>