Commit 7f21e077 authored by dbhuller's avatar dbhuller

explored singleton and prototype scope for beans

parent 4f4f608e
......@@ -2,13 +2,17 @@ import com.example.repository.HibernateSpeakerRepositoryImpl;
import com.example.repository.SpeakerRepository;
import com.example.service.SpeakerService;
import com.example.service.SpeakerServiceImpl;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
public class AppConfig {
@Bean(name = "speakerService")
@Scope(value = BeanDefinition.SCOPE_SINGLETON)
// @Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
public SpeakerService getSpeakerService() {
// Using Constructor Injection
SpeakerServiceImpl service = new SpeakerServiceImpl(getSpeakerRespository());
......
......@@ -10,7 +10,16 @@ public class Application {
ApplicationContext appContext = new AnnotationConfigApplicationContext(AppConfig.class);
// SpeakerService service = new SpeakerServiceImpl();
SpeakerService service = appContext.getBean("speakerService", SpeakerService.class);
System.out.println(service);
System.out.println(service.findALl().get(0).getFirstName());
// Returns same bean as service --> Singleton
// With scope Prototype, returns two different instances of each bean --> Prototype
SpeakerService service2 = appContext.getBean("speakerService", SpeakerService.class);
System.out.println(service2);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment