日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

【Spring】Spring高级话题-@Enable***注解的工作原理

發布時間:2023/12/18 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Spring】Spring高级话题-@Enable***注解的工作原理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

@EnableAspectJAutoProxy

?

前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。

?

@EnableAspectJAutoProxy注解 激活Aspect自動代理

<aop:aspectj-autoproxy/>
  • 1

開啟對AspectJ自動代理的支持。

在用到AOP的自動代理的時候用,如果你理解了Java的動態代理,很容易的就會熟悉AOP的自動代理的。

@EnableAsync

@EnableAsync注解開啟異步方法的支持。?
這個相信大家都比較熟悉的。對于異步應該都理解的。?
不太熟悉的,可以看這篇博客:-有示例?
【Spring】Spring高級話題-多線程-TaskExecutor

@EnableScheduling

@EnableScheduling注解開啟計劃任務的支持。

也就是字面上的意思,開啟計劃任務的支持!?
一般都需要@Scheduled注解的配合。

詳情見此博客:?
【Spring】Spring高級話題-計劃任務-@EnableScheduling

@EnableWebMVC

@EnableWebMVC注解用來開啟Web MVC的配置支持。

也就是寫Spring MVC時的時候會用到。

@EnableConfigurationProperties

@EnableConfigurationProperties注解是用來開啟對@ConfigurationProperties注解配置Bean的支持。

也就是@EnableConfigurationProperties注解告訴Spring Boot 使能支持@ConfigurationProperties

@EnableJpaRepositories

@EnableJpaRepositories注解開啟對Spring Data JPA Repostory的支持。

Spring Data JPA 框架,主要針對的就是 Spring 唯一沒有簡化到的業務邏輯代碼,至此,開發者連僅剩的實現持久層業務邏輯的工作都省了,唯一要做的,就只是聲明持久層的接口,其他都交給 Spring Data JPA 來幫你完成!

簡單的說,Spring Data JPA是用來持久化數據的框架。

@EnableTransactionManagement

@EnableTransactionManagement注解開啟注解式事務的支持。

注解@EnableTransactionManagement通知Spring,@Transactional注解的類被事務的切面包圍。這樣@Transactional就可以使用了。

@EnableCaching

@EnableCaching注解開啟注解式的緩存支持

通過這些簡單的@Enable*可以開啟一項功能的支持,從而避免自己配置大量的代碼,很大程度上降低了使用難度。

我們一起來觀察下這些@Enable*注解的源碼,可以發現所有的注解都有一個@Import注解。

@Import注解是用來導入配置類的,這也就是說這些自動開啟的實現其實是導入了一些自動配置的Bean。

這些導入配置方式主要分為以下三種類型。

@Import注解導入配置方式的三種類型

第一類:直接導入配置類

// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) //package org.springframework.scheduling.annotation;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.Import; import org.springframework.scheduling.annotation.SchedulingConfiguration;@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Import({SchedulingConfiguration.class}) @Documented public @interface EnableScheduling { }
  • ?

直接導入配置類SchedulingConfiguration,這個類注解了@Configuration,且注冊了一個scheduledAnnotationProcessor的Bean,源碼如下:

/** Copyright 2002-2015 the original author or authors.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.springframework.scheduling.annotation;import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Role; import org.springframework.scheduling.config.TaskManagementConfigUtils;/*** {@code @Configuration} class that registers a {@link ScheduledAnnotationBeanPostProcessor}* bean capable of processing Spring's @{@link Scheduled} annotation.** <p>This configuration class is automatically imported when using the* @{@link EnableScheduling} annotation. See {@code @EnableScheduling}'s javadoc* for complete usage details.** @author Chris Beams* @since 3.1* @see EnableScheduling* @see ScheduledAnnotationBeanPostProcessor*/ @Configuration @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public class SchedulingConfiguration {@Bean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)@Role(BeanDefinition.ROLE_INFRASTRUCTURE)public ScheduledAnnotationBeanPostProcessor scheduledAnnotationProcessor() {return new ScheduledAnnotationBeanPostProcessor();}}
  • ?

第二類:依據條件選擇配置類

EnableAsync 注解核心代碼:

@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Import(AsyncConfigurationSelector.class) public @interface EnableAsync {Class<? extends Annotation> annotation() default Annotation.class;boolean proxyTargetClass() default false;AdviceMode mode() default AdviceMode.PROXY;int order() default Ordered.LOWEST_PRECEDENCE;} ublic class AsyncConfigurationSelector extends AdviceModeImportSelector<EnableAsync> {private static final String ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME ="org.springframework.scheduling.aspectj.AspectJAsyncConfiguration";/*** {@inheritDoc}* @return {@link ProxyAsyncConfiguration} or {@code AspectJAsyncConfiguration} for* {@code PROXY} and {@code ASPECTJ} values of {@link EnableAsync#mode()}, respectively*/@Overridepublic String[] selectImports(AdviceMode adviceMode) {switch (adviceMode) {case PROXY:return new String[] { ProxyAsyncConfiguration.class.getName() };case ASPECTJ:return new String[] { ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME };default:return null;}}}
  • ?

第三類:動態注冊Bean

@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Import(AspectJAutoProxyRegistrar.class) public @interface EnableAspectJAutoProxy {boolean proxyTargetClass() default false; }
  • ?

本文章由[諳憶]編寫, 所有權利保留。

轉載請注明出處:http://blog.csdn.net/qq_26525215

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的【Spring】Spring高级话题-@Enable***注解的工作原理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。