SpringBoot简单尝试
一、spring boot核心
配置在类路径下autoconfigure下(多瞅瞅)
@SpringBootApplication里的重要注解(@Configuration,@EnableAutoConfiguration,@ComponentScan三个注解的组合。)
@ComponentScan:默认就会装配标识了@Controller/@Service/@Component/@Repository注解的类到spring容器中
@Component是一个元注解,意思是可以注解其他类注解泛指组件,当组件不好归类的时候,我们可以使用这个
我们可以使用这个注解进行标注,作用就相当于XML配置<bean id="" class=""></bean>
@EnableAutoConfiguration: 开启自动装配
@AutoConfigurationPackage:自动配置包
@Import({Registrar.class}):给容器中导入组件(扫描启动类同级的包及子包的所有组件扫描到spring容器中)
(规定包只能在启动类同级目录)
@Import({AutoConfigurationImportSelector.class}):导入容器所需要的组件,并配置好组件(使用x组件,配置好x)
@SpringBootConfiguraztion: 支持JavaConfig的方式来进行配置(使用Configuration配置类等同于XML文件)。
@Configuration: @configuration标注配置类 = mvc写的配置文件(也是组件)
@Component: 说明是一个spring组件
Springboot在启动时候从类路径的 META-INF/spring.factories 中获取EnableAutoConfiguration指定的值,将这些值
作为自动配置类导入到容器中
二、springboot配置(约定大于配置)
1、基本配置
(1)、POM
//启动器 都以spring-boot-starter开头不用写版本,有版本管理库,定义spring的应用场景,下面是springboot在web的应用场景
(导入web环境所有依赖)
//官网有各种启动器 可以查看pom首个parent(groupId)=》parent(groupId)=》parent 就是springboot版本管理
// (没有的需要手动声名版本)
//可以进去看启动器的具体场景应用依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
(2)、静态资源配置
搜索查看WebMvcAutoConfiguration配置类即可看到添加资源处理器(addResourceHandlers),根据源码可以在resources下
创建public和resources优先级为(public < static < resources)
(3)、Ico图标配置
放在static下即可加载
(4)、404,500配置
在templates下创建error文件夹里面放404.html 500.html即可
2、自定义配置(Yml)
//基本语法
yaml:
person:
age: 18
name: 张三
lis:
- 第一个
- 第二个
mas: {money: 9999999}
veges:
color: 绿色
type: 黄瓜
指定开发环境:
1.spring.profiles.active=dev/*激活application-dev.properties*/
2.yml支持文档块,以---(三个减号)回车分模块 spring:profiles:dev
spring:profiles:active: dev
配置文件优先级:
classpath:==》classpath:config/ ==》file:/==》file:config/
3、日志配置
(1)、POM日志简单配置
logging.file.path:d:/mine //保存日志
logging.pattern.console //控制台输入日志格式
logging.pattern.file //保存日志时候日志的格式
//yml
#指定日志等级并保存日志到path 不加字符直接写格式报错
logging:
file:
path: d:/logs
level:
cn.bdqn: info
pattern:
console: 日志:%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
file: 日志:%msg%n
(2)、日志格式
%d表示日期时间
%thread表示线程名
%-5level:级别从左显示5个字符宽度
%logger{50}表示logger名字最长50个字符,否则按照句点分割
%msg:日志消息
%n:换行符
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
属性 解释
logging.config= 日志配置文件的位置。例如,classpath:logback.xml。
logging.exception-conversion-word=%wEx 转换异常时使用的转换字。
logging.file= 设置保存日志的日志文件
logging.file.max-history=0 are neat
logging.file.max-size=10MB 设置日志文件最大大小
logging.level.(cn.bdqn)= 设置日志等级
logging.path= 日志文件的位置,例如/var/log
logging.pattern.console= 定义打印的日志格式
logging.pattern.dateformat=yyyy-MM-dd HH:mm:ss.SSS 设置日志日期格式
logging.pattern.file= 定义输出到日志文件的日志格式
logging.register-shutdown-hook=false 当初始化日志系统时,为其注册一个关闭钩子。
4、热部署
热部署 //老实讲,这个玩意有问题,好多时候网页会出错。
先上原理
深层原理是使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),另一个ClassLoader
加载会更改的类称为restart ClassLoader,这样在有代码更改的时候,原来的restart ClassLoader 被丢弃,重新创建一个
restart ClassLoader,由于需要加载的类相比较少,所以实现了较快的重启时间。
(1)修改pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>runtime</scope>
</dependency>
(2)maven插件设置参数也就是在maven打包插件加一个configurattion
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
(3)编译设置:File->Settings->Build,Execution,Deployment->Compiler:选中 Build project automatically
(4)运行设置:ctrl+shift+a 调起搜索,输入registry,点击第一项,打开Registry编辑框,选中compiler.automake
.allow.when.app.running
**网页热部署**
spring:
thymeleaf:
cache: false
三、模板引擎thymeleaf
1、常用的th标签
| 关键字 | 功能介绍 | 案例 |
|---|---|---|
| th:id | 替换id | <input th:id="'xxx' + ${collect.id}"/> |
| th:text | 文本替换 | <p th:text="${collect.description}">description</p> |
| th:utext | 支持html的文本替换 | <p th:utext="${htmlcontent}">conten</p> |
| th:object | 替换对象 | <div th:object="${session.user}"> |
| th:value | 属性赋值 | <input th:value="${user.name}" /> |
| th:with | 变量赋值运算 | <div th:with="isEven=${prodStat.count}%2==0"></div> |
| th:style | 设置样式 | th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''" |
| th:onclick | 点击事件 | th:onclick="'getCollect()'" |
| th:each | 属性赋值 | tr th:each="user,userStat:${users}"> |
| th:if | 判断条件 | <a th:if="${userId == collect.userId}" > |
| th:unless | 和th:if判断相反 | <a th:href="@{/login}" th:unless=${session.user != null}>Login</a> |
| th:href | 链接地址 | <a th:href="@{/login}" th:unless=${session.user != null}>Login</a> /> |
| th:switch | 多路选择 配合th:case 使用 | <div th:switch="${user.role}"> |
| th:case | th:switch的一个分支 | <p th:case="'admin'">User is an administrator</p> |
| th:fragment | 布局标签,定义一个代码片段,方便其它地方引用 | <div th:fragment="alert"> |
| th:include | 布局标签,替换内容到引入的文件 | <head th:include="页面名字:: 使用页面定义的名字" th:with="title='xx'"></head> /> |
| th:replace | 布局标签,替换整个标签到引入的文件 | <div th:replace="fragments/header :: title"></div> |
| th:selected | selected选择框 选中 | th:selected="(${xxx.id} == ${configObj.dd})" |
| th:src | 图片类地址引入 | <img class="img-responsive" alt="App Logo" th:src="@{/img/logo.png}" /> |
| th:inline | 定义js脚本可以使用变量 | <script type="text/javascript" th:inline="javascript"> |
| th:action | 表单提交的地址 | <form action="subscribe.html" th:action="@{/subscribe}"> |
| th:remove | 删除某个属性 | <tr th:remove="all"> |
| 1.all:删除包含标签和所有的孩子。 | ||
| 2.body:不包含标记删除,但删除其所有的孩子。 | ||
| 3.tag:包含标记的删除,但不删除它的孩子。 | ||
| 4.all-but-first:删除所有包含标签的孩子,除了第一个。 | ||
| 5.none:什么也不做。这个值是有用的动态评估。 | ||
| th:attr | 设置标签属性,多个属性可以用逗号分隔 | 比如 th:attr="src=@{/image/aa.jpg},title=#{logo}",此标签不太优雅,一般用的比较少。 |
2、thymeleaf引入操作
<!--引入动态-->
<script th:src="@{/webjars/jquery/3.5.1/dist/jquery.js}"></script>
<!--引入静态-->
<img th:src="@{/images/tianshi.png}">
<!--背景url-->
<ul th:style="'background-image: url('+@{/images/tupian.png}+');'">
<!--a标签 通过controller解析-->
<a th:href="${#httpServletRequest.getContextPath()+'/story/chongzhi'}"</a>
<a th:href="@{/story/cz(id=1,money=999.0)}">屠龙宝刀,点击就送</a>
<!--拼接图片-->
<img th:src="${#httpServletRequest.getContextPath()+'/images/'+giveGift.giftImg}">
<!--传递a=1 controller接受也必须int a-->
<!--不行就重启-->
<div class="c_b_p_desc" th:utext="${pageInfo.say}+'<a class=c_b_p_desc_readmore href='+@{note(url=${pageInfo.url})}+'>
阅读全文'">'"</div>
3、thymeleaf展示数据
<!--a+span-->
<a th:each="AnimeType : ${listAnimeType}" href="">
<span th:text="${AnimeType.animeTypeName}"></span>
</a>
<!--a+Table-->
<table>
<tr th:each="Vote : ${listVotePerson}">
<a th:text="${Vote.voteTitle}"></a>
</tr>
</table>
<!--遍历单个数据(不用th:each)--->
<span th:text="${voteNeeds.get(0).vote.voteTitle}"></span>
<!--格式化LocalDateTime)--->
${#temporals.format(pageInfo.riqi, 'yyyy-MM-dd HH:mm')}
SpringBoot简单尝试的更多相关文章
- 简单尝试Spring Cloud Gateway
简单尝试Spring Cloud Gateway 简介 Spring Cloud Gateway是一个API网关,它是用于代替Zuul而出现的.Spring Cloud Gateway构建于Sprin ...
- springboot简单介绍
1.springboot简单介绍 微服务架构 Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程. 该框架使用了特定的方 ...
- dubbo+zookeeper+springboot简单示例
目录 dubbo+zookeeper+springboot简单示例 zookeeper安装使用 api子模块 生产者producer 消费者consumer @(目录) dubbo+zookeeper ...
- Springboot简单集成ActiveMQ
Springboot简单集成ActiveMQ 消息发送者的实现 pom.xml添加依赖 <dependency> <groupId>org.springframework.bo ...
- SpringBoot简单整合redis
Jedis和Lettuce Lettuce 和 Jedis 的定位都是Redis的client,所以他们当然可以直接连接redis server. Jedis在实现上是直接连接的redis serve ...
- python+selenium 简单尝试
前言 selenium是一种自动化测试工具,简单来说浏览器会根据写好的测试脚本自动做一些操作. 关于自动化测试,一开始接触的是splinter,但是安装的时候发现它是基于selenium的,于是打算直 ...
- react-native学习笔记——简单尝试
毫无疑问,我是个不善于写博文的人. 毫无疑问,react是个出的框架. 毫无疑问,react-native更是个牛逼的引擎. 我个人对react-native的理解就是js被js引擎编译,去调用本地语 ...
- 关于MS12-020一次简单尝试
由于之前着重于web漏洞,主机漏洞这块比较薄弱.也没有用过metasploit,对于很多系统漏洞还不熟悉,正好这几天不忙,就想着慢慢学习,再写点简单的东西,进行总结记录. 这次尝试的是MS12-020 ...
- SpringBoot简单打包部署(附工程)
前言 本文主要介绍SpringBoot的一些打包事项和项目部署以及在其中遇到一些问题的解决方案. SpringBoot打包 在SpringBoot打包这块,我们就用之前的一个web项目来进行打包. 首 ...
随机推荐
- Matplotlib图例中文乱码
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签 plt.rcParams['axes.unicode_minus']=False #用来正 ...
- 翻译 - ASP.NET Core 基本知识 - 配置(Configuration)
翻译自 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-5.0 ASP ...
- 经过努力+作弊,我终于完成了leetcode通过率最低的一道题
前两天刷leetcode的时候,突发奇想,leetcode中最难的一道题是什么样子的呢? 于是,我就将所有题目(https://leetcode-cn.com/problemset/all/ )按照通 ...
- 基于scrapy框架的爬虫基本步骤
本文以爬取网站 代码的边城 为例 1.安装scrapy框架 详细教程可以查看本站文章 点击跳转 2.新建scrapy项目 生成一个爬虫文件.在指定的目录打开cmd.exe文件,输入代码 scrapy ...
- CSS 常用样式 – 背景属性
一.背景颜色 background-color 属性名:background-color 作用:在盒子区域添加背景颜色的修饰 加载区域:在 border 及以内加载背景颜色 属性值:颜色名.颜色值 & ...
- AgileConfig - 轻量级配置中心1.2.0发布,全新的UI✨✨✨
AgileConfig自发布以来有个"大问题"-UI太丑.因为当初这个项目是给自己用的,连UI界面都没有,全靠手动在数据库里改配置.后来匆匆忙忙使用bootstrap3简单的码了一 ...
- Dynamic CRM登陆界面的客制化(持续更新)
Dynamic CRM的登陆页面比较西化,不是很适合中国人使用.目前先把注销跳转的问题解决了. 服务端使用下面命令,将文件导出来 Export-AdfsWebTheme –Name default – ...
- OO第二章总结
OO第二章总结 电梯作业终于结束了!!! 这三周作业用多线程模拟搭建电梯的运行,我从开始对多线程的一无所知到结束时的能够完成一些多线程任务的水平,进步还是蛮大的,尽管过程有点艰难. 一.复杂度与UML ...
- python进阶(7)--文件与异常
一.文件读取二.文件写入三.异常四.存储数据 ---------------------------------------分割线:正文-------------------------------- ...
- Leedcode算法专题训练(树)
递归 一棵树要么是空树,要么有两个指针,每个指针指向一棵树.树是一种递归结构,很多树的问题可以使用递归来处理. 1. 树的高度 104. Maximum Depth of Binary Tree (E ...