
Introduction : β
Greeting πππ
Today we will continue about the concepts of Spring Framework concerning Spring Bean Scope that allows us to have more granular control of the bean instances creation and the annotation @AutoWiring allows the Spring container to automatically resolve dependencies between collaborating beans.
Spring Scope β
The scope of a bean defines the life cycle and visibility of that bean in the contexts in which it is used.
There are five scopes that spring provides to configure a bean :
Singleton:
Each time we ask for an istance of that bean from the application context , it's returning the same bean for us .
AppConfig.class:java@Configuration public class AppConfig { @Bean(name="speakerService") @Scope(value = BeanDefinition.SCOPE_SINGLETON) //by default public SpeakerService getSpeakerService(){ SpeakerServiceImpl service=new SpeakerServiceImpl(getSpeakerRepository()); return service; } @Bean(name="speakerRepository") public SpeakerRepository getSpeakerRepository(){ return new HibernateSpeakerRepositoryImpl(); } }Application.class:javapublic class Application { public static void main(String[] args) { ApplicationContext applicationContext=new AnnotationConfigApplicationContext(AppConfig.class);// when this line run ,it's going to create a basic registry with 2 beans (speakerService bean ,speakerRepository bean) SpeakerService service1= applicationContext.getBean("speakerService",SpeakerService.class); SpeakerService service2= applicationContext.getBean("speakerService",SpeakerService.class); System.out.println(service1); System.out.println(service1.findAll().get(0).getFirstName()); System.out.println(service2); } }Output:
As a result, we have the same memory address for both objects:
Service1,Service2.Singletonscope is the default scope of Spring.The difference between Singleton in java and in spring:

Prototype:
It gives us a unique bean per request or reference(
getbean()) and it is the opposite of singleton scope.AppConfig.class:java@Configuration public class AppConfig { @Bean(name="speakerService") @Scope(value = BeanDefinition.SCOPE_Prototype) public SpeakerService getSpeakerService(){ SpeakerServiceImpl service=new SpeakerServiceImpl(getSpeakerRepository()); return service; } @Bean(name="speakerRepository") public SpeakerRepository getSpeakerRepository(){ return new HibernateSpeakerRepositoryImpl(); } }Output:
As a result, The object
Service1have different memory address with the objectService2.Session:
Return a new bean per HTTP request.
Request:
Return a single bean per HTTP. for example when the user session is alive for 10 or 30 min.
Global:
Return a single bean per application. So once to access it, itβs alive for the duration of that application.
The three last scopes: Session,Request and Global will be introduced in the future blog when we talk about Spring MVC.
Autowiring β
The annotation Autowiring allows the Spring container to automatically resolve dependencies between collaborating beans by inspecting the beans that have been defined. And it is a great technique used to reduce the wiring up and configuration of code(convention-over-configuration).
Autowiring is a great feature that allows spring to get the reference objects by name or by type(Not to worry by the terms, we will explain in a while).
To autowiring using Java configuration we just simply add:
@ComponentScan({βcom.elmmalemβ})into configuration file.AppConfig.class:java@Configuration @ComponentScan({"com.elmaalem"}) public class AppConfig { }@Beanby Name or by Type(instance type ).AppConfig.class:java@Configuration public class AppConfig { @Bean(name="speakerService") public SpeakerService getSpeakerService(){ SpeakerServiceImpl service=new SpeakerServiceImpl(getSpeakerRepository()); return service; } @Bean(name="speakerRepository") public SpeakerRepository getSpeakerRepository(){ return new HibernateSpeakerRepositoryImpl(); } }Autowiringby Type cannot happen if there are two or more variables of the same datatype. i.e. IfSpeakerServiceImplclass contains two variables of typeSpeakerRepository,Autowiringby Type does not work.
Conclusion β
That was all for this blog, I hope you enjoy it and you understood two techniques that we have touched on. Therefore, In the next blog, we will explain spring configuration using java with stereotype annotations and advanced bean configuration.
Thank you for reading! If you enjoyed it, please upvote π it and donβt forget to share! ππ€
