一、概述
      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. OpenStack 学习笔记 (三)

    个人网站:臭蛋www.choudan.net 一直苦于不知道如何加入到开源社区参与开发,感受开源社区分布式协作开发和巨神们coding的魅力,特意在网上查了资料,直接指导的很少,还得的靠官网上的文档. ...

  2. win10_64位系统下安装ALM12.01(QC),笔者只在Win10平台上试过,大家也可以在win7的平台上试一下,一个道理!(Alpha)

    HP的ALM是一个非常全面的缺陷管理系统,但安装学习的非常的麻烦,以前学的是ALM,好久没用带今天想学习一下发现安装的非常的困难 !发现网上对ALM的系统安装的介绍非常少,也非常琐碎!今天就借着自己学 ...

  3. TCP协议 状态解析和状态统计

    一.三次握手和四次挥手 1.建立连接(三次握手)   (1)服务器会处于listen状态,客户端发送一个带SYN标志的TCP报文到服务器.   (2)服务器端回应客户端的请求,这是三次握手中的第2个报 ...

  4. 有关从经典部署模型迁移到 Azure Resource Manager 部署模型的常见问题

    此迁移计划是否影响 Azure 虚拟机上运行的任何现有服务或应用程序? 不可以. VM(经典)是公开上市的完全受支持的服务. 你可以继续使用这些资源来拓展你在 Azure 上的足迹. 如果我近期不打算 ...

  5. Java中执行.exe文件

    public static void main(String args[]){ try { String command ="notepad"; // 笔记本 Process ch ...

  6. 泛型举例:List<T>与DateTable相互转换

    一. DataTable转换到List<T> /// <summary> /// TableToList /// </summary> public class T ...

  7. Queue<T>队列与Stack<T>堆栈

    一.概述: Queue<T>队列,对象的先进先出集合("FIFO").Stack<T>栈,对象的后进先出集合("LIFO"). Queu ...

  8. unittest:2 执行多条用例,仅执行一次setUp和tearDown

    对象方法setUp()和tearDown() 每个用例执行前后都会被调用.但是有另外一种场景:setUp之后执行完所有用例,最后调用一次tearDown.比如打开网页,多条用例分别验证网页上的元素正确 ...

  9. Shell传递参数【转载】

    Shell 传递参数 我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… 实例 以 ...

  10. Win8.1下运行环境/配置问题解决方案总结

    目录 1.运行 adb shell 时报错" adb server version (26) doesn't match this client (39); killing... " ...