From 84e5e9e36321a53ab1eecb91eda077341af46e08 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Wed, 20 Jan 2021 13:31:56 +0000 Subject: [PATCH 1/3] Update dependencies --- Spring/pom.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Spring/pom.xml b/Spring/pom.xml index 0ec0889..e929863 100644 --- a/Spring/pom.xml +++ b/Spring/pom.xml @@ -28,6 +28,11 @@ spring-context 5.2.0.RELEASE + + javax.annotation + javax.annotation-api + 1.3.2 + \ No newline at end of file From 196ca845c8ab9724855bb67e692f39069b024c36 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Wed, 20 Jan 2021 13:32:19 +0000 Subject: [PATCH 2/3] Demonstrate Bean Lifecycle with PostConstruct --- .../main/java/conference/service/SpeakerServiceImpl.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Spring/src/main/java/conference/service/SpeakerServiceImpl.java b/Spring/src/main/java/conference/service/SpeakerServiceImpl.java index cf904e4..be2d0db 100644 --- a/Spring/src/main/java/conference/service/SpeakerServiceImpl.java +++ b/Spring/src/main/java/conference/service/SpeakerServiceImpl.java @@ -3,6 +3,7 @@ package conference.service; import conference.model.Speaker; import conference.repository.SpeakerRepository; +import javax.annotation.PostConstruct; import java.util.List; public class SpeakerServiceImpl implements SpeakerService { @@ -13,6 +14,11 @@ public class SpeakerServiceImpl implements SpeakerService { this.speakerRepository = speakerRepository; } + @PostConstruct + private void initialise() { + System.out.println("Initialisation After Constructors"); + } + public void setSpeakerRepository(SpeakerRepository speakerRepository) { this.speakerRepository = speakerRepository; } From e94d701af31e22f0c8a66d0fac2459b22ab258ec Mon Sep 17 00:00:00 2001 From: sgoudham Date: Wed, 20 Jan 2021 13:32:50 +0000 Subject: [PATCH 3/3] 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(); + } +}