Chapter 2, Inversion of Control in Spring

Q1. What are Inversion of Control (IoC) and Dependency Injection (DI)?

IoC is a more general concept, and DI is a concrete design pattern. In software engineering, IoC is a programming technique where the assembler object compels object coupling at runtime using static analysis. DI reduces the coupling between objects. DI is a design pattern on which the dependency of object is injected by the framework, rather than created by the object itself.

IoC makes your code more portable, more testable, and more manageable and keeps component configuration, dependencies, and life cycle events outside of the components.

Q2. What are the different types of Dependency Injection in Spring?

In the Spring Framework, DI is used to satisfy the dependencies between objects. It exits in two major types:

  • Constructor Injection: Constructor-based DI can be accomplished by invoking parameterized constructor. These constructor arguments will be injected during the instantiation of the instance.
  • Setter Injection: Setter-based DI is the preferred method of Dependency Injection in Spring that can be accomplished by calling setter methods on your bean after invoking a no-argument static factory method or no-argument constructor to instantiate this bean.

Q3. Explain autowiring in Spring. What are the different modes of autowiring.

A Spring container can use five modes of autowiring as follows:

  • no: By default, Spring bean autowiring is turned off which means that no autowiring is to be performed, and you should use explicit bean reference ref for wiring.
  • byName: This property name is used for this type of autowiring. If the bean property is same as other bean name, autowire it. The setter method is used for this type of autowiring to inject dependency.
  • byType: This data type is used for this type of autowiring. If the data type bean property is compatible with data type of other bean, autowire it. For this type, only one bean should be configured in configuration file else a fatal exception will be thrown.
  • constructor: This is similar to autowire byType, but here constructor is used for injecting dependency.
  • autodetect: Autowiring by autodetect in Spring is deprecated, and it first tries to autowire by constructor, and if it does not work, then autowire by type.

Q4. Explain different Spring bean scope.

The following list gives the Spring bean scope:

  • Singleton: Singleton in Spring represents in a particular Spring Container and there is only one instance of bean created in that container that is used across different references.
  • Prototype: This is a new bean created with every request or reference. For every getBean() call, Spring has to do initialization so instead of doing default initialization while a context is being created, it waits for getBean() call.
  • Request: A new bean is created per Servlet request. Spring will be aware of when a new request is happening because it ties well with Servlet APIs and depending on request, Spring creates a new bean.
  • Session: A new bean is created per session. As long as there is one user accessing in a single session, on each call to getBean() will return same instance of bean.
  • Global-session: This is applicable in portlet context. There will be a global session in an individual portlet session, and a bean can be tied with global session. Here, a new bean is created per global HTTP session.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset