溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

springboot 配置注解是什么意思

發(fā)布時(shí)間:2020-10-27 11:14:05 來源:億速云 閱讀:274 作者:小新 欄目:編程語言

springboot 配置注解是什么意思?這個(gè)問題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見到的。希望通過這個(gè)問題能讓你收獲頗深。下面是小編給大家?guī)淼膮⒖純?nèi)容,讓我們一起來看看吧!

一、概述

Spring Boot設(shè)計(jì)目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程。Spring Boot并不是對(duì)Spring功能上的增強(qiáng),而是提供了一種快速使用Spring的方式。

二、特性

①創(chuàng)建獨(dú)立的Spring應(yīng)用程序
②嵌入的Tomcat,無需部署WAR文件
③簡化Maven配置
④自動(dòng)配置Spring
⑤提供生產(chǎn)就緒型功能,如指標(biāo),健康檢查和外部配置
⑥開箱即用,沒有代碼生成,也無需XML配置。

三、注解說明

@SpringBootApplication        Spring Boot項(xiàng)目的核心注解,主要目的是開啟自動(dòng)配置;
@Configuration 作用于類上,相當(dāng)于一個(gè)xml配置文件,配置Spring
@Bean 作用于方法上,相當(dāng)于xml配置中的<bean>
@ComponentScan 默認(rèn)掃描@SpringBootApplication所在類的同級(jí)目錄以及它的子目錄。
@PropertySource("classpath:env.properties") 讀取外部的配置文件,通過@Value注解獲取值
@Transactional 申明事務(wù)

四、SpringBoot目錄文件結(jié)構(gòu)講解

src/main/java:存放代碼
src/main/resources
static:    存放靜態(tài)文件,比如 css、js、image, (訪問方式 http://localhost:8080/js/main.js)
templates: 存放靜態(tài)頁面jsp,html,tpl
config:   存放配置文件,application.properties

五、SpringBoot默認(rèn)加載文件的路徑

/META-INF/resources/
/resources/
/static/
/public/

SpringBoot默認(rèn)配置

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 勾選中左側(cè)的Build Project automatically

③idea設(shè)置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、指定文件不進(jìn)行熱部署 spring.devtools.restart.exclude=static/**,public/**
3、手工觸發(fā)重啟 spring.devtools.restart.trigger-file=trigger.txt
    改代碼不重啟,通過一個(gè)文本去控制

七、自定義啟動(dòng)Banner

①訪問http://patorjk.com/software/taag/#p=display&h=3&v=3&f=4Max&t=itcast%20Spring%20Boot
②拷貝生成的字符到一個(gè)文本文件中,并且將該文件命名為banner.txt
③將banner.txt拷貝到項(xiàng)目的resources目錄中

八、全局配置文件(application.properties或application.yml)

server.port=8088
server.servlet-path=*.html
server.tomcat.uri-encoding=UTF-8 
logging.level.org.springframework=DEBUG

更多點(diǎn)擊參見官網(wǎng)地址

九、Starter pom

spring-boot-starter 核心Spring Boot starter,包括自動(dòng)配置支持,日志和YAML
spring-boot-starter-amqp        對(duì)高級(jí)消息隊(duì)列協(xié)議的支持,通過spring-rabbit實(shí)現(xiàn)
spring-boot-starter-aop 對(duì)面向切面編程的支持,包括spring-aop和AspectJ
spring-boot-starter-data-elasticsearch 對(duì)Elasticsearch搜索擎的支持,包括spring-data-elasticsearch
spring-boot-starter-data-jpa        對(duì)Java持久化API的支持,包括spring-data-jpa,spring-orm和Hibernate
spring-boot-starter-jdbc        對(duì)JDBC數(shù)據(jù)庫的支持
spring-boot-starter-redis        對(duì)REDIS鍵值數(shù)據(jù)存儲(chǔ)的支持,包括spring-redis
spring-boot-starter-data-redis
spring-boot-starter-security        對(duì)spring-security的支持
spring-boot-starter-test        對(duì)常用測(cè)試依賴的支持,包括JUnit, Hamcrest和Mockito,spring-test
spring-boot-starter-velocity        對(duì)Velocity模板引擎的支持
spring-boot-starter-activemq
spring-boot-starter-freemarker
spring-boot-starter-thymeleaf
spring-boot-starter-web 對(duì)全棧web開發(fā)的支持,包括Tomcat和spring-webmvc
spring-boot-starter-webflux
(更多配置見百度)

十、常用json框架

(1)JavaBean序列化為Json,性能:
Jackson > FastJson > Gson > Json-lib
(2)jackson處理相關(guān)注解
指定字段不返回:@JsonIgnore
指定日期格式:   @JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")
空字段不返回:   @JsonInclude(Include.NON_NUll)
指定別名: @JsonProperty

十一、SpringBoot使用任務(wù)調(diào)度

(1)使用步驟:

①啟動(dòng)類里面 @EnableScheduling開啟定時(shí)任務(wù),自動(dòng)掃描
②定時(shí)任務(wù)業(yè)務(wù)類 加注解 @Component被容器掃描
③定時(shí)執(zhí)行的方法加上注解 @Scheduled(fixedRate=2000) 定期執(zhí)行一次

(2)常用定時(shí)任務(wù)表達(dá)式配置和在線生成器

cron 定時(shí)任務(wù)表達(dá)式 @Scheduled(cron="*/1 * * * * *") 表示每秒

crontab 工具  https://tool.lu/crontab/

ixedRate: 定時(shí)多久執(zhí)行一次(上一次開始執(zhí)行時(shí)間點(diǎn)后xx秒再次執(zhí)行;)
fixedDelay: 上一次執(zhí)行結(jié)束時(shí)間點(diǎn)后xx秒再次執(zhí)行
fixedDelayString:  字符串形式,可以通過配置文件指定

(3)異步定時(shí)任務(wù)

啟動(dòng)類里面使用@EnableAsync注解開啟功能,自動(dòng)掃描
定義異步任務(wù)類并使用@Component標(biāo)記組件被容器掃描,異步方法加上@Async
①要把異步任務(wù)封裝到類里面,不能直接寫到Controller
②增加Future<String> 返回結(jié)果 AsyncResult<String>("task執(zhí)行完成");  
③如果需要拿到結(jié)果 需要判斷全部的 task.isDone()

十二、SpringBoot攔截器、過濾器、監(jiān)聽器

(1)SpringBoot啟動(dòng)默認(rèn)加載的Filter

characterEncodingFilter
hiddenHttpMethodFilter
httpPutFormContentFilter
requestContextFilter

(2)Filter優(yōu)先級(jí)

Ordered.HIGHEST_PRECEDENCE
Ordered.LOWEST_PRECEDENCE

(3)自定義Filter

1)使用Servlet3.0的注解進(jìn)行配置
2)啟動(dòng)類里面增加 @ServletComponentScan,進(jìn)行掃描
3)新建一個(gè)Filter類,implements Filter,并實(shí)現(xiàn)對(duì)應(yīng)的接口
4) @WebFilter 標(biāo)記一個(gè)類為filter,被spring進(jìn)行掃描
urlPatterns:攔截規(guī)則,支持正則
5)控制chain.doFilter的方法的調(diào)用,來實(shí)現(xiàn)是否通過放行
  不放行,web應(yīng)用resp.sendRedirect("/index.html");
