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 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; } 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(); + } +}