Demonstrate Bean Autowiring
parent
196ca845c8
commit
e94d701af3
@ -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…
Reference in New Issue