IOC(Inversion of control)- It’s a concept where we invert the control of creating object from new keyword to configuration files, In spring this concept is achieved by DI(Dependency injection).
Dependency Injection(DI)- In dependency injection, rather instantiating and initialing objects, they are described in configuration files that how they should be configured and assembled.
IOC containers- They are used to configure and instantiate objects.
Types of IOC containers- there are 2 types.
Type 1- BeanFactory –
-Implementation of factory pattern that applies inversion of control to separate the configuration and dependencies from application.
-Implementation of BeanFactory,
-it is used to provide basic configuration to application by loading required beans from metadata.
-uses lazy instantiation of beans, means bean is instatiated when getBean() is called.
-no support for annotation based DI.
Resource resource = new ClassPathResource(“springConfigFile”);
BeanFactory beanFactory= new XmlBeanFactory(resource);
Type 2-ApplicationContext –
-Extends the Beanfactory
-provides more specific functionalities to apllication.
-uses eager instantion, instantiate all beans at startup.
-supports annatation based DI(@Autowired, @PreDestroy)
implements ApplicationContext interface, Beans loaded from classpth
implements ApplicationContext interface, Beans loaded using full path of file.
implements ApplicationContext interface, Beans loaded using web application context.
Types of Dependency Injection-
1-Constructor based DI- Data members and objects are configured using constructor.
2-Setter method based DI- Data members and objects are configured using setter method of pojo files.
3-Interface based DI- Not supported in spring.
Autowiring – Wiring is spring shows relationship bw beans, and we have constructor and setter method based DI to establish relationships bw beans.
But in spring we also have autowire attribute to provide automation to wiring.
Types of autowiring-
1- no– by default, no autowiring
2- byName – wiring based on instance name
3–byType – wiring based on datatype, there should be only one unique datatype of bean is declared.
4-constructor – It’s similar to byType in constructor argument
5-autoDetect- chooses between autowiring by constructor or bytype.