SpringBoot补充

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));
            }

        }
    }

总结:

  1. 在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 页面国际化
  1. 我们需要配置i18n文件
  2. 如果我们需要在项目中进行按钮自动切换,我们需要定义一个组件LocalResolver
  3. 记得将自己写的组件配置到Spring容器。@Bean
  4. #{}

2.3 如何快速搭建web应用

  1. 前端搞定:页面脏什么样子:数据
  2. 设计数据库(难点)
  3. 前端让它自动运行,独立化工程
  4. 数据接口如何对接:json,对象 all in one
  5. 前后端联调测试

2.3.1 自己准备

  1. 有一套自己熟悉的后台模板:工作必要!x-admin
  2. 前端界面:至少自己能够通过前端框架,组合出来一个网站页面
  • index
  • about
  • blog
  • post
  • user
  1. 让这个网站能够独立运行

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

  1. 导入包
  2. 编写配置文件
  3. mybatis配置
  4. 编写sql
    5.业务层调用dao
  5. controller调用service

SpingBoot所有的配置类,都有一个自动配置类
自动配置类都会绑定一个properties配置文件

5. SpringBoot17:Dubbo和Zookeeper集成

步骤:

  1. 提供者提供服务
    1. 导入依赖
    2. 配置注册中心的地址,以及服务器发现名,和要扫描的包
    3. 在想要被注册的服务上面~增加一个注解@Service
  2. 消费者如何消费
    1. 导入依赖
    2. 配置注册中心地址,配置自己的服务名
    3. 从远程注入服务

万变不离其宗:
分布式解决方案:

  1. API网关,服务路由
  2. HTTP RPC框架,异步调用
  3. 服务注册与发现,高可用
  4. 熔断机制,服务降级

为什么?本质:网络是不可靠的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值