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项目来进行打包. 首 ...
随机推荐
- HDU_3949 XOR 【线性基】
一.题目 XOR 二.分析 给定$N$个数,问它的任意子集异或产生的数进行排列,求第K小的数. 构造出线性基$B$后,如果$|B| < N$,那么代表N个数中有一个数是可以由线性基中的其他数异或 ...
- python常用数据处理库
Python之所以能够成为数据分析与挖掘领域的最佳语言,是有其独特的优势的.因为他有很多这个领域相关的库可以用,而且很好用,比如Numpy.SciPy.Matploglib.Pandas.Scikit ...
- 图解双链表(Java实现)
原创公众号:bigsai 文章已收录在 全网都在关注的数据结构与算法学习仓库 前言 前面有很详细的讲过线性表(顺序表和链表),当时讲的链表以但链表为主,但实际上在实际应用中双链表的应用多一些就比如Li ...
- Java中的集合Set - 入门篇
前言 大家好啊,我是汤圆,今天给大家带来的是<Java中的集合Set - 入门篇>,希望对大家有帮助,谢谢 简介 前面介绍了集合List,映射Map,最后再简单介绍下集合Set,相关类如下 ...
- vue 快速入门 系列 —— 侦测数据的变化 - [基本实现]
其他章节请看: vue 快速入门 系列 侦测数据的变化 - [基本实现] 在 初步认识 vue 这篇文章的 hello-world 示例中,我们通过修改数据(app.seen = false),页面中 ...
- WordPress的SEO优化技巧
世界上大约有30%的网站都是由Wordpress搭建的,因为Wordpress自身构架清晰,代码规范,且网页评论直接书写在整个页面里,能够被搜索引擎检索到,因此对搜索引擎很友好.但有时候还是会出现只被 ...
- 创建第一个HTML文件
首先右键新建文本文档,然后打开新建的文档,文本内容写上: <html> <head> <title>我的HTML标题</title> </head ...
- OO第二章总结
OO第二章总结 电梯作业终于结束了!!! 这三周作业用多线程模拟搭建电梯的运行,我从开始对多线程的一无所知到结束时的能够完成一些多线程任务的水平,进步还是蛮大的,尽管过程有点艰难. 一.复杂度与UML ...
- 2020 OO 第四单元总结 UML
title: 2020 OO 第四单元总结 date: 2020-06-14 19:10:06 tags: OO categories: 学习 1. 本单元三次作业的架构设计 本单元的代码编写与第三单 ...
- 构建一个Flowable命令行应用
官网链接 [(https://flowable.com/open-source/docs/bpmn/ch02-GettingStarted/#building-a-command-line-appli ...