Hammy 4 years ago
commit f4d6e4d23d

@ -28,6 +28,11 @@
<artifactId>spring-context</artifactId> <artifactId>spring-context</artifactId>
<version>5.2.0.RELEASE</version> <version>5.2.0.RELEASE</version>
</dependency> </dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -3,6 +3,7 @@ package conference.service;
import conference.model.Speaker; import conference.model.Speaker;
import conference.repository.SpeakerRepository; import conference.repository.SpeakerRepository;
import javax.annotation.PostConstruct;
import java.util.List; import java.util.List;
public class SpeakerServiceImpl implements SpeakerService { public class SpeakerServiceImpl implements SpeakerService {
@ -13,6 +14,11 @@ public class SpeakerServiceImpl implements SpeakerService {
this.speakerRepository = speakerRepository; this.speakerRepository = speakerRepository;
} }
@PostConstruct
private void initialise() {
System.out.println("Initialisation After Constructors");
}
public void setSpeakerRepository(SpeakerRepository speakerRepository) { public void setSpeakerRepository(SpeakerRepository speakerRepository) {
this.speakerRepository = speakerRepository; this.speakerRepository = speakerRepository;
} }

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

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

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

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

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

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

@ -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<Speaker> findAll() {
return speakerRepository.findAll();
}
}
Loading…
Cancel
Save