How @Enable Annotations Power Spring Modules – Deep Dive into @EnableAsync

This article explains the inner workings of Spring's @Enable annotations, detailing how @Import processes configuration classes, the roles of ImportSelector and ImportBeanDefinitionRegistrar, and using @EnableAsync as a concrete example to illustrate module activation.

Sanyou's Java Diary
Sanyou's Java Diary
Sanyou's Java Diary
How @Enable Annotations Power Spring Modules – Deep Dive into @EnableAsync

This article explains the implementation principle of Spring's @Enable module-driven mechanism.

In Spring, adding features like scheduled tasks or asynchronous processing involves using annotations such as @EnableScheduling or @EnableAsync, which belong to the @Enable family of annotations.

An @Enable module is a collection of related functional components, e.g., Web MVC, AspectJ, Caching, JMX, Async, etc. Enabling a module essentially toggles a switch that triggers automatic configuration of all its components.

The core of an @EnableXXX annotation is the @Import annotation. @Import imports configuration classes, and its processing occurs in the processImports method of ConfigurationClassParser.

If the imported class implements ImportSelector, Spring calls its selectImports method to obtain a list of configuration class names, which are then parsed. If it implements ImportBeanDefinitionRegistrar, Spring invokes its registerBeanDefinitions method to register additional BeanDefinition s. If neither interface is implemented, the class is treated as a regular configuration class.

The flow of @Import processing is illustrated below.

Further details of the processImports method are shown in the next diagram.

The role of ImportSelector is to inject a batch of configuration classes into the Spring container.

If a class implements ImportBeanDefinitionRegistrar, it registers additional BeanDefinition s into the container.

For @EnableAsync, the annotation imports AsyncConfigurationSelector, which extends AdviceModeImportSelector and implements ImportSelector. It reads the mode() attribute (default PROXY) and returns the fully qualified name of ProxyAsyncConfiguration, which registers AsyncAnnotationBeanPostProcessor as a BeanPostProcessor to handle @Async.

The AdviceModeImportSelector implementation is shown below.

The ProxyAsyncConfiguration class registers the AsyncAnnotationBeanPostProcessor, which processes @Async annotations during the bean lifecycle.

Understanding this mechanism enables developers to create custom @Enable annotations to activate specific functionalities.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

springAsyncBeanPostProcessorEnableAnnotationImportSelector
Sanyou's Java Diary
Written by

Sanyou's Java Diary

Passionate about technology, though not great at solving problems; eager to share, never tire of learning!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.