SpringBoot
2021-12-05 21:15:06 28 举报
AI智能生成
登录查看完整内容
SpringBoot 总结
作者其他创作
大纲/内容
SpringBoot
基础回顾
约定优于配置
概念
spring优点:轻量级EJB,实现了IOC、AOP
spring缺点:配置重,需要分析导入依赖。坐标版本选错了,严重阻碍开发进度。
SpringBoot解决spring所有问题
起步依赖:传递依赖,把坐标打在一起,提供默认功能
自动配置:自动将配置类的Bean注册ioc容器。对于主流开发框架无配置集成,提高开发、部署效率
SpringBoot 案例实现
使用Spring Initializr构建Spring Boot(需要联网)
创建controller
运行
解决中文乱码
@RequestMapping(produces = \"application/json; charset=utf-8\")
#设置响应utf-8spring.http.encoding.force-response=true
单元测试与热部署
添加依赖
编写测试方法(@RunWith(SpringRunner.class) @SpringBootTest)
热部署
引入依赖
idea配置(compiler->Build project automatically)
快捷键 Ctrl+Shift+Alt+/ ->compiler.automake.allow.when.app.running
全剧配置文件
application.properties配置文件可以是系统属性、环境变量、自定义配置
@ConfigurationProperties(prefix = \"person\") 表示为 配置文件中以person开头通过setXX注入到实体类中
实体类注入到ico容器中,这样 @ConfigurationProperties 赋值
application.yaml
普通数据类型
server: port: 8081 path: /hello
数组/单列集合
person: hobby: - play - read - sleep
配置文件属性值注入
@Value(\"${person.id}\") 直接给属性赋值
自定义配置
使用@PropertySource加载配置文件
@Component@PropertySource(\"classpath:test.properties\") //配置自定义配置文件的名称及位置@ConfigurationProperties(prefix = \"test\")
@Configuration自定义配置文件
使用配置类的方式向容器中添加组件配置,替代XML配置文件
在标注 @Configuration 类下的@Bean将返回值对象作为组件添加到容器中
随机数&参数引用
随机数
参数引用
app.name=MyAppapp.description=${app.name} is a Spring Boot application(注意要相互关联的属性)
源码剖析
依赖管理
spring-boot-starter-parent->spring-boot-dependencies配置了常用的框架的约定版本,进行版本统一管理如果pom.xml没有spring-boot-starter-parent依赖,则需要指定版本
spring-boot-starter-web提供了web开发所需的所有底层依赖框架像额外导入的web依赖文件和tomcat都由parent统一管理
第三方依赖starter需要写版本号->druid-spring-boot-starter
自动配置(启动流程)
@SpringBootApplication能够扫描组件,并自动配置
@SpringBootConfiguration底层是@Configuration,标注它是个配置类
@EnableAutoConfiguration
@AutoConfigurationPackage
@Import(AutoConfigurationPackages.Registrar.class)将Registrar类导入,作用是将所在包和子包组件扫描到容器中所以注意main程序路径位置
导入AutoConfigurationImportSelector告诉springboot 需要导入哪些组件可以帮助springboot应用将所有符合条件的@Configuration配置都加载到当前SpringBoot创建并使用的IoC容器(ApplicationContext)中
loadMetadata
读取spring-boot-autoconfigure.jar包中spring-autoconfigure-metadata.properties的信息生成urls枚举对象获得 PATH 对应的 URL遍历 URL 数组,读取到 properties 中解析urls枚举对象中的信息封装成properties对象并加载将 properties 转换成 PropertiesAutoConfigurationMetadata 对象根据封装好的properties对象生成AutoConfigurationMetadata对象返回
getAutoConfigurationEntry
getCandidateConfigurations()用来获取默认支持的自动配置类名列表将自动配置导入事件通知监听器,并触发fireAutoConfigurationImportEvents事件。创建 AutoConfigurationEntry 对象
总结
1. springboot应用启动2. @SpringBootApplication 起作用3. @EnableAutoConfiguration4. @AutoConfigurationPackage 扫描主配置同类以及子包,并将相应的组件导入到容器中5. @Import(AutoConfigurationImportSelector.class) 查找classpath 上所有jar包中的META-INF/spring.factories,进行容器创建。
@ComponentScan
包扫描,对@AutoConfigurationPackage的具体位置,从而的到主程序启动类所在的具体位置
自定义Stater
SpringBoot starter机制
不需要具体配置细节,引用对应starter,自动织入相应的Bean
命名规则
xxx-spring-boot-starter
创建步骤
1. 创建SimpleBean实体类,加入 @EnableConfigurationProperties(SimpleBean.class)// 使使用 @ConfigurationProperties 注解的类生效。@ConfigurationProperties(prefix = \"simplebean\") // 配置配置文件的前缀2. MyAutoConfiguration 创建自定义配置文件,增加@Configuration//标注配置类@ConditionalOnClass3. 把simpleBean 标注@bean加入配置文件中,加入到容器中4. 打包 并在测试项目中引用依赖,进行测试
执行原理
执行SpringApplication.run()即可启动整个Spring Boot 项目
run()内部源码
SpringApplication实例初始化创建(实例化)
//项目启动类 SpringbootDemoApplication.class设置为属性存储起来\t\tthis.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
//设置应用类型是SERVLET应用(Spring 5之前的传统MVC应用)还是REACTIVE应用(Spring 5开始出现的WebFlux交互式应用)\t\tthis.webApplicationType = WebApplicationType.deduceFromClasspath();
// 设置监听器(Listener) (添加到监听器容器,初始化完成通知)\t\tsetListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
// 初始化 mainApplicationClass 属性:用于推断并设置项目main()方法启动的主程序启动类\t\tthis.mainApplicationClass = deduceMainApplicationClass();
项目初始化启动,调用run方法
(1)获取并启动监听器\t\tSpringApplicationRunListeners listeners = getRunListeners(args);\t\tlisteners.starting();
// 准备Banner打印器 - 就是启动Spring Boot的时候打印在console上的ASCII艺术字体\t\t\tBanner printedBanner = printBanner(environment);
(3)创建Spring容器\t\t\tcontext = createApplicationContext();
(5):刷新容器\t\t\trefreshContext(context);
(7)发出结束执行的事件通知\t\t\tlisteners.started(context);
(9)发布应用上下文就绪事件listeners.running(context);
整体流程图
数据访问
整合Mybaits
1. 引入mybatis-starter 依赖2. 配置文件 url/username/password3. 注解整合@Mapper/@MapperScan(\"xxx\")->自动扫描 xml整合 编写mapper.xml & mybatis.mapper-locations=classpath:mapper/*.xml // 配置路径 & mybatis.type-aliases-package=com.zjn.pojo // 别名4. 测试5. mybatis.configuration.map-underscore-to-camel-case=true 开启驼峰
整合JPA
1. 添加starter-data-jpa依赖2. 实体类 增加 @Entity(name = \"t_comment\
整合Redis
1. 添加 starter-data-redis 依赖2. 实体类上添加@RedisHash(\"persons\"),指定存储空间 。@Indexed 标识二级索引3. Repository接口 继承 CrudRepository或者 JpaRepository(必须引入jpa&redis)
视图技术
不太支持常用的JSP模版,没有提供相应的整合原因如下:1. boot 默认以JAR打包进行项目部署,这种JAR包方式不支持JSP模版2. 使用Undertow 嵌入式容器,也不支持JSP3. boot 默认提供请求路径 “/error” 统一处理错误,使用JSP无法对默认页进行覆盖
Thymeleaf
语法
标准表达式
变量表达式${...}<p th:text=\"${title}\">xxx</p>
内置对象
选择变量表达式<div th:object=\"${book}\"><p>titile: <span th:text=\"*{title}\">标题</span>.</p></div>title 表示获取 boot 的titile 属性值
消息表达式#{...}国际化内容的动态展示和替换
链接表达式@{...} 引入资源<a th:href=\"@{/order/details(orderId=${o.id})}\">view</a>
片段表达式~{...} 标记一个片段模版,传递模版<div th:insert=\"~{thymeleafDemo::title}\"></div>使用th:insert 属性将title 引用进来
基本使用
引入Thymeleaf依赖
配置参数
静态资源访问从resources中public/resources/static三个子目录狭隘查找静态资源
配置国际化
在resources 狭隘创建i18n的文件夹,编写多语言配置文件
login.propertieslogin_zh_CN.propertieslogin_en_US.properties
编写配置文件 spring.messages.basename=i18n.logini18n表示国际化文件相对项目路径resources的位置,login 是前缀
自定义MyLocalResovel@Configurationpublic class MyLocaleResovel implements LocaleResolver将自定义MyLocaleResovel 注册一个类型的LocaleResoler 的Bean 组件
缓存管理
默认缓存管理
@EnableCaching 开启注解缓存支持@EnableCaching @SpringBootApplicationpublic class Springboot04CacheApplication
@Cacheable注解对数据操作方法进行缓存管理注解到service类的查询方法上
执行流程&时机
方法运行前,查询Cache缓存组件,按照name 获取,没有Cache 自动创建,SimpleKeyGenerator 生成Key默认策略
SPEL表达式
@CachePut 用于修改操作
@CacheEvict用于删除操作
整合Redis 缓存实现
支持9种缓存组件,有加载先后顺序
基于注解的Redis 缓存实现@Cacheable(cacheNames = \"comment\
spring.cache.redis.time-to-live=60000 // 缓存有效期配置
基于API的Redis 缓存实现@Autowiredprivate RedisTemplate redisTemplate;commentRepository.deleteById(comment_id);redisTemplate.delete(\"comment_\"+comment_id);
自定义Redis缓存序列化机制
自定义RedisTemplate
默认序列化机制,点RedisTemplate进入,在afterPropertiesSet()方法中,如果defaultSerializer 变量为空, 将数据默认序列化方式设置成JdkSerializationRedisSerializer
自定义RedisCacheManager
打开boot整合Redis自动配置类RedisCacheConfiguration(org.springframework.boot.autoconfigure.cache)包下的 ,仿照写
0 条评论
回复 删除
下一页