1. 程式人生 > 程式設計 >Spring @Enable模組驅動原理及使用例項

Spring @Enable模組驅動原理及使用例項

Spring @Enable 模組概覽

框架實現 @Enable註解模組 啟用模組
Spring Framework @EnableWebMvc Web MVC 模組
@EnableTransactionManagement 事務管理模組
@EnableCaching Caching 模組
@EnableMBeanExport JMX 模組
@EnableAsync 非同步處理模組
@EnableWebFlux Web Flux 模組
@EnableAspectJAutoProxy AspectJ 代理模組
Spring Boot @EnableAutoConfiguration 自動裝配
@EnableManagementContext Actuator 管理模組
@EnableConfigurationProperties 配置屬性繫結模組
@EnableOAuth2Sso OAuth2單點登入模組
Spring Cloud @EnableEurekaServer Eureka 伺服器模組
@EnableConfigServer 配置伺服器模組
@EnableFeignClients Feign客戶端模組
@EnableZuulProxy 服務閘道器Zuul模組
@EnableCircuitBreaker 服務熔斷模組

理解 @Enable 以 @EnableWebMVC 為例進行理解

定義如下:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}

發現該註解中引入的 DelegatingWebMvcConfiguration.class

@Configuration(proxyBeanMethods = false)
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
...
}

public class WebMvcConfigurationSupport implements ApplicationContextAware,ServletContextAware {
@Bean
@SuppressWarnings("deprecation")
public RequestMappingHandlerMapping requestMappingHandlerMapping(
   @Qualifier("mvcContentNegotiationManager") ContentNegotiationManager contentNegotiationManager,@Qualifier("mvcConversionService") FormattingConversionService conversionService,@Qualifier("mvcResourceUrlProvider") ResourceUrlProvider resourceUrlProvider) {
   ...
}
...
}

其中 實現類 WebMvcConfigurationSupport.java 中 預定義了 多個 Spring Bean 物件,

隨著 @EnableWebMVC 驅動註解的載入而被載入到 Spring 上下文中從而實現 Spring Web MVC的功能。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。