From d0e17b03bc8c098610d355f682da79e5f782a536 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Tue, 19 Jan 2021 12:20:31 +0000 Subject: [PATCH] Demonstrate Spring --- Spring/pom.xml | 33 +++++++++++++++++++ .../src/main/java/conference/AppConfig.java | 23 +++++++++++++ .../src/main/java/conference/Application.java | 15 +++++++++ .../main/java/conference/model/Speaker.java | 28 ++++++++++++++++ .../HibernateSpeakerRepositoryImpl.java | 16 +++++++++ .../repository/SpeakerRepository.java | 9 +++++ .../conference/service/SpeakerService.java | 9 +++++ .../service/SpeakerServiceImpl.java | 24 ++++++++++++++ 8 files changed, 157 insertions(+) create mode 100644 Spring/pom.xml create mode 100644 Spring/src/main/java/conference/AppConfig.java create mode 100644 Spring/src/main/java/conference/Application.java create mode 100644 Spring/src/main/java/conference/model/Speaker.java create mode 100644 Spring/src/main/java/conference/repository/HibernateSpeakerRepositoryImpl.java create mode 100644 Spring/src/main/java/conference/repository/SpeakerRepository.java create mode 100644 Spring/src/main/java/conference/service/SpeakerService.java create mode 100644 Spring/src/main/java/conference/service/SpeakerServiceImpl.java diff --git a/Spring/pom.xml b/Spring/pom.xml new file mode 100644 index 0000000..0ec0889 --- /dev/null +++ b/Spring/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + com.pluralsight + Spring + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 14 + 14 + + + + + + + + org.springframework + spring-context + 5.2.0.RELEASE + + + + \ No newline at end of file diff --git a/Spring/src/main/java/conference/AppConfig.java b/Spring/src/main/java/conference/AppConfig.java new file mode 100644 index 0000000..ee7e305 --- /dev/null +++ b/Spring/src/main/java/conference/AppConfig.java @@ -0,0 +1,23 @@ +package conference; + +import conference.repository.HibernateSpeakerRepositoryImpl; +import conference.repository.SpeakerRepository; +import conference.service.SpeakerService; +import conference.service.SpeakerServiceImpl; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration + +public class AppConfig { + + @Bean(name = "speakerService") + public SpeakerService getSpeakerService() { + return new SpeakerServiceImpl(getSpeakerRepository()); + } + + @Bean(name = "speakerRepository") + public SpeakerRepository getSpeakerRepository() { + return new HibernateSpeakerRepositoryImpl(); + } +} diff --git a/Spring/src/main/java/conference/Application.java b/Spring/src/main/java/conference/Application.java new file mode 100644 index 0000000..b402a53 --- /dev/null +++ b/Spring/src/main/java/conference/Application.java @@ -0,0 +1,15 @@ +package conference; + +import conference.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); + + System.out.println(service.findAll().get(0).getForename()); + } +} diff --git a/Spring/src/main/java/conference/model/Speaker.java b/Spring/src/main/java/conference/model/Speaker.java new file mode 100644 index 0000000..cfb3cad --- /dev/null +++ b/Spring/src/main/java/conference/model/Speaker.java @@ -0,0 +1,28 @@ +package conference.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/repository/HibernateSpeakerRepositoryImpl.java b/Spring/src/main/java/conference/repository/HibernateSpeakerRepositoryImpl.java new file mode 100644 index 0000000..c5b671e --- /dev/null +++ b/Spring/src/main/java/conference/repository/HibernateSpeakerRepositoryImpl.java @@ -0,0 +1,16 @@ +package conference.repository; + +import conference.model.Speaker; + +import java.util.ArrayList; +import java.util.List; + +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/repository/SpeakerRepository.java b/Spring/src/main/java/conference/repository/SpeakerRepository.java new file mode 100644 index 0000000..8aca714 --- /dev/null +++ b/Spring/src/main/java/conference/repository/SpeakerRepository.java @@ -0,0 +1,9 @@ +package conference.repository; + +import conference.model.Speaker; + +import java.util.List; + +public interface SpeakerRepository { + List findAll(); +} diff --git a/Spring/src/main/java/conference/service/SpeakerService.java b/Spring/src/main/java/conference/service/SpeakerService.java new file mode 100644 index 0000000..7d7b4d1 --- /dev/null +++ b/Spring/src/main/java/conference/service/SpeakerService.java @@ -0,0 +1,9 @@ +package conference.service; + +import conference.model.Speaker; + +import java.util.List; + +public interface SpeakerService { + List findAll(); +} diff --git a/Spring/src/main/java/conference/service/SpeakerServiceImpl.java b/Spring/src/main/java/conference/service/SpeakerServiceImpl.java new file mode 100644 index 0000000..56c9f8f --- /dev/null +++ b/Spring/src/main/java/conference/service/SpeakerServiceImpl.java @@ -0,0 +1,24 @@ +package conference.service; + +import conference.model.Speaker; +import conference.repository.SpeakerRepository; + +import java.util.List; + +public class SpeakerServiceImpl implements SpeakerService { + + private SpeakerRepository speakerRepository; + + public SpeakerServiceImpl(SpeakerRepository speakerRepository) { + this.speakerRepository = speakerRepository; + } + + @Override + public List findAll() { + return speakerRepository.findAll(); + } + + public void setSpeakerRepository(SpeakerRepository speakerRepository) { + this.speakerRepository = speakerRepository; + } +}