Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
pluralsight-projects
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Deep Bhuller
pluralsight-projects
Commits
7f21e077
Commit
7f21e077
authored
Mar 31, 2021
by
dbhuller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
explored singleton and prototype scope for beans
parent
4f4f608e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
13 additions
and
0 deletions
+13
-0
AppConfig.java
...roject/conference-javaconfig/src/main/java/AppConfig.java
+4
-0
Application.java
...ject/conference-javaconfig/src/main/java/Application.java
+9
-0
AppConfig.class
...ject/conference-javaconfig/target/classes/AppConfig.class
+0
-0
Application.class
...ct/conference-javaconfig/target/classes/Application.class
+0
-0
No files found.
springframework-springfundamentals-project/conference-javaconfig/src/main/java/AppConfig.java
View file @
7f21e077
...
...
@@ -2,13 +2,17 @@ import com.example.repository.HibernateSpeakerRepositoryImpl;
import
com.example.repository.SpeakerRepository
;
import
com.example.service.SpeakerService
;
import
com.example.service.SpeakerServiceImpl
;
import
org.springframework.beans.factory.config.BeanDefinition
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Scope
;
@Configuration
public
class
AppConfig
{
@Bean
(
name
=
"speakerService"
)
@Scope
(
value
=
BeanDefinition
.
SCOPE_SINGLETON
)
// @Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
public
SpeakerService
getSpeakerService
()
{
// Using Constructor Injection
SpeakerServiceImpl
service
=
new
SpeakerServiceImpl
(
getSpeakerRespository
());
...
...
springframework-springfundamentals-project/conference-javaconfig/src/main/java/Application.java
View file @
7f21e077
...
...
@@ -10,7 +10,16 @@ public class Application {
ApplicationContext
appContext
=
new
AnnotationConfigApplicationContext
(
AppConfig
.
class
);
// SpeakerService service = new SpeakerServiceImpl();
SpeakerService
service
=
appContext
.
getBean
(
"speakerService"
,
SpeakerService
.
class
);
System
.
out
.
println
(
service
);
System
.
out
.
println
(
service
.
findALl
().
get
(
0
).
getFirstName
());
// Returns same bean as service --> Singleton
// With scope Prototype, returns two different instances of each bean --> Prototype
SpeakerService
service2
=
appContext
.
getBean
(
"speakerService"
,
SpeakerService
.
class
);
System
.
out
.
println
(
service2
);
}
}
springframework-springfundamentals-project/conference-javaconfig/target/classes/AppConfig.class
View file @
7f21e077
No preview for this file type
springframework-springfundamentals-project/conference-javaconfig/target/classes/Application.class
View file @
7f21e077
No preview for this file type
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment