How Spring uses Annotations.

Ian CarsonIan Carson
3 min read

Spring Boot and Java Tutorial: Build a CRUD API

In Spring, annotations are used to mark classes, methods, and fields with metadata that Spring's container can process, reducing the need for extensive xml configuration. Annotations in spring, work technically through a combination of reflection and runtime processing. When a class, method, or field is annotated, a metadata is essentially being added to the elements. The Spring container, during initialization, scans the application's Class Path to discover classes with these annotations. Below is how it works:

  • ClassPath Scanning: Spring uses ClassPath scanning to search for classes in the project that are marked with Spring's annotations. It looks for classes with specified package names, directories, or jar files.

  • Metadata extraction: When Spring identifies a Class with a relevant annotation(e.g @Component or @Controller ). It extracts the Metadata associated with that annotation. This Metadata may include information such as Class name, the annotation type, and any attribute values associated with it, within the annotation.

  • Bean definition Creation: Spring, after extracting the Metadata from annotated classes, creates a bean definition for each classes. A bean definition contains information about the class to instantiate and how to configure it.

  • Bean Initialization: When the Spring's application context is initialized, it reads the bean definitions, and as needed, creates instances of the beans. For example, if a class is marked with @Component, Spring will create an instance of that class and manage it's lifecycle.

  • Dependency injection: In Spring @Autowired and Similar annotations, the container will analyze the dependencies between the beans. It will then inject the required dependencies by examining the type and name of fields or methods annotated with @Autowired. This injection process occurs during bean initialization.

  • AOP Processing: AOP processing annotations such as @Aspect and @Before, the Spring's AOP framework processes these annotations and weaves advice into the application at runtime, as specified in the Aspect definition.

  • Request Mapping(for Web Applications): In Spring MVC, annotations such as @RequestMapping specify the URL mappings to @Controller methods. These annotations map HTTP requests to the appropriate controller methods for handling.

  • Validation(For Data Validation): Annotations such as @Valid and @Validated trigger data validation checks when applied to methods or method parameters.

  • Property Value Injections: The @Value annotation allows for one to inject property values from property files or environment variables into the bean. During application context initialization, Spring processes these annotations, extracts associated metadata, creates bean definitions, manages beans lifecycles, and provides dependency injection.

    The class responsible for searching for annotations in the Class Path in Spring is ClassPathScanningCandidateComponentProvider and is Subclass ClassPathBeanDefinitionScanner. This class is part of the Spring Framework and is used by Spring's component Scanning Mechanisms to identify classes with Specific annotations.

    Below is how it works:

  • ClassPathBeanDefinitionScanner scans the ClassPath for classes that match certain criteria, typically classes marked with annotations like @Component, @Service, @repository, among others.

  • It uses Java's reflection capabilities to inspect the classes, and identify those with specified annotations.

  • The classes that meet the Criteria are then registered as Spring beans, and their instances are managed by the Spring Container.

This is the starting point to demystify some of the Spring Boot magic. This can be experimented with by placing breaking Point at ClassPathBeanDefinitionScanner and following the sequence of event from the Classes's annotation to the configuration of the Application's ApplicationContext.

Thank you and see you in the next one.

Credits to @rixlabs on X(Formerly Twitter).

0
Subscribe to my newsletter

Read articles from Ian Carson directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ian Carson
Ian Carson

A Disciplined, keen on details, and curious problem solver. I read and code a lot. I believe in Teamwork, Accountability, Transparency, and Competency.