Skip to main content

Dependency Injection

What is Dependency Injection?
=======================================
-> Dependency Injection (DI) is a design pattern that removes the dependency from the programming code so that it can be easy to manage and test the application.
-> Dependency Injection makes our programming code loosely coupled.
-> In such case we provide the information from the external source such as XML file.
-> It makes our code loosely coupled and easier for testing.
-> DI is all about objects and their dependencies.
-> In Spring frameowork, Dependency Injection (DI) design pattern is used to define the object dependencies between each other.
-> It is a process where objects are defining their dependencies and other objects work with them either constructor arguments or properties .
-> There are 2 widely used types: 1. Setter Injection, and 2. Constructor Injection
-> If we configure both setter injection and constructor injection on bean property the values given by setter injection will remain as final values of injection
     because setter(-) method executes after constructor executes and overrides the values injected by the constructor execution or injection.


-> When we go for Setter Injection or when we go for Constructor Injection?
====================================
-> At the time of creating our target class object, the dependent objects are injected or instantiated through Constructor Injection.
-> Whereas in Setter Injection dependent objects are not injected or instantiated while creating the target class object;
     those will be injected after the target class has been instantiated by calling the setter on the target object.
-> If bean class contains only one property or if all the property of bean class should participate in dependency injection then
     we should go for constructor injection because constructor injection is very faster than setter injection.
-> If bean class contains more than one property and there is no need of making all properties (means optional to inject) are
     participating in dependency injection, then we should go for setter injection. So it is bit delayed injection.
-> Constructor Injection doesn’t support cyclic dependency.
-> Whereas Setter injection supports cyclic or circular dependency injection.
-> To perform constructor injection on n properties in all angles, n! Constructors are required.
-> Whereas to perform setter injection on n properties in all angles “n-setter” methods are required.
 -> To perform constructor injection we will use <constructor-arg>.
-> Whereas to perform setter injection we will use <property> tag.


-> Spring supports dependency injection on the following properties:
    a. Simple properties (primitive data types, String)
    b. Reference or Object type properties
    c. Collection properties (Array, List, Set, Map, Properties, and etc...)

Comments

Popular posts from this blog

Mockito interview Questions

1.       Question 1. What Is Mockito? Answer : Mockito allows creation of mock object for the purpose of Test Driven Development and Behavior Driven development. Unlike creating actual object, Mockito allows creation of fake object (external dependencies) which allows it to give consistent results to a given invocation. 2.       Question 2. Why Do We Need Mockito? What Are The Advantages? Answer : Mockito differentiates itself from the other testing framework by removing the expectation beforehand. So, by doing this, it reduces the coupling. Most of the testing framework works on the "expect-run-verify". Mockito allows it to make it "run-verify" framework. Mockito also provides annotation which allows to reduce the boilerplate code. 3.       Question 3. Can You Explain A Mockito Framework? Answer : In Mockito, you always check a particular class. The dependency in that class is injected using m...

REST Methods

GET ============================================= HTTP GET method is used to **read** (or retrieve) a representation of a resource. According to the design of the HTTP specification, GET requests are used only to read data and not change it. Therefore, when used this way, they are considered safe. That is, they can be called without risk of data modification or corruption. Means calling it once has the same effect as calling it 10 times. Additionally, GET is idempotent which means that making multiple identical requests ends up having the same result as a single request. Donot expose unsafe operations via GET. It should never modify any resources on the server. Example: ------------    GET http://www.example.com/customers/12345/orders POST =================================== -> The POST verb is most-often utilized to **create** new resources. In particular, it's used to create subordinate resources. -> That is, subordinate to some other (e.g. parent) reso...