0% found this document useful (0 votes)
36 views3 pages

Spring Day 2

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)
36 views3 pages

Spring Day 2

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
You are on page 1/ 3

@Component

 The @Component annotation indicates that an annotated class is a “spring


bean/component”.
 The @Component annotation tells spring container to automatically create a spring bean.

@Autowired

 The @Autowired annotation is used to inject the bean automatically.


 The @Autowired annotation is used in constructor injection, setter injection and field
injection.

@Qualifier

 The @Qualifier Annotation is used in conjunction with Autowired to avoid confusion When
we have two or more bean configured for same time.

@Primary

 We use @Primary Annotation to give higher preference to a bean when there are multiple
bean of the same type

@Bean

 The @Bean Annotation indicates that a method produces a bean to be managed by a spring
container.
 The @Bean annotation is usually declared in configuration class to create spring bean
definitions.
 By default, the bean name is same as method name. We can specify bean name using
@Bean(name = “beanName”).
 @Bean annotations provides initMethod and destoryMethod attributes to perform certain
actions after bean initialization or before bean destruction by a container.
 These annotations are used to create spring bean automatically in the application context
(spring IOC container).
 The main stereotype annotation is @Component.
 By using this annotation, Spring provides more stereotype meta-annotations, such as
@Servise, @Repository and @Controller.

 @Service annotation is used to create spring beans at the service layer.


 @Repository annotation is used to create spring bean for the Repository’s at the DAO layer.
 @Controller is used to create spring beans at the controller layer.

@Lazy

 By default, Spring creates all singleton beans eagerly at the startup/bootstrapping of the
application context.
 You can load the spring beans lazily (on-demand) using @Lazy annotation.
 @Lazy annotation can used with @Configuration, @Component and @Bean Annotations.
 Eager initialization is recommended: to avoid and default all possible errors immediately
rather than at runtime.

@Scope

 Spring bean scopes:


 The latest version of the spring framework defines 6 types of scopes:
1. Singleton 2. Prototype
3. Request 4. Session
5. Application 6. WebSocket

 The last four scopes mentioned request, session, application and WebSocket are only
available in a Web-aware application.
@Scope Annotation:

 @Scope annotation is used to define a scope of the bean.


 We use @Scope to define the scope of a @Copmponent class or a @Bean definition.
 @Singleton: only one instance of the bean is created and shared across the entire
application. This is the default scope.
 @Prototype: a new instance of the bean is created every time it is requested.

@Value

 Spring @Value annotation is used to assign default values to variables and method
arguments.
 @Value annotation is mostly used to get the value for specific property key from the
properties file.
 We can read the spring environment variables as well as system variables using @Value
annotation.

@PropertySource

 Spring @PropertySource annotation is used to provide properties file to spring environment.

@PropertySources

 Spring @PropertySources annotation is used to provide multiple properties file to spring


environment
 This annotation is used with @Configuration classes.
 Spring @PropertySource annotation is repeatable, means you can have multiple
PropertySource on a Configuration class.
 We can use @Value annotation and Environment class to read the property file.

@Controller

 Spring provides @Controller annotation to make a java class as a spring MVC controller.
 The @Controller annotation indicates that a particular class serves the role of a controller.
 Controller in spring MVC web application is a component that handles incoming http
requests.
 @Controller annotation is simply a specialization of the @Component class, Which allows us
to auto-detect Implementation classes through the class path scanning.
 We typically use @Controller in combination with a @RequestMapping annotation for
request handling methods.

You might also like