From e94d701af31e22f0c8a66d0fac2459b22ab258ec Mon Sep 17 00:00:00 2001 From: sgoudham Date: Wed, 20 Jan 2021 13:32:50 +0000 Subject: [PATCH] Demonstrate Bean Autowiring --- .../conference/with/autowiring/AppConfig.java | 9 +++++ .../with/autowiring/Application.java | 21 ++++++++++++ .../with/autowiring/model/Speaker.java | 28 +++++++++++++++ .../HibernateSpeakerRepositoryImpl.java | 18 ++++++++++ .../repository/SpeakerRepository.java | 9 +++++ .../autowiring/service/SpeakerService.java | 9 +++++ .../service/SpeakerServiceImpl.java | 34 +++++++++++++++++++ 7 files changed, 128 insertions(+) create mode 100644 Spring/src/main/java/conference/with/autowiring/AppConfig.java create mode 100644 Spring/src/main/java/conference/with/autowiring/Application.java create mode 100644 Spring/src/main/java/conference/with/autowiring/model/Speaker.java create mode 100644 Spring/src/main/java/conference/with/autowiring/repository/HibernateSpeakerRepositoryImpl.java create mode 100644 Spring/src/main/java/conference/with/autowiring/repository/SpeakerRepository.java create mode 100644 Spring/src/main/java/conference/with/autowiring/service/SpeakerService.java create mode 100644 Spring/src/main/java/conference/with/autowiring/service/SpeakerServiceImpl.java diff --git a/Spring/src/main/java/conference/with/autowiring/AppConfig.java b/Spring/src/main/java/conference/with/autowiring/AppConfig.java new file mode 100644 index 0000000..bbca220 --- /dev/null +++ b/Spring/src/main/java/conference/with/autowiring/AppConfig.java @@ -0,0 +1,9 @@ +package conference.with.autowiring; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan({"conference.with.autowiring"}) +public class AppConfig { +} diff --git a/Spring/src/main/java/conference/with/autowiring/Application.java b/Spring/src/main/java/conference/with/autowiring/Application.java new file mode 100644 index 0000000..e959466 --- /dev/null +++ b/Spring/src/main/java/conference/with/autowiring/Application.java @@ -0,0 +1,21 @@ +package conference.with.autowiring; + +import conference.with.autowiring.service.SpeakerService; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class Application { + public static void main(String[] args) { + + ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); + SpeakerService service = applicationContext.getBean("speakerService", SpeakerService.class); + + showObjectAddress(applicationContext.getBean("speakerService", SpeakerService.class)); + System.out.println(service.findAll().get(0).getForename()); + showObjectAddress(applicationContext.getBean("speakerService", SpeakerService.class)); + } + + private static void showObjectAddress(SpeakerService speakerService) { + System.out.println(speakerService); + } +} diff --git a/Spring/src/main/java/conference/with/autowiring/model/Speaker.java b/Spring/src/main/java/conference/with/autowiring/model/Speaker.java new file mode 100644 index 0000000..a568b96 --- /dev/null +++ b/Spring/src/main/java/conference/with/autowiring/model/Speaker.java @@ -0,0 +1,28 @@ +package conference.with.autowiring.model; + +public class Speaker { + + private String forename; + private String surname; + + public Speaker(String forename, String surname) { + this.forename = forename; + this.surname = surname; + } + + public String getForename() { + return forename; + } + + public void setForename(String forename) { + this.forename = forename; + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } +} diff --git a/Spring/src/main/java/conference/with/autowiring/repository/HibernateSpeakerRepositoryImpl.java b/Spring/src/main/java/conference/with/autowiring/repository/HibernateSpeakerRepositoryImpl.java new file mode 100644 index 0000000..b30f070 --- /dev/null +++ b/Spring/src/main/java/conference/with/autowiring/repository/HibernateSpeakerRepositoryImpl.java @@ -0,0 +1,18 @@ +package conference.with.autowiring.repository; + +import conference.with.autowiring.model.Speaker; +import org.springframework.stereotype.Repository; + +import java.util.ArrayList; +import java.util.List; + +@Repository("speakerRepository") +public class HibernateSpeakerRepositoryImpl implements SpeakerRepository { + + @Override + public List findAll() { + return new ArrayList<>() {{ + add(new Speaker("Goudham", "Suresh")); + }}; + } +} diff --git a/Spring/src/main/java/conference/with/autowiring/repository/SpeakerRepository.java b/Spring/src/main/java/conference/with/autowiring/repository/SpeakerRepository.java new file mode 100644 index 0000000..dd1c30b --- /dev/null +++ b/Spring/src/main/java/conference/with/autowiring/repository/SpeakerRepository.java @@ -0,0 +1,9 @@ +package conference.with.autowiring.repository; + +import conference.with.autowiring.model.Speaker; + +import java.util.List; + +public interface SpeakerRepository { + List findAll(); +} diff --git a/Spring/src/main/java/conference/with/autowiring/service/SpeakerService.java b/Spring/src/main/java/conference/with/autowiring/service/SpeakerService.java new file mode 100644 index 0000000..3c5dbdc --- /dev/null +++ b/Spring/src/main/java/conference/with/autowiring/service/SpeakerService.java @@ -0,0 +1,9 @@ +package conference.with.autowiring.service; + +import conference.with.autowiring.model.Speaker; + +import java.util.List; + +public interface SpeakerService { + List findAll(); +} diff --git a/Spring/src/main/java/conference/with/autowiring/service/SpeakerServiceImpl.java b/Spring/src/main/java/conference/with/autowiring/service/SpeakerServiceImpl.java new file mode 100644 index 0000000..04f4383 --- /dev/null +++ b/Spring/src/main/java/conference/with/autowiring/service/SpeakerServiceImpl.java @@ -0,0 +1,34 @@ +package conference.with.autowiring.service; + +import conference.with.autowiring.model.Speaker; +import conference.with.autowiring.repository.SpeakerRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service("speakerService") +public class SpeakerServiceImpl implements SpeakerService { + + private SpeakerRepository speakerRepository; + + public SpeakerServiceImpl() { + System.out.println("SpeakerServiceImpl No Args Constructor Called"); + } + + public SpeakerServiceImpl(SpeakerRepository speakerRepository) { + System.out.println("SpeakerServiceImpl Repository Constructor Called"); + this.speakerRepository = speakerRepository; + } + + @Autowired + public void setSpeakerRepository(SpeakerRepository speakerRepository) { + System.out.println("SpeakerServiceImpl Setter Called"); + this.speakerRepository = speakerRepository; + } + + @Override + public List findAll() { + return speakerRepository.findAll(); + } +}