一、概述
      Spring Boot设计目的是用来简化新Spring应用的初始搭建以及开发过程。Spring Boot并不是对Spring功能上的增强,而是提供了一种快速使用Spring的方式。

二、特性
①创建独立的Spring应用程序
②嵌入的Tomcat,无需部署WAR文件
③简化Maven配置
④自动配置Spring
⑤提供生产就绪型功能,如指标,健康检查和外部配置
⑥开箱即用,没有代码生成,也无需XML配置。
三、注解说明
@SpringBootApplication         Spring Boot项目的核心注解,主要目的是开启自动配置;
@Configuration 作用于类上,相当于一个xml配置文件,配置Spring
@Bean 作用于方法上,相当于xml配置中的<bean>
@ComponentScan 默认扫描@SpringBootApplication所在类的同级目录以及它的子目录。
@PropertySource("classpath:env.properties") 读取外部的配置文件,通过@Value注解获取值
@Transactional 申明事务
四、SpringBoot目录文件结构讲解
src/main/java:存放代码
src/main/resources
static:    存放静态文件,比如 css、js、image, (访问方式 http://localhost:8080/js/main.js)
templates: 存放静态页面jsp,html,tpl
config:   存放配置文件,application.properties
五、SpringBoot默认加载文件的路径
/META-INF/resources/
/resources/
/static/
/public/
       SpringBoot默认配置
spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
六、Spring Boot热部署
①添加依赖            
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
②Compiler 勾选中左侧的Build Project automatically
③idea设置Auto-Compile,然后 Shift+Ctrl+Alt+/,选择Registry
勾选compiler.automake.allow.when.app.running
④不被热部署的文件
  1、/META-INF/maven, /META-INF/resources, /resources, /static, /public, or /templates
  2、指定文件不进行热部署 spring.devtools.restart.exclude=static/**,public/**
  3、手工触发重启 spring.devtools.restart.trigger-file=trigger.txt
  改代码不重启,通过一个文本去控制
七、自定义启动Banner
①访问http://patorjk.com/software/taag/#p=display&h=3&v=3&f=4Max&t=itcast%20Spring%20Boot
②拷贝生成的字符到一个文本文件中,并且将该文件命名为banner.txt
③将banner.txt拷贝到项目的resources目录中
八、全局配置文件(application.properties或application.yml)
server.port=8088
server.servlet-path=*.html
server.tomcat.uri-encoding=UTF-8 
logging.level.org.springframework=DEBUG
更多点击参见官网地址

九、Starter pom

spring-boot-starter 核心Spring Boot starter,包括自动配置支持,日志和YAML
spring-boot-starter-amqp         对高级消息队列协议的支持,通过spring-rabbit实现
spring-boot-starter-aop 对面向切面编程的支持,包括spring-aop和AspectJ
spring-boot-starter-data-elasticsearch 对Elasticsearch搜索擎的支持,包括spring-data-elasticsearch
spring-boot-starter-data-jpa         对Java持久化API的支持,包括spring-data-jpa,spring-orm和Hibernate
spring-boot-starter-jdbc         对JDBC数据库的支持
spring-boot-starter-redis         对REDIS键值数据存储的支持,包括spring-redis
spring-boot-starter-data-redis
spring-boot-starter-security         对spring-security的支持
spring-boot-starter-test         对常用测试依赖的支持,包括JUnit, Hamcrest和Mockito,spring-test
spring-boot-starter-velocity         对Velocity模板引擎的支持
spring-boot-starter-activemq
spring-boot-starter-freemarker
spring-boot-starter-thymeleaf
spring-boot-starter-web 对全栈web开发的支持,包括Tomcat和spring-webmvc
spring-boot-starter-webflux
(更多配置见百度)
十、常用json框架
(1)JavaBean序列化为Json,性能:
Jackson > FastJson > Gson > Json-lib 
(2)jackson处理相关注解
指定字段不返回:@JsonIgnore
指定日期格式:   @JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")
空字段不返回:   @JsonInclude(Include.NON_NUll)
指定别名: @JsonProperty
十一、SpringBoot使用任务调度
(1)使用步骤:
①启动类里面 @EnableScheduling开启定时任务,自动扫描
②定时任务业务类 加注解 @Component被容器扫描
③定时执行的方法加上注解 @Scheduled(fixedRate=2000) 定期执行一次
(2)常用定时任务表达式配置和在线生成器
cron 定时任务表达式 @Scheduled(cron="*/1 * * * * *") 表示每秒
1)crontab 工具  https://tool.lu/crontab/
fixedRate: 定时多久执行一次(上一次开始执行时间点后xx秒再次执行;)
fixedDelay: 上一次执行结束时间点后xx秒再次执行
fixedDelayString:  字符串形式,可以通过配置文件指定
(3)异步定时任务
启动类里面使用@EnableAsync注解开启功能,自动扫描
定义异步任务类并使用@Component标记组件被容器扫描,异步方法加上@Async
①要把异步任务封装到类里面,不能直接写到Controller
②增加Future<String> 返回结果 AsyncResult<String>("task执行完成");  
③如果需要拿到结果 需要判断全部的 task.isDone()
十二、SpringBoot拦截器、过滤器、监听器
(1)SpringBoot启动默认加载的Filter 
characterEncodingFilter
hiddenHttpMethodFilter
httpPutFormContentFilter
requestContextFilter
(2)Filter优先级
Ordered.HIGHEST_PRECEDENCE
Ordered.LOWEST_PRECEDENCE
(3)自定义Filter
1)使用Servlet3.0的注解进行配置
2)启动类里面增加 @ServletComponentScan,进行扫描
3)新建一个Filter类,implements Filter,并实现对应的接口
4) @WebFilter 标记一个类为filter,被spring进行扫描 
urlPatterns:拦截规则,支持正则
6)控制chain.doFilter的方法的调用,来实现是否通过放行
  不放行,web应用resp.sendRedirect("/index.html");
