Demonstrate Spring

master
sgoudham 4 years ago
parent 59de2e518b
commit d0e17b03bc
No known key found for this signature in database
GPG Key ID: EF51A29A50FB754C

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.pluralsight</groupId>
<artifactId>Spring</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>14</source>
<target>14</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
</dependencies>
</project>

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

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

@ -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;
}
}

@ -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<Speaker> findAll() {
return new ArrayList<>() {{
add(new Speaker("Goudham", "Suresh"));
}};
}
}

@ -0,0 +1,9 @@
package conference.repository;
import conference.model.Speaker;
import java.util.List;
public interface SpeakerRepository {
List<Speaker> findAll();
}

@ -0,0 +1,9 @@
package conference.service;
import conference.model.Speaker;
import java.util.List;
public interface SpeakerService {
List<Speaker> findAll();
}

@ -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<Speaker> findAll() {
return speakerRepository.findAll();
}
public void setSpeakerRepository(SpeakerRepository speakerRepository) {
this.speakerRepository = speakerRepository;
}
}
Loading…
Cancel
Save