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项目来进行打包. 首 ...
随机推荐
- 在用free()函数释放指针内存时为何要将其指针置空
在通过free()函数释放指针内存之后讲其指针置空,这样可以避免后面的程序对与该指针非法性的判断所造成的程序崩溃问题.释放空间,指针的值并没有改变,无法直接通过指针自身来进行判断空间是否已经被释放,将 ...
- 采用React + Fabric + ImageMagick 实现大图片DIY定制
一,需求背景: 某个印刷公司,有一系列的设计文件模板.接到客户订单时,就在这些设计文件模板上,做一些简单的定制,就能够满足客户的印刷需求. 如在设计文件模板上添加客户的Logo,二维码,联系方式等. ...
- PTA 输出数组元素
7-3 输出数组元素 (15 分) 本题要求编写程序,对顺序读入的n个整数,顺次计算后项减前项之差,并按每行三个元素的格式输出结果. 输入格式: 输入的第一行给出正整数n(1).随后一行给出n个整 ...
- Java例题_17 猴子偷桃问题
1 /*17 [程序 17 猴子吃桃问题] 2 题目:猴子吃桃问题 3 猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 4 第二天早上又将剩下的桃子吃掉一半,又多吃了一个. 5 以后每天 ...
- vue+django实现websocket连接
一.概述 在项目中,需要使用websocket,来展示一些实时信息. 这里使用django 3.1.5 二.django项目 安装模块 pip3 install django-cors-headers ...
- windows环境下抓密码总结
在线抓密码 1.mimikatz privilege::debug token::whoami token::elevate lsadump::sam mimikatz.exe "privi ...
- Python的flask接收前台的ajax的post数据和get数据
ajax向后台发送数据: ①post方式 ajax: @app.route("/find_worldByName",methods=['POST']) type:'post', d ...
- maven setting.xml 阿里云镜像 没有一句废话
<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...
- 【Prometheus学习笔记】主机监控 -node_exporter
Exporter for machine metrics prometheus/node_exporter 安装 Prometheus sudo tar -zxvf prometheus-*.tar. ...
- 一键生成dotnet5项目解决方案
> 作为一名从2002年.Net 1.0一路走来的老码农,也持续跟进了dotnet core 1.0~3.1的变革,并不离不弃的玩起了dotnet 5. 每次接到新项目,都要从头搭建项目解决方案 ...