场景:权限控制、用户登录(非前端后端分离场景)等
(4)Servlet3.0的注解自定义原生Listener监听器
自定义Listener(常用的监听器 servletContextListener、httpSessionListener、servletRequestListener)
@WebListener
public class RequestListener implements ServletRequestListener {

@Override
public void requestDestroyed(ServletRequestEvent sre) {
// TODO Auto-generated method stub
System.out.println("======requestDestroyed========");
}

@Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("======requestInitialized========");

}
(5)自定义拦截器
1)implements WebMvcConfigurer
@Configuration
public class CustomWebMvcConfigurer implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginIntercepter()).addPathPatterns("/api2/*/**");
//.excludePathPatterns("/api2/xxx/**");
WebMvcConfigurer.super.addInterceptors(registry);
}
}
2)自定义拦截器 HandlerInterceptor
preHandle:调用Controller某个方法之前
postHandle:Controller之后调用,视图渲染之前,如果控制器Controller出现了异常,则不会执行此方法
afterCompletion:不管有没有异常,这个afterCompletion都会被调用,用于资源清理
3)按照注册顺序进行拦截,先注册,先被拦截
(6)对比
Filter是基于函数回调 doFilter(),而Interceptor则是基于AOP思想
Filter在只在Servlet前后起作用,而Interceptor够深入到方法前后、异常抛出前后等
Filter依赖于Servlet容器即web应用中,而Interceptor不依赖于Servlet容器所以可以运行在多种环境。
在接口调用的生命周期里,Interceptor可以被多次调用,而Filter只能在容器初始化时调用一次。
Filter和Interceptor的执行顺序:过滤前->拦截前->action执行->拦截后->过滤后
十三、两种部署方式jar和war
(1)jar包方式启动
添加maven插件,执行打包即可,启动命令:    java -jar **.jar &
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
(2)war包方式启动
a.在pom.xml中将打包形式 jar 修改为war  <packaging>war</packaging>
b.添加构建项目名称 <finalName>xdclass_springboot</finalName>
c.修改启动类
public class XdclassApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(XdclassApplication.class);
}

public static void main(String[] args) throws Exception {
SpringApplication.run(XdclassApplication.class, args);
}
}
d.打包项目,启动tomcat
十四、SpringBoot多环境配置
①不同环境使用不同配置
例如数据库配置,在开发的时候,我们一般用开发数据库,而在生产环境的时候,我们是用正式的数据
②配置文件存放路径
classpath根目录的“/config”包下
classpath的根目录下
③spring boot允许通过命名约定按照一定的格式(application-{profile}.properties)来定义多个配置文件  
---------------------
作者:带你去学习
来源:CSDN
原文:https://blog.csdn.net/zhou870498/article/details/80685774
版权声明:本文为博主原创文章,转载请附上博文链接!

