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

Java Bean vs POJO vs Spring Bean Explained

pojo class notes

Uploaded by

Pratik Patil
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)
8 views2 pages

Java Bean vs POJO vs Spring Bean Explained

pojo class notes

Uploaded by

Pratik Patil
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

Exploring Java Bean vs POJO vs Spring Bean 16 Constructor-based : Dependencies are set by

creating the Bean using its Constructor Setter-based : Dependencies are set by calling setter
methods on your beans Field: No setter or constructor. Dependency is injected using
reflection. Question: Which one should you use? Spring team recommends Constructor-
based injection as dependencies are automatically set when an object is created! Exploring
Spring - Dependency Injection Types 17 When a dependency needs to be @Autowired, IOC
container looks for matches/candidates (by name and/or type) 1: If no match is found
Result: Exception is thrown You need to help Spring Framework find a match Typical
problems: @Component (or ..) missing Class not in component scan 2: One match is found
Result: Autowiring is successful 3: Multiple candidates Result: Exception is thrown You need
to help Spring Framework choose between the candidates 1: Mark one of them as @Primary
If only one of the candidates is marked @Primary, it becomes the auto-wired value 2: Use
@Qualifier - Example: @Qualifier("myQualifierName") Provides more specific control Can be
used on a class, member variables and method parameters Exploring auto-wiring in depth
18 @Primary vs @Qualifier - Which one to use? @Primary - A bean should be given
preference when multiple candidates are qualified @Qualifier - A specific bean should be
auto-wired (name of the bean can be used as qualifier) ALWAYS think from the perspective
of the class using the SortingAlgorithm: 1: Just @Autowired: Give me (preferred)
SortingAlgorithm 2: @Autowired + @Qualifier: I only want to use specific SortingAlgorithm -
RadixSort (REMEMBER) @Qualifier has higher priority then @Primary @Component
@Primary class QuickSort implement SortingAlgorithm {} @Component class BubbleSort
implement SortingAlgorithm {} @Component @Qualifier("RadixSortQualifier") class
RadixSort implement SortingAlgorithm {} @Component class ComplexAlgorithm
@Autowired private SortingAlgorithm algorithm; @Component class
AnotherComplexAlgorithm @Autowired @Qualifier("RadixSortQualifier") private
SortingAlgorithm iWantToUseRadixSortOnly; 19 @Component (..): An instance of class will
be managed by Spring framework Dependency: GameRunner needs GamingConsole impl!
GamingConsole Impl (Ex: MarioGame) is a dependency of GameRunner Component Scan:
How does Spring Framework find component classes? It scans packages!
(@ComponentScan("com.in28minutes")) Dependency Injection: Identify beans, their
dependencies and wire them together (provides IOC - Inversion of Control) Spring Beans: An
object managed by Spring Framework IoC container: Manages the lifecycle of beans and
dependencies Types: ApplicationContext (complex), BeanFactory (simpler features - rarely
used) Autowiring: Process of wiring in dependencies for a Spring Bean Spring Framework -
Important Terminology 20 @Component vs @Bean Heading @Component @Bean Where?
Can be used on any Java class Typically used on methods in Spring Configuration classes Ease
of use Very easy. Just add an annotation. You write all the code. Autowiring Yes - Field, Setter
or Constructor Injection Yes - method call or method parameters Who creates beans? Spring
Framework You write bean creation code Recommended For Instantiating Beans for Your
Own Application Code: @Component 1:Custom Business Logic 2: Instantiating Beans for 3rd-
party libraries: @Bean Beans per class? One (Singleton) or Many (Prototype) One or Many -
You can create as many as you want 21 - In Game Runner Hello World App, we have very few
classes BUT Real World applications are much more complex: Multiple Layers (Web,
Business, Data etc) Each layer is dependent on the layer below it! Example: Business Layer
class talks to a Data Layer class Data Layer class is a dependency of Business Layer class
There are thousands of such dependencies in every application! With Spring Framework:
INSTEAD of FOCUSING on objects, their dependencies and wiring You can focus on the
business logic of your application! Spring Framework manages the lifecycle of objects: Mark
components using annotations: @Component (and others..) Mark dependencies using
@Autowired Allow Spring Framework to do its magic! Ex: BusinessCalculationService Why
do we have a lot of Dependencies? 22 Create classes and interfaces as needed Use
constructor injection to inject dependencies Make MongoDbDataService as primary Create a
Spring Context Prefer annotations Retrieve BusinessCalculationService bean and run
findMax method

You might also like