首頁 > Java > java教程 > 主體

springboot 配置註解的詳細介紹

不言
發布: 2019-02-01 11:36:39
轉載
4451 人瀏覽過

這篇文章帶給大家的內容是關於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熱部署

# ①新增依賴             

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
登入後複製

②Compiler 勾選左側的新鍵勾選「勾選」左側的Bumatic #utocor5

#③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.excludes/x/exclude=ic/* *,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 Spring Boot

②拷貝產生的字元到一個文本文件中,並且將該檔案命名為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
p -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        p- data-redis
spring-boot-starter-security         對spring-security的支援
spring-boot-starter-test         對常用測試依賴的支持,包括對服用的支持式,包括JUnit、c  -boot-starter-velocity         對Velocity模板引擎的支援
spring-boot-starter-activemq
spring-boot-starter-freemarker
spring-boot-starter-thypoot# -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 &

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
登入後複製

(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)来定义多个配置文件  

以上是springboot 配置註解的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:cnblogs.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!