Sunday 18 June 2023

SPRING INTERVIEW QUESTIONS

1)What is Inversion of Control?

Inversion of control is a design principle in which control of object is transferred to framework. It means that it is  the framework that would take control of the flow of the program and makes calls to our custom code. Framework has abstraction around how it does that along with additional behaviour.

In IOC if we want to add our own behaviour we need to extend the classes of the framework or plugin our own classes.

                                                         OR

Inversion of Control is a design principle (many people called it pattern) which is used to invert different kind of controls in object oriented design to achieve loose coupling. Here,  control refers to any additional capabilities that a class has other than its main responsibility. This control could be control over flow of program, control over dependent object creation and binding etc.


Advantages of IOC:

Helps in creating loosely coupled classes which are easy to test , maintain and extend . It provides loosely coupling as all other task/controls are delegated to someone else rather than an individual class. For example IOC container is responsible for creating object in case of spring bean.


Various controls inverted by IOC

1)control over the flow of program

2)control over dependent object creation


Different design pattern which follow IOC

1)Factory Design pattern

2)Abstract Factory

3)Service Locator

4)Dependency Injection

5)Template Method.

 

2)What is Dependency Injection?

Dependency Injection is a design pattern to implement IOC where the control which is inverted is the way dependent objects will be created. Dependent objects wont be created by class but that will be others responsibility to do so.

 Object dependency creation in traditional programming.

public class Store {

    private Item item;

 

    public Store() {

        item = new ItemImpl1();    

    }

}


Using DI we can add dependent object without writing its implementation.


public class Store {

    private Item item;

    public Store(Item item) {

        this.item = item;

    }

}


3)Spring IOC container:

IOC container is the characteristic of the framework that implements IOC.

Responsibilities of IOC container

1)In spring interface ApplicationContext is the IOC container which is responsible for instantiating, configuring , assembling objects known as beans .

In oder to assemble beans, container uses configuration metadata which is in the form of xml or annotation.

2)It also managed the entire lifecycle of the objects from creation till removal.


Different Implementation of ApplicationContext:

1)ClassPathXMLApplicationContext for standalone application

2)FileSystemXMLApplicationContext for standalone application

3)WebApplicationContext for web application


How to initialize/instantiate  container manually:

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");


4)Different ways to do dependency injection

    4.1)Constructors

        @Configuration // It tells that this class is source to various beans . We can have other configuration                                                   classes as well.


         class appconfig{

          @Bean

            public Item item1(){ 

            return new ItemImpl();                 

            }


          @Bean

            public Store  store(){ 

            return new Store(item1());         //Constructor DI         

            }

        }

      

    4.2)Setters

        @Configuration // It tells that this class is source to various beans . We can have other configuration                                                   classes as well.


         class appconfig{

          @Bean

            public Item item1(){ 

            return new ItemImpl();                 

            }


          @Bean

            public Store  store(){ 

             Store store= new Store();        

              store.setItem(item1());   ////Setter  DI 

           

            }

        }

    4.3)Field-Based :

             In order to inject field dependency we can use @Autowired annotation .


            public class Store {

                    @Autowired

                    private Item item; 

            }

While constructing the Store object if there is no constructor and setter method to inject item object then container uses Reflection to inject item object into store bean.

Field based dependency Injection is not recommened why?

1)becoz it uses Reflection to inject dependent object which is costly as compared to constructor or setter injection.

2)It so simple to add multiple objects and doing so violate Single responsibility of class as class is doing additional task may be.













No comments:

Post a Comment