跳转到主内容
websoft网络软件专家 - 深耕网络技术,打造实用软件!

springboot 配置注解的详细介绍

本篇文章给大家带来的内容是关于 springboot 配置注解的详细介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。 一、概述 Spring Boot设计目的是用来简化新Spring应用的初始搭建以及开发过程。Spring Boot并不是对Spring功能上的增强,而是提供了一种快速使用Spring的方式。 二、特性 ①创建独立的Spring应用程序 ②嵌入的Tomcat,无需部署WAR文件 ③简化Maven配置 ④自动配置Spring ⑤提供生产就绪型功能,如指标,健康检查和外部配置 ⑥开箱即用,没有代码生成,也无需XML配置。 三、注解说明 @SpringBootApplication         Spring Boot项目的核心注解,主要目的是开启自动配置; @Configuration 作用于类上,相当于一个xml配置文件,配置Spring @Bean 作用于方法上,相当于xml配置中的 @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热部署 ①添加依赖
org.springframework.boot spring-boot-devtools
②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 * * * * *") 表示每秒 crontab 工具  https://tool.lu/crontab/ ixedRate: 定时多久执行一次(上一次开始执行时间点后xx秒再次执行;) fixedDelay: 上一次执行结束时间点后xx秒再次执行 fixedDelayString:  字符串形式,可以通过配置文件指定 (3)异步定时任务 启动类里面使用@EnableAsync注解开启功能,自动扫描 定义异步任务类并使用@Component标记组件被容器扫描,异步方法加上@Async ①要把异步任务封装到类里面,不能直接写到Controller ②增加Future 返回结果 AsyncResult("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:拦截规则,支持正则 5)控制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 &
org.springframework.boot spring-boot-maven-plugin
(2)war包方式启动 a.在pom.xml中将打包形式 jar 修改为war  war b.添加构建项目名称 xdclass_springboot 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)来定义多个配置文件

相关文章