![]() |
VOOZH | about |
Spring Annotations are metadata in Java used to provide configuration and behavior information to the Spring Framework. They eliminate the need for XML-based configuration, making Spring applications more concise, readable, and easier to maintain.
In essence, annotations tell the Spring Container how to create, configure, and manage application components (beans). They are widely used for dependency injection, bean configuration, web components, and context management.
Annotations in the org.springframework.beans.factory.annotation and org.springframework.context.annotation packages are collectively called Spring Core Annotations.
They can be divided into two main categories:
These annotations are used to perform dependency injection and define how beans are created and wired together in the Spring container.
Automatically injects dependencies by type. It can be used on constructors, fields, or setter methods.
Example (Field Injection):
Example (Constructor Injection):
Example (Setter Injection):
Used with @Autowired to resolve conflicts when multiple beans of the same type exist. It specifies the exact bean name to be injected.
Specifies a default bean when multiple beans of the same type are available for autowiring.
Indicates that a method produces a Spring-managed bean. Used inside @Configuration classes.
Delays the initialization of a bean until it is first requested.
Injects values from properties files, environment variables, or expression language.
Defines the scope of a Spring bean. Common scopes:
Used for method injection, it allows a method to return a new instance of a prototype-scoped bean each time itβs called.
Previously used to indicate that a property must be injected, now deprecated as of Spring 5.1.
These annotations are used to define how Spring discovers, configures, and loads beans into the ApplicationContext.
Marks a class as a source of bean definitions. Beans within this class are defined using @Bean methods.
Specifies the base packages to scan for Spring-managed components like @Component, @Service, or @Repository.
Used to import additional @Configuration classes into another configuration class.
Imports legacy XML-based configurations into a Java-based configuration class.
Imports legacy XML-based configurations into a Java-based configuration class.
Specifies that a bean should only be registered when a particular Spring profile is active.
Loads a .properties file into the Spring environment so that property values can be injected using @Value.
Conditionally registers a bean based on a custom condition class.