SpringBoot 基础学习

需要学习的内容

约定大于配置    高内聚,低耦合

微服务论文中文版:  微服务论文(马丁·福勒) - 简书

给属性赋值的几种方式

  • 通过@Value注解

  • 使用 @ConfigurationProperties(prefix="")赋值

通过 @ConfigurationProperties(prefix="person") 注解绑定 .yml配置文件属性进行绑定赋值;

使用 @ConfigurationProperties 前提是 这个组件是容器中的组件

多环境配置及配置文件位置

  1. 自动装配原理

  1. META-INF 下的srping.factories文件里面很多 xxxAutoConfiguration;
  2. xxxAutoConfiguration类里面是 @EnableConfigurationProperties
  3. @EnableConfigurationProperties 里面使用的是 @ConfigurationProperties
  4. 注解 @ConfigurationProperties(prefix="") 使用在 xxxProperties类上,通过注解前缀绑定到对应类,对类的属性进行赋值

赋值:

查看哪些组件生效 debug: true

  • 静态资源导入
    • 可以直接访问的静态资源存放路径

静态资源访问的优先级就是按照下面这个顺序

new String[]{

"classpath:/META-INF/resources/",

"classpath:/resources/",

 "classpath:/static/",

"classpath:/public/"}

静态资源可以放到这些目录下,就可以访问到;

直接访问:

首页和图标定制

引入图标:根据查看 WebMvcAutoConfiguration.class 类的源码

可以得知 classpath: /resources , classpath: /public 下的文件可直接访问,不需要加具体路径

<link rel="icon" type="image/x-icon" href="favicon.ico" />

注意:命名必须是 favicon.ico

Thymeleaf模版引擎

templates下的页面只能通过Controller跳转,这个需要模版引擎 Tymeleaf的支持。读源码得知,thymeleaf模版引擎会直接读取 classpath:/templates/ 且后缀为 .html 下的文件。

Thymeleaf语法

引入thymeleaf依赖

<!--thymeleaf 模版引擎-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>2.3.12.RELEASE</version>
</dependency>

引入命名空间

<html lang="en" xmlns:th="http://www.thymeleaf.org">

语法

-- 取值

<div th:text="${msg}" /> 

-- 遍历
<div th:each="name:${list}" th:text="${name}" />

SpringMVC自动扩展

使用 @Configuration 注解 +  实现 WebMvcConfigurer 接口,重写你要使用的方法;

注:其他的功能扩展同理;

@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值