需要学习的内容
约定大于配置 高内聚,低耦合
微服务论文中文版: 微服务论文(马丁·福勒) - 简书
给属性赋值的几种方式
- 通过@Value注解
- 使用 @ConfigurationProperties(prefix="")赋值
通过 @ConfigurationProperties(prefix="person") 注解绑定 .yml配置文件属性进行绑定赋值;
使用 @ConfigurationProperties 前提是 这个组件是容器中的组件
多环境配置及配置文件位置
- 自动装配原理
- META-INF 下的srping.factories文件里面很多 xxxAutoConfiguration;
- xxxAutoConfiguration类里面是 @EnableConfigurationProperties
- @EnableConfigurationProperties 里面使用的是 @ConfigurationProperties
- 注解 @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 {}