0% found this document useful (0 votes)
9 views2 pages

Essential Spring Annotations Guide

The document outlines various Spring annotations used for dependency injection and bean management, including @Required, @Autowired, @Qualifier, and JSR-250 annotations. It explains the roles of annotations like @Configuration, @Bean, @ComponentScan, @Component, @Service, @Repository, and @Controller in the Spring framework. These annotations facilitate automatic component scanning, bean registration, and the specification of bean types within the Spring IoC container.

Uploaded by

Utkarsh Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Essential Spring Annotations Guide

The document outlines various Spring annotations used for dependency injection and bean management, including @Required, @Autowired, @Qualifier, and JSR-250 annotations. It explains the roles of annotations like @Configuration, @Bean, @ComponentScan, @Component, @Service, @Repository, and @Controller in the Spring framework. These annotations facilitate automatic component scanning, bean registration, and the specification of bean types within the Spring IoC container.

Uploaded by

Utkarsh Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SPRING ANNOTATIONS

@Required

The @Required annotation applies to


bean property setter methods.

2 @Autowired
The @Autowired annotation can apply to bean property setter
methods, non-setter methods, constructor and properties.

3 @Qualifier
The @Qualifier annotation along with @Autowired can be used to
remove the confusion by specifiying which exact bean will be wired.

4 JSR-250 Annotations
Spring supports JSR-250 based annotations which include
@Resource, @PostConstruct and @PreDestroy annotations.

@Configuration indicates that the class can be used by the Spring IoC container as a
source of bean definitions.
The @Bean annotation tells Spring that a method annotated with @Bean will return an
object that should be registered as a bean in the Spring application context.

@Import annotation allows for loading @Bean definitions from another configuration class.

1. @ComponentScan:
Configures component scanning directives for use with
@Configuration classes. Here we can specify the base packages to
scan for spring components.
2. @Component: Indicates that an annotated class is a “component”. Such
classes are considered as candidates for auto-detection when using
annotation-based configuration and classpath scanning.
3. @PropertySource: provides a simple declarative mechanism for adding a
property source to Spring’s Environment. There is a similar
annotation for adding an array of property source files
i.e @PropertySources.
4. @Service: Indicates that an annotated class is a “Service”. This
annotation serves as a specialization of @Component, allowing for
implementation classes to be autodetected through classpath
scanning.
SPRING ANNOTATIONS

5. @Repository:
Indicates that an annotated class is a “Repository”. This
annotation serves as a specialization of @Component and advisable
to use with DAO classes.
6. @Autowired: Spring @Autowired annotation is used for automatic
injection of beans. Spring @Qualifier annotation is used in
conjunction with Autowired to avoid confusion when we have two of
more bean configured for same type.

7. With @Component, @Repository, @Service and @Controller annotations in place


and automatic component scanning enabled, Spring will automatically
import the beans into the container and inject to dependencies. These
annotations are called Stereotype annotations as well.
8. @Component annotation marks a java class as a bean so the component-
scanning mechanism of spring can pick it up and pull it into the application
context.
9. @Repository annotation is a specialization of the @Component annotation with
similar use and functionality. In addition to importing the DAOs into the DI
container, it also makes the unchecked exceptions (thrown from DAO
methods) eligible for translation into Spring DataAccessException. it also
considers the unchecked exceptions that can be used in Spring.
10.
11. @Service annotation is also a specialization of the component annotation. It
doesn’t currently provide any additional behavior over
the @Component annotation, but it’s a good idea to
use @Service over @Component in service-layer classes because it specifies
intent better.
12.
13. @Controller annotation marks a class as a Spring Web MVC controller. It too
is a @Component specialization, so beans marked with it are automatically
imported into the DI container. When we add the @Controller annotation to a
class, we can use another annotation i.e. @RequestMapping; to map URLs to
instance methods of a class.
14.

Common questions

Powered by AI

In Spring, the @Configuration annotation indicates that a class can be used as a source of bean definitions for the Spring IoC container. Within a class annotated with @Configuration, methods annotated with @Bean define individual beans that should be registered and managed within the container. This approach allows developers to define how application components are created and configured, supporting Spring's dependency injection and lifecycle management .

The @PropertySource annotation in Spring is used to declare a property source to the application's environment, typically specifying a properties file. This provides a declarative and conventional way to externalize configuration parameters, allowing for easier management of environment-specific settings that can be changed without altering the code base. This is especially helpful in environments where different configurations are needed for different environments like development, testing, and production .

The @Repository annotation is specifically for DAO (Data Access Object) classes in the Spring framework. It serves as a specialization of the @Component annotation with additional functionality. One critical feature is the automatic translation of data access exceptions into Spring's unchecked exceptions hierarchy, specifically, into Spring's DataAccessException. This ensures that any persistence-related exceptions thrown in the DAO layer are consistent with the exception models used throughout the rest of the application, making exception handling more manageable and consistent .

The @Resource annotation is part of the JSR-250 specification and is supported by Spring for dependency injection, similar to @Autowired. However, @Resource follows Java EE standards, allowing for injection based on bean name as well as type. By default, it behaves like a Java EE resource injection, where it first looks for a match by the bean's name before considering its type, offering an alternative injection strategy to the type-focused injection of @Autowired. This can be particularly useful in scenarios where precise control over injected bean names is required .

In a Spring MVC application, the @Controller annotation is used to indicate that a particular class serves the role of a web controller, handling requests and returning responses. It is a specialization of @Component, which means it makes the class a Spring bean. The @RequestMapping annotation is then used within a class marked with @Controller to map specific URLs or URL patterns to the corresponding handler methods. This allows the controller to direct incoming web requests to the appropriate method based on the URL, ensuring that the correct logic is executed in response to client requests .

In Spring, both @Service and @Component annotations make a class a candidate for component scanning and automatic registration by the IoC container. While technically both achieve the same functionality, using @Service over @Component for service-layer classes is considered a best practice because it explicitly indicates the role of the class in the application. @Service adds semantic clarity, signaling that the class provides business services, even though it doesn't add any extra behavior beyond what @Component offers .

The @Import annotation in Spring is used to import @Bean definitions from other configuration classes into the current application context. This facilitates modularization and reuse of configuration logic across different components of an application. By using @Import, developers can split configuration into multiple classes, making it easier to manage and maintain configuration logic. It also promotes separation of concerns, dividing application setup across logically grouped configuration classes, which enhances scalability and readability .

The @PostConstruct annotation is part of JSR-250 annotations supported by Spring, and it enhances the initialization process of beans by marking a method that needs to be executed after dependency injection is done. This allows developers to perform any initialization actions, such as setting up resources or validating conditions, after a bean's dependencies have been injected but before the bean is put into use. It provides a clear lifecycle phase where initialization logic can be handled consistently across beans .

Stereotype annotations in Spring, such as @Component, @Service, @Repository, and @Controller, act as markers for Spring's component-scanning mechanism to detect and register beans within the IoC container automatically. These annotations are specializations of the @Component annotation and signify the role a particular class plays in the application, allowing Spring to organize and manage the beans effectively. By enabling automatic component scanning, these annotations facilitate managing bean dependencies across different layers of an application .

The @Autowired annotation in Spring is used to automatically inject beans into a class's properties or methods. However, when multiple beans of the same type exist in the Spring context, @Autowired alone can't distinguish between them to determine which one to inject. This is where the @Qualifier annotation becomes useful; it allows developers to specify exactly which bean should be injected by explicitly naming the bean. Without @Qualifier, Spring would throw an exception in the case of multiple beans of the same type .

You might also like