Spring Boot 執行原理
3 核心註解
開啟任意*AutoConfiguration檔案,一般都有下面的條件註解,在spring-boot-autoconfigure-1.5.3.RELEASE.jar的org.springframework.boot.autoconfigure.condition包下條件註解如下:
- @ConditionalOnBean:當前容器有指定Bean的條件下。
- @ConditionalOnClass:當前類路徑下有指定的類的條件下。
- @ConditionalOnExpression:基於SpEL表示式作為判斷條件。
- @ConditionalOnJava:基於JVM版本作為判斷條件。
- @ConditionalOnJndi:在JNDI存在的條件下查詢指定的位置。
- @ConditionalOnMissingBean:當容器裡沒有指定Bean的情況下。
- @ConditionalOnMissingClass:當類路徑下沒有指定的類的條件下。
- @ConditionalOnNotWebApplication:當前專案不是WEB專案的條件下。
- @ConditionalOnProperty:指定屬性是否有指定的值。
- @ConditionalOnResource:類路徑是否有指定的值。
- @ConditionalOnSingleCandidate:當指定Bean在容器中只有一個,或者雖然有多個但 是指定首選的Bean。
- @ConditionalOnWebApplication:當前專案是WEB專案的條件下。
這些註解都組合了@Conditional元註解,只是使用了不同的條件(Conditional),Spring 條件註解(@Conditional)我們介紹過根據不同條件建立不同Bean。
簡單分析一下@ConditionalOnWebApplication註解:
package org.springframework.boot.autoconfigure.condition;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation .Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Conditional;
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnWebApplicationCondition.class)
public @interface ConditionalOnWebApplication {
}
從原始碼我們可以看出,此註解使用的條件是OnWebApplicationCondition類,下面我們看看這個類是怎麼構造的:
package org.springframework.boot.autoconfigure.condition;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.StandardServletEnvironment;
@Order(Ordered.HIGHEST_PRECEDENCE + 20)
class OnWebApplicationCondition extends SpringBootCondition {
private static final String WEB_CONTEXT_CLASS = "org.springframework.web.context."
+ "support.GenericWebApplicationContext";
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
boolean webApplicationRequired = metadata
.isAnnotated(ConditionalOnWebApplication.class.getName());
ConditionOutcome webApplication = isWebApplication(context, metadata);
if (webApplicationRequired && !webApplication.isMatch()) {
return ConditionOutcome.noMatch(webApplication.getMessage());
}
if (!webApplicationRequired && webApplication.isMatch()) {
return ConditionOutcome.noMatch(webApplication.getMessage());
}
return ConditionOutcome.match(webApplication.getMessage());
}
private ConditionOutcome isWebApplication(ConditionContext context,
AnnotatedTypeMetadata metadata) {
if (!ClassUtils.isPresent(WEB_CONTEXT_CLASS, context.getClassLoader())) {
return ConditionOutcome.noMatch("web application classes not found");
}
if (context.getBeanFactory() != null) {
String[] scopes = context.getBeanFactory().getRegisteredScopeNames();
if (ObjectUtils.containsElement(scopes, "session")) {
return ConditionOutcome.match("found web application 'session' scope");
}
}
if (context.getEnvironment() instanceof StandardServletEnvironment) {
return ConditionOutcome
.match("found web application StandardServletEnvironment");
}
if (context.getResourceLoader() instanceof WebApplicationContext) {
return ConditionOutcome.match("found web application WebApplicationContext");
}
return ConditionOutcome.noMatch("not a web application");
}
}
從isWebApplication方法可以看出,判斷條件是:
- GenericWebApplicationContext是否在類路徑中;
- 容器中是否有名為session的scope;
- 當前容器的Enviroment是否為StandardServletEnvironment;
- 當前的ResourceLoader是否是WebApplicationContext(ResourceLoader是ApplicationContext的頂級介面之一);
- 我們需要構建ConditionOutcome類的物件來幫助我們,最終通過ConditionOutcome.isMatch方法返回值來確定條件。
相關推薦
SpringBoot15-springboot核心-Spring Boot執行原理
這篇來先通過學習分析Spring Boot的執行原理後,根據已掌握的知識來自定義一個start pom。 Spring Boot關於自動配置的原始碼在spring-boot-autoconfigure-xxx.jar內。若想檢視有哪些自動配置,可以檢
Spring Boot 執行原理
3 核心註解 開啟任意*AutoConfiguration檔案,一般都有下面的條件註解,在spring-boot-autoconfigure-1.5.3.RELEASE.jar的org.springframework.boot.autoconfigure.conditi
Spring Boot深入原理 - SpringApplication啟動原理
什麽 nal state int spring img erl prepare try Spring Boot深入原理 - SpringApplication啟動原理 我們知道,如果不需要特殊的配置,只需要在main方法裏調用SpringApplicatio.run()
spring boot 啟動原理剖析
urn rgs 獲取 target loader 技術分享 提前 pub 流程 準備 SpringBoot為我們做的自動配置,確實方便快捷,若不大明白SpringBoot內部啟動原理,以後難免會吃虧,所以這次博主就跟你們一起一步步揭開SpringBoot的神秘面紗,讓它不再
Spring Boot從原理到實戰
.config 文件配置 為我 mls class def failed sch rgs “回眸一笑百媚生”,說的就是Spring Boot。開始只是聽說了這個名詞,那它和spring有啥區別啊,名字有點類似哦。所以說:Spring Boot是spring的升級版,但並不是
spring boot啟動原理(包含自動配置)解析
轉自:https://www.cnblogs.com/xiaoxi/p/7999885.html 我們開發任何一個Spring Boot專案,都會用到如下的啟動類 1 @SpringBootApplication 2 public class Application { 3 publ
Spring boot ConditionalOnClass原理解析
Spring boot如何自動載入 對於Springboot的ConditionalOnClass註解一直非常好奇,原因是我們的jar包裡面可能沒有對應的class,而使用ConditionalOnClass標註的Configuration類又import了這個類,那麼如果想載入Configuration類
Spring Boot 啟動原理解析
前言 前面幾章我們見識了SpringBoot為我們做的自動配置,確實方便快捷,但是對於新手來說,如果不大懂SpringBoot內部啟動原理,以後難免會吃虧。所以這次博主就跟你們一起一步步揭開SpringBoot的神祕面紗,讓它不在神祕。 正文 我們開發任何一個Spring Boot專
Linux系統CentOS 7配置Spring Boot執行環境
從阿里雲新買的一臺Linux伺服器,用來部署SpringBoot應用,由於之前一直使用Debian版本,環境配置有所不同,也較為繁瑣,本文主要介紹CentOS下配置SpringBoot環境的過程 新建使用者 由於新拿到的機器只有root使用者,用
spring boot 執行緒任務
//任務模組 @Service public class TaskManager extends SimpleEventDispatcher<BaseTask> { private static final Logger log = LoggerFacto
spring-boot啟動原理深入(轉)
每個SpringBoot程式都有一個主入口,也就是main方法,main裡面呼叫SpringApplication.run()啟動整個spring-boot程式,該方法所在類主要需要使用@SpringBootApplication註解,@SpringBootA
spring架構---spring-mvc執行原理解讀
上來一張圖,沒圖沒真相: 1、 使用者傳送請求至前端控制器DispatcherServlet。 2、 DispatcherServlet收到請求呼叫HandlerMapping處理器對映器。 3、 處理器對映器找到具體的處理器(可以根據xml配置
spring boot 執行定時任務
在spring boot專案的啟動類中新增@EnableScheduling註解,表示開啟定時任務,如 @SpringBootApplication @EnableScheduling public
Spring Boot核心原理-自動配置
作者簡介:朱清,畢業於電子科技大學,現任職冰鑑科技高階研發經理,主導冰鑑風控系統架構設計和研發。 之前在公司內部推行spring boot時,有同事跟我提到過,感覺換到spring boot這個框架後,好處是小白也能迅速上手寫業務程式碼了。但是呢,這種情況下新手很容易寫得雲裡霧裡的,因為完全不知道背後的
Spring Boot實戰:Spring Boot核心原理剖析
Spring Boot基本配置介紹 1 關閉某個自動配置 通過上節@SpringBootApplication下的@EnableAutoConfiguration得知,Spring Boot會根據專案中的jar包依賴,自動做出配置,Spring Boot支
spring boot 執行 jar 或 war 提示沒有主清單屬性解決
在將專案開發,打包 jar 或者 war 的時候, 我們可以通過 java -jar xxxx.jar/war 的命令直接執行,這個確實很方便 今天lz 就遇到了打包 jar 或者 war 通過 java -jar xxxx.jar/war 執行確提示沒有主清單屬性 在
Spring實現原理分析(二十四).Spring Boot實現原理分析
前陣子在分析sprng boot的原始碼,有了些感悟和心得,今天寫篇部落格和大家分享下。先來段題外話,在人體的血液中含有血細胞,而血細胞又大致可以分為紅細胞、白細胞、血小板。它們各自有各自的用處和特點,互相協作保障人體的建康。 一. 各種Bean 如
Spring Boot【原理分析】(3)——BeanDefinition
一、簡介 BeanDefinition描述了一個Bean的例項,包括屬性,構造方法引數,註解等更多資訊。為後面例項化Bean提供元資料依據。 BeanDefinition的實現類有: 1. RootBeanDefinition:spring BeanFac
Spring Boot執行緒池配置使用
首先,我們要了解什麼是執行緒池,執行緒,大家應該都不陌生,我們可以把一個介面處理的單個任務當做一個執行緒,比如上傳檔案,呼叫介面,上傳檔案,上傳完成。 這時會初始化一個執行緒,等待上傳結束,執行緒銷燬 。 但是這樣會頻繁的建立銷燬執行緒,為了不浪費更多的系統資源,我們使用執行
spring boot-執行Async任務時,使用自定義的執行緒池
一、增加配置屬性類 package com.chhliu.springboot.async.configuration; import org.springframework.boot.context.properties.ConfigurationPropertie