Spring Day 2
Spring Day 2
@Autowired
@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.
@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
The last four scopes mentioned request, session, application and WebSocket are only
available in a Web-aware application.
@Scope Annotation:
@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
@PropertySources
@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.