Skip to content

Commit c8c322e

Browse files
author
DESKTOP-BL7US23\architbiet
committed
Merge branch 'feature/Bean_Scopes_and_Lifecycle_Methods' into develop
2 parents abb62af + 0689202 commit c8c322e

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/com/luv2code/springdemo/AnnotationDemoApp.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ public static void main(String[] args) {
1111

1212
// Retrieve the bean from Spring container
1313
Coach coach = context.getBean("tennisCoach", Coach.class);
14+
Coach otherCoach = context.getBean("tennisCoach", Coach.class);
15+
16+
System.out.println("---------------Scope Testing-------------------");
17+
boolean result = (coach == otherCoach);
18+
System.out.println("Pointing to the same object : " + result);
19+
System.out.println("Memory location of coach : " + coach);
20+
System.out.println("Memory location of otherCoach : " + otherCoach);
21+
System.out.println("---------------Scope Testing-------------------");
1422

1523
// Call methods on the bean
1624
System.out.println(coach.getDailyWorkout());

src/com/luv2code/springdemo/TennisCoach.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.luv2code.springdemo;
22

33
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Scope;
45
import org.springframework.stereotype.Component;
56

67
/**
78
* @Component - To find Spring within the component scan that which classes are beans
89
* thatSillyCoach - bean id
910
*/
1011
@Component
12+
@Scope("prototype")
1113
public class TennisCoach implements Coach {
1214

1315
@Autowired

src/notes.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Spring main class steps:
2+
1. Define/Read the Spring config file = (container) = (context)
3+
2. Retrieve the bean(s) from Spring container
4+
3. Call method(s) on the bean
5+
4. Close the context
6+
7+
8+
Bean Scopes refer to lifecycle of a bean:
9+
- How long the bean lives
10+
- How many instances are created
11+
- How is the bean shared
12+
13+
Default Scope of Spring bean is SINGLETON (shared reference to the same bean)
14+
PROTOTYPE (different references to different beans)
15+
16+
Bean Lifecycle
17+
- Add any method to the bean class
18+
- Annotate those with @PostConstruct
19+
@PreDestroy
20+
21+
22+
Spring Annotations
23+
-> @Component - To find Spring within the component scan package that which classes are beans
24+
Can also provide explicit bean id @Component("theBean")
25+
26+
-> @Autowired - To let Spring find the bean implementation(s) of FortuneService
27+
Selects the appropriate candidate with the help of @Qualifier
28+
Creates the bean and automatically injects at (Contructor, Setter, Method, Field)
29+
-> @Scope - Defines bean scope (Default:Singleton)
30+
-> @PostConstruct - Bean lifecycle
31+
-> @PreDestroy - Bean lifecycle

0 commit comments

Comments
 (0)