Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views

Spring Annotation Based Configuration Class Notes

The document discusses Spring annotation based configuration. It explains the usage of important annotations like @Component, @ComponentScan, @Autowired, @Qualifier to configure Spring beans. It also describes stereotype annotations @Controller, @Service, @Repository and their purpose.

Uploaded by

George Popa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Spring Annotation Based Configuration Class Notes

The document discusses Spring annotation based configuration. It explains the usage of important annotations like @Component, @ComponentScan, @Autowired, @Qualifier to configure Spring beans. It also describes stereotype annotations @Controller, @Service, @Repository and their purpose.

Uploaded by

George Popa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Spring Annotation

Based Configuration

Ramesh Fadatare ( Java Guides)


Steps for Annotation based configuration
1. Annotated a class with @Component annotatio
2. Use @ComponentScan annotation to specify package name for
scanning those classes that are annotated with @Component
annotatio
3. Use @Autowired annotation to automatically inject the Spring bea
4. Use @Quali er annotation to avoid the confusion between
multiple Spring beans of the same typ
5. Create Spring IoC Container (ApplicationContext) and Retrieve
Spring bean from Spring IoC container.

Ramesh Fadatare ( Java Guides)


n

fi
e

1. Annotate a class with @Component annotation


@Component annotation tells Spring IoC
container to automatically create Spring bean
2. Use @ComponentScan annotation to specify package name

This annotation is used to specify the base packages to


scan for spring beans/components.
3. Use @Autowired annotation to automatically inject the bean

@Autowired annotation tells Spring IoC


container to inject the bean automatically
4. Use @Qualifier annotation to avoid confusion

@Quali er annotation is used in


conjunction with Autowired to avoid
confusion when we have two or
more beans con gured for same
type.
fi
fi
5. Create Spring IoC container and retrieve bean

Ramesh Fadatare ( Java Guides)


Stereotype annotations
1. These annotations are used to create Spring beans automatically in
the application context (Spring IoC container
2. The main stereotype annotation is @Component
3. By using this annotation, Spring provides more Stereotype meta
annotations such as @Service, @Repository and @Controlle
4. @Service annotation is used to create Spring beans at the Service
laye
5. @Repository is used to create Spring beans for the repositories at
the DAO laye
6. @Controller is used to create Spring beans at the controller layer
Ramesh Fadatare ( Java Guides)
r

Spring Stereotype Annotations


@Component
Java Guides
Java Guides
Java Guides Java Guides

@Controller @Service @Repository

Controller Classes Service Classes in Repository Classes


in Controller Layer Service Layer in DAO Layer

Ramesh Fadatare ( Java Guides)


Java base Annotation base
Con guration Con guration

Create a method and annota on it with Annotate a class with @Component


@Bean annota on annota on

We need to write a code to create objects Spring IoC container take care of crea ng
and inject the dependencies objects and injec ng the dependencies

Annota ons used: @Component,


Annota ons used: @Con gura on and @Autowired, @Quali er, @Primary,
@Bean @ComponentScan
@Controller, @Service, @Repository

Ramesh Fadatare ( Java Guides)


fi
fi
ti
ti
ti
d

ti
ti
d

fi
fi
ti
ti

ti
Spring Framework Annotations and Terminologies
Spring bean: In Spring, the objects that form the backbone of your applica on and that are managed by
the Spring IoC container are called beans.
A bean is an object that is created and managed by a Spring IoC container.
@Con gura on: Used to indicate that a con gura on class declares one or more @Bean methods.
These classes are processed by the Spring container to generate bean de ni ons and service requests
for those beans at run me.

@Bean: Indicates that a method produces a bean to be managed by the Spring container.

@Component: Indicates that an annotated class is a “spring bean”. Such classes are considered as
candidates for auto-detec on when using annota on-based con gura on and classpath scanning.

@ComponentScan: This annota on is used to specify the base packages to scan for spring beans/
components.

@Autowired: Spring @Autowired annota on is used for automa c injec on of beans.


fi
ti
ti
ti
ti
ti

fi
ti
ti
fi
ti
ti
ti
fi
ti
ti
Spring Framework Annotations and Terminologies
@Quali er annota on is used in conjunc on with Autowired to avoid confusion when we have two or
more beans con gured for same type.

@Primary: We use @Primary annota on to give higher preference to a bean when there are mul ple
beans of the same type.

Dependency: An object usually requires objects of other classes to perform its opera ons. We call these
objects dependencies.

Injec on/Autowiring: The process of providing the required dependencies to an object.

Spring IoC Container: Responsible for crea ng the objects (spring beans), injec ng one abject into
another object and managing the spring bean’s en re life-cycle
Dependency Injec on: Dependency Injec on is a design pa ern on which dependency of the object is
injected by the framework rather than created by Object itself.
Dependency Injec on iden es beans, their dependencies and wire/inject the depedency
ti
fi
fi
ti
ti
ti
ti
fi
ti
ti
ti
ti
ti
tt

ti
ti

ti

You might also like