Spring Framework is known for its powerful Dependency Injection (DI) and Inversion of Control (IoC) features. One of the key factors that make Spring so flexible and powerful is its use of annotations. These annotations help simplify configuration, reduce boilerplate code, and enable a more declarative approach to programming. In this blog, we'll walk through some of the most important annotations in Spring, explaining their purpose and use cases.
1. @Component
The @Component annotation is one of the most important and
commonly used annotations in Spring. It is used to mark a class as a
Spring-managed bean. When Spring starts up, it will automatically discover all
the classes annotated with @Component (and its specializations)
and register them as beans in the Spring context.
In the example above, the MyService class is automatically
registered as a Spring bean during component scanning.
Specializations:
-
@Service– A specialization of@Componentused to indicate that the class provides some business service. -
@Repository– A specialization of@Componentthat indicates the class is a Data Access Object (DAO) and deals with persistence-related operations. -
@Controller– A specialization of@Componentthat designates the class as a Spring MVC controller.
2. @Autowired (Dependency Injection)
The @Autowired annotation is used for automatic dependency
injection in Spring. It allows Spring to resolve and inject collaborating
beans into a class. By marking a field, constructor, or setter method with
@Autowired, Spring automatically wires the dependencies.
Constructor Injection (Recommended):
Field Injection:
Setter Injection:
3. @Configuration
@Configuration is used to indicate that a class contains Spring
bean definitions. It marks the class as a source of bean definitions for the
Spring container. This annotation is typically used when defining beans via
Java configuration.
In the example above, the @Bean annotation defines beans that
Spring will manage.
4. @Bean
The @Bean annotation is used within a
@Configuration class to explicitly declare a bean. This is an
alternative to @Component for more fine-grained control over bean
instantiation and configuration.
5. @Value
The @Value annotation is used to inject values into Spring beans.
It can be used to inject values from application properties, environment
variables, or expressions.
In this case, the value of app.name will be injected from the
application's application.properties or
application.yml file.
6. @Qualifier
When there are multiple beans of the same type, the
@Qualifier annotation helps Spring determine which one to inject.
It is often used in combination with @Autowired to avoid
ambiguity.
In this example, @Qualifier specifies which
MyRepository implementation to inject.
7. @RequestMapping (and related annotations)
In Spring MVC, @RequestMapping is used to map HTTP requests to
handler methods of MVC controllers. This annotation can be used at the method
or class level. It is often specialized with HTTP method annotations.
@RequestMapping:
Specializations:
@GetMapping– Maps HTTP GET requests.@PostMapping– Maps HTTP POST requests.@PutMapping– Maps HTTP PUT requests.@DeleteMapping– Maps HTTP DELETE requests.
Each of these is a more specific version of @RequestMapping,
making the code more readable and expressive.
8. @EnableAutoConfiguration
@EnableAutoConfiguration is used in Spring Boot applications to
automatically configure Spring beans based on the project's dependencies. It
is typically used in combination with @SpringBootApplication to
enable auto-configuration for common Spring setup tasks.
This single annotation enables various auto-configuration features, such as setting up data sources, web configurations, and more.
9. @Transactional
The @Transactional annotation is used to indicate that a method
or class should run within a transaction context. Spring manages the
transaction boundaries (begin, commit, rollback) for the annotated method.
This annotation ensures that if any exception occurs during the method execution, the transaction is rolled back, maintaining consistency.
10. @PostConstruct and @PreDestroy
@PostConstruct and @PreDestroy are used to mark
methods that should be executed after the bean initialization and before bean
destruction, respectively. These are useful for setting up and cleaning up
resources.
Conclusion
Spring's annotations provide a powerful and flexible way to manage dependencies, configure beans, handle HTTP requests, and manage transactions. Understanding these core annotations will significantly improve your ability to write cleaner, more efficient, and maintainable Spring-based applications. Keep exploring the Spring documentation for more specialized annotations as you deepen your knowledge.

0 Comments