javascript
【Spring】Spring高级话题-@Enable***注解的工作原理
@EnableAspectJAutoProxy
?
前些天發(fā)現(xiàn)了一個(gè)巨牛的人工智能學(xué)習(xí)網(wǎng)站,通俗易懂,風(fēng)趣幽默,忍不住分享一下給大家。點(diǎn)擊跳轉(zhuǎn)到教程。
?
@EnableAspectJAutoProxy注解 激活A(yù)spect自動(dòng)代理
<aop:aspectj-autoproxy/>- 1
開(kāi)啟對(duì)AspectJ自動(dòng)代理的支持。
在用到AOP的自動(dòng)代理的時(shí)候用,如果你理解了Java的動(dòng)態(tài)代理,很容易的就會(huì)熟悉AOP的自動(dòng)代理的。
@EnableAsync
@EnableAsync注解開(kāi)啟異步方法的支持。?
這個(gè)相信大家都比較熟悉的。對(duì)于異步應(yīng)該都理解的。?
不太熟悉的,可以看這篇博客:-有示例?
【Spring】Spring高級(jí)話題-多線程-TaskExecutor
@EnableScheduling
@EnableScheduling注解開(kāi)啟計(jì)劃任務(wù)的支持。
也就是字面上的意思,開(kāi)啟計(jì)劃任務(wù)的支持!?
一般都需要@Scheduled注解的配合。
詳情見(jiàn)此博客:?
【Spring】Spring高級(jí)話題-計(jì)劃任務(wù)-@EnableScheduling
@EnableWebMVC
@EnableWebMVC注解用來(lái)開(kāi)啟Web MVC的配置支持。
也就是寫Spring MVC時(shí)的時(shí)候會(huì)用到。
@EnableConfigurationProperties
@EnableConfigurationProperties注解是用來(lái)開(kāi)啟對(duì)@ConfigurationProperties注解配置Bean的支持。
也就是@EnableConfigurationProperties注解告訴Spring Boot 使能支持@ConfigurationProperties
@EnableJpaRepositories
@EnableJpaRepositories注解開(kāi)啟對(duì)Spring Data JPA Repostory的支持。
Spring Data JPA 框架,主要針對(duì)的就是 Spring 唯一沒(méi)有簡(jiǎn)化到的業(yè)務(wù)邏輯代碼,至此,開(kāi)發(fā)者連僅剩的實(shí)現(xiàn)持久層業(yè)務(wù)邏輯的工作都省了,唯一要做的,就只是聲明持久層的接口,其他都交給 Spring Data JPA 來(lái)幫你完成!
簡(jiǎn)單的說(shuō),Spring Data JPA是用來(lái)持久化數(shù)據(jù)的框架。
@EnableTransactionManagement
@EnableTransactionManagement注解開(kāi)啟注解式事務(wù)的支持。
注解@EnableTransactionManagement通知Spring,@Transactional注解的類被事務(wù)的切面包圍。這樣@Transactional就可以使用了。
@EnableCaching
@EnableCaching注解開(kāi)啟注解式的緩存支持
通過(guò)這些簡(jiǎn)單的@Enable*可以開(kāi)啟一項(xiàng)功能的支持,從而避免自己配置大量的代碼,很大程度上降低了使用難度。
我們一起來(lái)觀察下這些@Enable*注解的源碼,可以發(fā)現(xiàn)所有的注解都有一個(gè)@Import注解。
@Import注解是用來(lái)導(dǎo)入配置類的,這也就是說(shuō)這些自動(dòng)開(kāi)啟的實(shí)現(xiàn)其實(shí)是導(dǎo)入了一些自動(dòng)配置的Bean。
這些導(dǎo)入配置方式主要分為以下三種類型。
@Import注解導(dǎo)入配置方式的三種類型
第一類:直接導(dǎo)入配置類
// // 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 { }- ?
直接導(dǎo)入配置類SchedulingConfiguration,這個(gè)類注解了@Configuration,且注冊(cè)了一個(gè)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();}}- ?
第二類:依據(jù)條件選擇配置類
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;}}}- ?
第三類:動(dòng)態(tài)注冊(cè)Bean
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Import(AspectJAutoProxyRegistrar.class) public @interface EnableAspectJAutoProxy {boolean proxyTargetClass() default false; }- ?
本文章由[諳憶](méi)編寫, 所有權(quán)利保留。
轉(zhuǎn)載請(qǐng)注明出處:http://blog.csdn.net/qq_26525215
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的【Spring】Spring高级话题-@Enable***注解的工作原理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 还要做最后的努力!
- 下一篇: [经典推荐]事半功倍系列之javascr