1. Introduction
2.Spring constructor-based dependency Injection 3. Spring Dependency Injection based on setpoint function 4. Inject internal beans5.Spring Injection Set
1. Introduction
Dependency Injection
Each application-based Java has several objects that work together to present the applications that the end user sees as work. When writing a complex Java application, the application classes should be as independent of other Java classes as possible to increase the likelihood of these classes being reused, and when doing unit tests, the tests are independent of the other classes. Dependency injection (or sometimes called cabling) helps to glue these classes together while keeping them separate.
Suppose you have an application that contains a text editor component, and you want to provide a spelling check. The standard code looks like this:
Public class texteditor { private spellchecker spellchecker; Public TextEditor () { new spellchecker (); }}
What we're doing here is creating a dependency between TextEditor and Spellchecker. In the case of inversion of control, we would instead do something like this:
Public class texteditor { private spellchecker spellchecker; Public TextEditor (spellchecker spellchecker) { this. spellchecker = spellchecker; }}
Here, TextEditor should not worry about the realization of spellchecker. The spellchecker will be implemented independently and will be provided to TextEditor when the TextEditor is instantiated, and the entire process is controlled by the Spring framework.
Here we have removed full control from TextEditor and saved it elsewhere (that is, the XML configuration file), and the dependency (that is, the Spellchecker class) is injected into the TextEditor class through the class constructor . Therefore, the control flow is "inverted" through dependency injection (DI) because you have effectively delegated dependencies to some external systems.
The second method of dependency injection is through the setter method of the TextEditor class, and we will create an spellchecker instance that will be used to invoke the setter method to initialize the properties of the TextEditor.
Therefore, DI has two main variants and the following two sub-chapters will cover them with examples:
Serial Number |
Dependency Injection Type & Description |
1 |
constructor-based Dependency Injection When a container invokes a constructor class with multiple arguments, the implementation is based on the constructor's DI, each representing a dependency in another class. |
2 |
setter-based Dependency Injection The DI based on the setter method is implemented by invoking the setter method of the beans after instantiating the bean by invoking a parameterless constructor or a parameterless static factory method. |
You can mix both methods, based on constructors and DI based on setter methods, but using constructors with mandatory dependencies and Sette R with optional dependencies is a good practice.
The code is a washing machine with DI principle, and the decoupling effect is more obvious when the object and their dependencies are provided. The object does not look for its dependencies, nor does it know the location or class of the dependencies, and all of this is controlled by the Spring framework.
2.Spring constructor-based dependency injection (no need to have get and set methods)
When a container invokes a class constructor with a set of parameters, the constructor-based DI is completed, where each parameter represents a dependency on another class
<!--If you explicitly specify the type of a constructor parameter by using the Type property, the container can also use a type that matches a simple type. -<BeanID= "Examplebean"class= "examples." Examplebean "> <Constructor-argtype= "int"value= "2001"/> <Constructor-argtype= "Java.lang.String"value= "Zara"/></Bean><!--Finally, and also the best way to pass the constructor arguments, use the Index property to explicitly specify the index of the constructor parameter. The following is an example based on index 0 -<BeanID= "Examplebean"class= "examples." Examplebean "> <Constructor-argIndex= "0"value= "2001"/> <Constructor-argIndex= "1"value= "Zara"/> </Bean><!--if you want to pass a reference to an object, you need to use the ref attribute of the label, if you want to pass the value directly, then you should use the Value property as shown above. -<Beans> <BeanID= "Foo"class= "X.y.foo"> <Constructor-argref= "Bar"/> <Constructor-argref= "Baz"/> </Bean> <BeanID= "Bar"class= "X.y.bar"/> <BeanID= "Baz"class= "X.y.baz"/></Beans>
3.Spring Dependency Injection based on set-valued functions
when a container invokes a parameterless constructor or an argument-free static factory method to initialize your bean, the set-valued function is called by the container on your bean, and the DI is completed based on the value function.
<!--To implement an XML configuration using p-namespace: -<BeanID= "John-classic"class= "Com.example.Person"> < Propertyname= "Name"value= "John Doe"/> < Propertyname= "spouse"ref= "Jane"/> </Bean> <Beanname= "Jane"class= "Com.example.Person"> < Propertyname= "Name"value= "John Doe"/> </Bean><!--value can be drawn separately to write -<BeanID= "Bird"class= "Com.my.entity.Bird"Scope= "Singleton"Init-method= "Initbean"Destroy-method= "Destroy">< Propertyname= "Bridname"><value>Joe</value></ Property>< Propertyname= "Color"value= "Yellow"/></Bean><!--the XML configuration file above can be overridden in a more concise way using P-namespace -<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd "> <BeanID= "John-classic"class= "Com.example.Person"P:name= "John Doe"P:spouse-ref= "Jane"/> </Bean> <Beanname= "Jane"class= "Com.example.Person"P:name= "John Doe"/> </Bean></Beans>
Here, you should not distinguish between specifying the original value and the object reference with the P-namespace. The -ref section shows that this is not a direct value, but a reference to another bean.
4. Inject internal beansInject internal Beans
As you know, the Java inner class is defined within the scope of other classes, and, similarly,inner beans is a bean defined within the scope of other beans. Elements inside or within an element are therefore called internal beans, as shown below.
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd "> <BeanID= "Outerbean"class="..."> < Propertyname= "Target"> <BeanID= "Innerbean"class="..."/> </ Property> </Bean></Beans>
<!--Definition for TextEditor bean using inner bean - <BeanID= "TextEditor"class= "Com.tutorialspoint.TextEditor"> < Propertyname= "Spellchecker"> <BeanID= "Spellchecker"class= "Com.tutorialspoint.SpellChecker"/> </ Property> </Bean>
5.Spring Injection Set
7.spring--Dependency Injection