最新学习springboot 配置注解的更多相关文章

  1. 学习SpringBoot零碎记录——配置应用URL名称

    学习SpringBoot配置应用名称,结果发现坑 到网上找 到 https://blog.csdn.net/qq_40087415/article/details/82497668 server: p ...

  2. 学习笔记_J2EE_SpringMVC_03_注解配置_@RequestMapping用法

    @RequestMappingde的用法 摘要: 主要介绍注解@RequestMapping的用法 一.@RequestMapping 简介 在Spring MVC 中使用 @RequestMappi ...

  3. 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比

    [原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...

  4. 从零开始学习springboot之热部署的配置

    各位看官大家好,博主之前因为毕业设计以及毕业旅游耽搁了好长一段时间没有更新博客了,从今天起又会慢慢开始学习啦. 今天主要是来学习springboot热部署的配置. 一. 热部署 我们通常在修改某些文件 ...

  5. SpringBoot 入门篇(二) SpringBoot常用注解以及自动配置

    一.SpringBoot常用注解二.SpringBoot自动配置机制SpringBoot版本:1.5.13.RELEASE 对应官方文档链接:https://docs.spring.io/spring ...

  6. 01.springboot入门--启用自动配置注解EnableAutoConfiguration

    springboot入门 <parent> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  7. spring boot 学习(十)SpringBoot配置发送Email

    SpringBoot配置发送Email 引入依赖 在 pom.xml 文件中引入邮件配置: <dependency> <groupId>org.springframework. ...

  8. springboot配置详解

    springboot配置详解 Author:SimpleWu properteis文件属性参考大全 springboot默认加载配置 SpringBoot使用两种全局的配置文件,全局配置文件可以对一些 ...

  9. SpringBoot核心注解应用

    1.今日大纲 了解Spring的发展 掌握Spring的java配置方式 学习Spring Boot 使用Spring Boot来改造购物车系统 2.Spring的发展 Spring1.x 时代 在S ...

随机推荐

  1. mac ASP.NET5

    不写1行代码,在Mac上体验ASP.NET 5的最简单方法   昨天微软发布了ASP.NET 5 beta2(详见ASP.NET 5 Beta2 发布),对ASP.NET 5的好奇心又被激发了. 今天 ...

  2. springAOP学习笔记

    目录 基础 引用 AOP方法 使用 xml配置 注解配置 基础 什么是aop? 把我们程序重复的代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改源码的 基础上,对我们的已有方法进行增强. ...

  3. 使用dsoframer控件出现"Unable to display the inactive document. Click here to reactivate the document."的问题 .

    使用如下属性设置: axFramerControl.ActivationPolicy = DSOFramer.dsoActivationPolicy.dsoKeepUIActiveOnAppDeact ...

  4. python笔记8-多线程threading之封装式

    执行函数 1.先写一个执行函数,用来实现做某件事情,不同的人吃火锅用一个参数people代替. # coding=utf-8 import threading import time def chiH ...

  5. Mac OS 10.8 配置SVN服务器

    Mac 10.8开始,不再默认安装svn,需要自行安装 如果您安装了XCode,会随同安装svn 更省事的办法: 1.安装homebrew,看看官网 http://brew.sh/index_zh-c ...

  6. 为Linux设置IPTables防火墙

    我们 来讨论一下如何为你的CentOS 服务器来设置简单的防火墙. 这里我们以DigitalOcean的CentOS 6 VPS为基础来讨论的,同样也适用于 阿里云上其他类型的LINUX系统. (阿里 ...

  7. [BZOJ 4555][Tjoi2016&Heoi2016]求和

    题意 给定 $n$ , 求下式的值: $$ f(n)= \sum_{i=0}^n\sum_{j=0}^i\begin{Bmatrix}i\\ j\end{Bmatrix}\times 2^j\time ...

  8. ubuntu 14.04 配置 java 环境

    下载java包 (这里以java8为例) java包的下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloa ...

  9. Angular Reactive Form - 填充表单模型

    setValue 使用setValue,可以通过传递其属性与FormGroup后面的表单模型完全匹配的数据对象来一次分配每个表单控件值. 在分配任何表单控件值之前,setValue方法会彻底检查数据对 ...

  10. 关于Struts2通配符无效的说明

    在struts2.3之前的版本,正常的配置就可以了,但在struts2.3版本之后,使用通配符调用方法时,内部会验证是否允许访问该方法. 1.struts2.5 为了增加安全性,在 struts.xm ...