How to Create Custom @Enable Annotations in Spring Boot with @Import
Learn how Spring Boot’s @Enable* annotations work, explore the role of @Import, and follow a step‑by‑step example that defines a custom @EnablePack annotation, implements ImportAware, and demonstrates its usage in a Spring Boot application.
@Enable* annotations in Spring Boot enable specific features, such as @SpringBootApplication's @EnableAutoConfiguration, @EnableAsync, @EnableConfigurationProperties, etc.
Many of these annotations share a common use of @Import.
The @Import annotation indicates one or more component classes to import, typically @Configuration classes, and since Spring 4.2 it also supports importing ordinary classes without @Configuration.
Below is a custom @EnablePack annotation that imports a Pack class and defines an attribute maxConnections with a default value.
@Retention(RetentionPolicy.RUNTIME)
@Import(Pack.class)
public @interface EnablePack {
int maxConnections() default 1000;
}The Pack class implements ImportAware and ApplicationContextAware to receive the annotation attributes and the application context.
public class Pack implements ImportAware, ApplicationContextAware {
private ApplicationContext ctx;
private int maxConnections;
@Override
public void setImportMetadata(AnnotationMetadata annotationMetadata) {
Map<String, Object> attributesMap = annotationMetadata.getAnnotationAttributes(EnablePack.class.getName());
AnnotationAttributes attrs = AnnotationAttributes.fromMap(attributesMap);
this.maxConnections = attrs.getNumber("maxConnections");
System.out.println(ctx.getBean(ProductService.class));
System.out.println(this.maxConnections);
}
public void store() {
System.out.println(this.maxConnections);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.ctx = applicationContext;
}
}ImportAware works together with @Import; similar functionality can be achieved by extending AdviceModeImportSelector or implementing ImportSelector.
The application entry point enables caching and the custom annotation with a specific maxConnections value.
@SpringBootApplication
@EnableCaching
@EnablePack(maxConnections = 10000)
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}Running the service prints the configured values to the console.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
