You are on page 1of 2

=> Annotation Based Autowiring :-

-> It will inject one bean object into another bean object automatically
using annotations
-> Annotations used in this case are :-
1. @Required
2. @Autowired
3. @Qualifier

------------------------------------

=> @Required :-
-> It is used to force the Spring Container (IoC Container) for Dependency
Injection
-> If we use @Required annotation then DI is necessary/compulsory

=> @Autowired :-
-> It is used to provide DI without using <property> tag or any other
attribute in XML Configuration file
-> @Autowired can be used with setter methods or properties/variables or
constructor
-> If we dont want to use @Required annotation then we can use "required"
member i.e. @Autowired(required = true)
-> It uses by default "byType" mechanism

=> @Qualifier :-
-> When we have multiple bean definations for single bean class in spring
configuration file, then confusion may occur that which bean it has to inject. So
to remove this confusion @Qualifier is used i.e. @Qualifier("bean-id")
-> @Qualifier is used with @Autowired annotation

===================================================================================
========

=> Stereotypes Annotation bases Autowiring :-


-> Stereotypes annotations are used to inject one bean object into another
bean object automatically
-> The main Stereotypes Annotation is "@Component"
-> Some meta-annotations in @Component are :-
1. @Controller
2. @Service
3. @Repositories

-> NOTE :- Stereotypes annotations denotes the roles of types or methods in


any architecture like MVC

----------------------------------------------

=> @Component :-
-> It is class level annotation. It is used to denote class as a component
-> Its functionality is :-
= It will scan our application for classes with @Component annotation
in provided package
= Then it will instantiate all those classes
= Then it will inject specified dependencies into them

-> NOTE : We dont need to provide bean definations in spring configuration


file

-> How @Component create objects :-


@Component
class Student ------------> Student student = new
Student();
{

@Component
class EmpModule -------------> EmpModule empModule =
new EmpModule();
{

-> How to change the object name :-


@Component("empmod")
class EmpModule -------------> EmpModule empmod = new
EmpModule();
{

-> How we can scan the multiple packages :-


1. We can provide multiple packages i.e.
<context:component-scan base-package="in.sp.beans,
in.sp.empbeans"/>
2. We can provide base package which contains all the Component classes
i.e.
<context:component-scan base-package="in.sp"/>

-> How we can inject primitive data type values ?


= by using @Value("---") annotation above properties

---------------------------------------

1. @Controller
-> It is used with the classes that acts as controller
-> It is mainly used in combination with annotation handler methods i.e.
@RequestMapping or @GetMapping or @Post or @Get etc

2. @Service
-> It is used with the classes that provides some business logics

3. @Repositories
-> It is used with the classes that are responsible for providing CRUD
operations on database

===================================================================================
=========

You might also like