1. SpringBoot02 运行原理初探 补充:
SpringBoot02
1.
2. 关于SpringBoot,谈谈你的理解
- 自动装配
- run方法
2. SpringBoot web开发:
jar:webapp!
回顾:
自动装配
springboot 到底帮我们配置了什么?我们能不能进行修改?能修改哪些东西?能不能扩展
- xxxxAutoConfiguration:向容器中自动配置组件
- xxxxProperties:自动装配类,装配配置文件中自定义的一些内容!
要解决的问题:
- 导入静态资源
- 首页
- jsp,模板引擎
- 装配和扩展SpringMVC
- 增删改查
- 拦截器
- 国际化
2.1 静态资源问题:
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
}
总结:
- 在springboot,我们可以使用以下方式处理静态资源:
-
webjars localhost:8080/webjars/
-
public static /** resources 映射到localhost:8080/
-
优先级:resources>static(默认)>public
2.2 首页和图标定制
2.2.1
templates目录下的所有页面,只能通过controller来跳转
index页面设置,需要模板引擎的支持 :thymeleaf
首页配置:
注意:所有页面的静态资源都需要使用thymeleaf接管;@{}
2.2.2 SpringBoot11:Thymeleaf模板引擎
结论:
只要使用thymeleaf,只需要导入对应的依赖就可以了!我们将html放在我们的templates目录下即可!
public static final String DEFAULT_PREFIX = ”classpath:/templates/“;
public static final String DEFAULT_SUFFIX = ”.html“;
pom依赖:
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
所有的HTML元素都可以被thymeleaf替换接管,th: 元素名
2.2.3 页面国际化
- 我们需要配置i18n文件
- 如果我们需要在项目中进行按钮自动切换,我们需要定义一个组件LocalResolver
- 记得将自己写的组件配置到Spring容器。@Bean
- #{}
2.3 如何快速搭建web应用
- 前端搞定:页面脏什么样子:数据
- 设计数据库(难点)
- 前端让它自动运行,独立化工程
- 数据接口如何对接:json,对象 all in one
- 前后端联调测试
2.3.1 自己准备
- 有一套自己熟悉的后台模板:工作必要!x-admin
- 前端界面:至少自己能够通过前端框架,组合出来一个网站页面
- index
- about
- blog
- post
- user
- 让这个网站能够独立运行
3. SpringBoot07:整合JDBC
spring:
datasource:
username: root
password: admin
#?serverTimezone=UTC解决时区的报错
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
4. SpringBoot09:整合MyBatis
补充配置文件application.properties
spring.datasource.data-username=root
spring.datasource.data-password=admin
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.type-aliases-package=com.kuang.pojo
mybatis.mapper-locations=classpath:mapper/*.xml
M:数据和业务
C:交接
V:HTML
- 导入包
- 编写配置文件
- mybatis配置
- 编写sql
5.业务层调用dao- controller调用service
SpingBoot所有的配置类,都有一个自动配置类
自动配置类都会绑定一个properties配置文件
5. SpringBoot17:Dubbo和Zookeeper集成
步骤:
- 提供者提供服务
- 导入依赖
- 配置注册中心的地址,以及服务器发现名,和要扫描的包
- 在想要被注册的服务上面~增加一个注解@Service
- 消费者如何消费
- 导入依赖
- 配置注册中心地址,配置自己的服务名
- 从远程注入服务
万变不离其宗:
分布式解决方案:
- API网关,服务路由
- HTTP RPC框架,异步调用
- 服务注册与发现,高可用
- 熔断机制,服务降级
为什么?本质:网络是不可靠的