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:
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:
getBean()
call, Spring has to do initialization so instead of doing default initialization while a context is being created, it waits for getBean()
call.getBean()
will return same instance of bean.