Backend Development 8 min read

Implementing a Custom @Autowired-like Annotation in Spring

This article demonstrates how to create a simple custom annotation that mimics Spring's @Autowired functionality by defining a new annotation, a BeanPostProcessor, configuring beans in XML, and wiring everything together in a test class to show the injection working at runtime.

Top Architect
Top Architect
Top Architect
Implementing a Custom @Autowired-like Annotation in Spring

The tutorial shows a step‑by‑step method for building a custom annotation that behaves like Spring's @Autowired , allowing developers to quickly experiment with dependency injection without writing complex code.

Six source files are required:

Custom annotation class package com.aqin.custom.annotation; import java.lang.annotation.*; @Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface AqinAutowired { boolean required() default true; }

BeanPostProcessor MyAnnotationBeanPostProcessor that extends AutowiredAnnotationBeanPostProcessor and replaces the default autowired type with AqinAutowired.class (the crucial line is this.autowiredAnnotationTypes.add(AqinAutowired.class); ).

Controller MyAnnotationController using the new annotation: package com.aqin.custom.annotation; public class MyAnnotationController { @AqinAutowired private MyAnnotationService myAnnotationService; public void testMyAnnotation() { myAnnotationService.test(); } }

Service MyAnnotationService that simply prints a message: package com.aqin.custom.annotation; public class MyAnnotationService { public void test() { System.out.println("(*≧ω≦)~"); } }

XML configuration populateBean.xml that registers the processor, controller and service beans: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="myAnnotationBeanPostProcessor" class="com.aqin.custom.annotation.MyAnnotationBeanPostProcessor"/> <bean id="myAnnotationController" class="com.aqin.custom.annotation.MyAnnotationController"/> <bean id="myAnnotationService" class="com.aqin.custom.annotation.MyAnnotationService"/> </beans>

Test class Test that loads the XML and invokes the controller method: package com.aqin.custom.annotation; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("populateBean.xml"); MyAnnotationController bean = ctx.getBean(MyAnnotationController.class); bean.testMyAnnotation(); } }

Running the Test class prints the expected output, confirming that the custom annotation is correctly processed by the BeanPostProcessor and the dependency is injected.

The article also analyses the internal Spring mechanisms: during bean definition merging, applyMergedBeanDefinitionPostProcessors() iterates over MergedBeanDefinitionPostProcessor implementations, invoking postProcessMergedBeanDefinition() on both the original AutowiredAnnotationBeanPostProcessor and the custom MyAnnotationBeanPostProcessor . The custom processor registers AqinAutowired in the autowiredAnnotationTypes set, so fields annotated with it are treated identically to those annotated with @Autowired .

Finally, the author invites readers to discuss the approach, provides additional resources, and includes promotional links and QR codes, but the core instructional content remains a practical guide for extending Spring's dependency injection.

backendjavaSpringCustom Annotationdependency injectionBeanPostProcessor
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login 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.