SpringBoot 基础知识点

tech2023-02-19  106

day1: .///错误经验input length1 :编码未配置,/1/设置utf-8, /2/把yml配置文件重新删除配置。 1.Spring Boot关于自动配置的源码在spring-boot-autoconfigure-x.x.x.x.jar中 2.@EnableAutoConfiguration,就是开启自动配置,主要功能由@Import实现导入的AutoConfigurationImportSelector方法 通过SpringFactoriesLoader.loadFactoryNames()扫描所有具有META-INF/spring.factories的jar包返回类实现 3.@ConfigurationProperties,它的作用就是从配置文件中绑定属性到对应的bean上, 而@EnableConfigurationProperties负责导入这个已经绑定了属性的bean到spring容器中 4.而诸多的XxxxAutoConfiguration自动配置类,就是Spring容器的JavaConfig形式, 作用就是为Spring 容器导入bean,而所有导入的bean所需要的属性都通过xxxxProperties的bean来获得 5.Conditional条件注解判断这个Bean多个条件是否被满足 6.自定义配置文件设置,@PropertySource(value=“classpath: bobo.properties”)这里就不需要@ConfigurationProperties注解绑定yml了 7.不建议使用@Value注解,建议在application.yml中配置 8.松散绑定:firstName(实体类) first-name(yml) 这两个可以绑定赋值///(-)后面的默认大写!! 9.JSR303校验: @Validation(主要掌握正则表达式)例子:message属性 @NotNull(message = “reason信息不可以为空”) 注解 @SpringBootApplication//springboot核心注解 @Component//组件允许被扫面 @ConfigurationProperties(prefix = “person”)//声明yml配置文件中的对象 @Value(“旺财”)//赋值 @Autowired//自动装配

day2: 1.配置文件的优先级: file:/config file:/ classpath:/config classpath:/(当前我们使用的位置) 2.用一个yml实现多块配置用 — 分隔开,yml中的:后面要跟一个空格标准 server : port : 8080 spring: profiles: active: dev

server: port: 8081 spring: profiles: dev

server: port:8082 spring: profiles: test 3.自动装配原理: (1)spring boot启动加载大量的类 (2)我们需要的组件是否在默认的自动配置类当中,有就不配置,没有就手动配置 (3)给容器中添加组件,会从properties中获取,就是相当于改变properties就可以了(yml) (4)XxxAutoConfiguration :自动配置类在WEB-INF/s-pconfigure.jar包下, 他 通过autoConfigurationproperties 与XXXproperties绑定,properties中有组建的i相关属性, 可以改相关联的配置文件就是yml来修改属性。

4.thymeleaf依赖: org.thymeleaf thymeleaf-spring5 org.thymeleaf.extras thymeleaf-extras-java8time 命名空间:xmlns:th=“http://www.thymeleaf.org” 5.thymeleaf模板引擎:html放在template 6.静态资源导入,配置优先级问题:, public resource static <1>resource <2> static <3> public (公共资源的最后) 7.thymeleaf语法: (1)th:text 用来将内容输出到所在标签的body中。 th:text="${变量}" 获取变量值 (2)#{home.welcome} 用来引入数据home对象中的 welcome属性。 (3)可以用th:utext 用来显示“unescaped ” 的html内容 8.springboot mvc扩展与原理:编写一个MyMvcConfig ,用@Configuration 标注是一个配置类,(注意: 不能标注@EnableWebMvc,标注之后,有一个conditionalMiss的注解会失效,产生support,WebMvcAutoConfiguration,自动配置会失效) 继承WebConfigurerAdapter,实现其中的某一个方法,之后依靠spring自动配置类WebMvcAutoConfiguration 中的WebMvcAutoConfigurationAdapter通过@Import导入内部类EnableWebMvcConfiguration.class, 继承DelegatingWebMvcConfiguration类。

最新回复(0)