一、概述
      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. SwipeRefreshLayout 报错 dispatchTouchEvent

    今天开发android中使用了 android-suport-v4 19.1 记录 SwipeRefreshLayout 的坑: http://stackoverflow.com/questions/ ...

  2. zabbix启动报错:Connection to database 'xxx' failed解决方法

    Zabbix 分布式系统监视系统 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通 ...

  3. win10系统80端口被System (PID=4)占用的解决

    今天想用wamp搭建虚拟目录.发现80端口被占用,操作挺麻烦的,所以想要更改. 具体流程如下: 1.“win+R”输入“cmd”,然后输入“netstat -ano | findstr "8 ...

  4. Oracle11g服务详细介绍及哪些服务是必须开启的

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/DaisyLoveZly/article/details/79463713 七个服务的含义分别为:1. ...

  5. Asp.Net MVC 开发技巧(一)

    开发程序时的流程: 1.设计数据模型. 数据模型最为重要,不仅关系到数据的存储,同时程序的可扩展性,效率也受影响,甚至决定开发工作量.所以要极其认真的设计数据库的表和相关字段. 建完基本的数据模型后, ...

  6. 认识 Java(配置环境变量)

    1. Java 简介 Java由Sun Microsystems公司于1995年5月推出,是一种面向对象的编程语言.在2009年4月20号,ORACLE (甲骨文)收购了 Sun 公司,也就是说 Ja ...

  7. 10、Node.js模块系统

    ##################################################################################介绍Node.js模块系统为了让No ...

  8. [转]solr系统query检索词特殊字符的处理

    原文地址:http://blog.csdn.net/wgw335363240/article/details/39889979 solr是基于 lucence开发的应用,如果query中带有非法字符串 ...

  9. CCControlExtension/CCControl

    #ifndef __CCCONTROL_H__ #define __CCCONTROL_H__ #include "CCInvocation.h" #include "C ...

  10. Java工具类(util) 之01- 数学运算工具(精确运算)

    数学运算工具(精确运算) /** * * @author maple * */ public abstract class AmountUtil { private AmountUtil() { } ...