場景:權(quán)限控制、用戶登錄(非前端后端分離場景)等

(4)Servlet3.0的注解自定義原生Listener監(jiān)聽器

自定義Listener(常用的監(jiān)聽器 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:調(diào)用Controller某個(gè)方法之前
postHandle:Controller之后調(diào)用,視圖渲染之前,如果控制器Controller出現(xiàn)了異常,則不會(huì)執(zhí)行此方法
afterCompletion:不管有沒有異常,這個(gè)afterCompletion都會(huì)被調(diào)用,用于資源清理
3)按照注冊(cè)順序進(jìn)行攔截,先注冊(cè),先被攔截

(6)對(duì)比

Filter是基于函數(shù)回調(diào) doFilter(),而Interceptor則是基于AOP思想
Filter在只在Servlet前后起作用,而Interceptor夠深入到方法前后、異常拋出前后等
Filter依賴于Servlet容器即web應(yīng)用中,而Interceptor不依賴于Servlet容器所以可以運(yùn)行在多種環(huán)境。
在接口調(diào)用的生命周期里,Interceptor可以被多次調(diào)用,而Filter只能在容器初始化時(shí)調(diào)用一次。
Filter和Interceptor的執(zhí)行順序:過濾前->攔截前->action執(zhí)行->攔截后->過濾后

十三、兩種部署方式j(luò)ar和war

(1)jar包方式啟動(dòng)

添加maven插件,執(zhí)行打包即可,啟動(dòng)命令:    java -jar **.jar &

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

(2)war包方式啟動(dòng)

a.在pom.xml中將打包形式 jar 修改為war  <packaging>war</packaging>
b.添加構(gòu)建項(xiàng)目名稱 <finalName>xdclass_springboot</finalName>
c.修改啟動(dòng)類

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.打包項(xiàng)目,啟動(dòng)tomcat

十四、SpringBoot多環(huán)境配置

①不同環(huán)境使用不同配置
例如數(shù)據(jù)庫配置,在開發(fā)的時(shí)候,我們一般用開發(fā)數(shù)據(jù)庫,而在生產(chǎn)環(huán)境的時(shí)候,我們是用正式的數(shù)據(jù)
②配置文件存放路徑
classpath根目錄的“/config”包下
classpath的根目錄下
③spring boot允許通過命名約定按照一定的格式(application-{profile}.properties)來定義多個(gè)配置文件

感謝各位的閱讀!看完上述內(nèi)容,你們對(duì)springboot 配置注解是什么意思大概了解了嗎?希望文章內(nèi)容對(duì)大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI