One, bean definition inheritance
A bean definition can contain many configuration information, including constructor parameters, property values, and container-specific information, such as initialization methods, static factory method names, and so on.
the child bean definition inherits the configuration data from the parent definition. a child definition can overwrite certain values or add other values as needed.
Spring Bean definition Inheritance has nothing to do with Java class inheritance, but the concept of inheritance is the same. You can define a parent bean definition as a template, and other child beans can inherit the desired configuration from the parent bean.
when you use XML-based configuration metadata, you can use the Parent property to specify the child bean definition and to specify the parent bean as the value for this property.
Demo Example:
(1) Writing Helloworld.java
PackageCom.tutorialspoint; Public classHelloWorld {PrivateString Message1; PrivateString Message2; Public voidsetMessage1 (String message) { This. Message1 =message; } Public voidsetMessage2 (String message) { This. Message2 =message; } Public voidGetMessage1 () {System.out.println ("World Message1:" +message1); } Public voidGetMessage2 () {System.out.println ("World Message2:" +message2); }}
(2) Writing Helloindea.java
PackageCom.tutorialspoint; Public classHelloindia {PrivateString Message1; PrivateString Message2; PrivateString Message3; Public voidsetMessage1 (String message) { This. Message1 =message; } Public voidsetMessage2 (String message) { This. Message2 =message; } Public voidsetMessage3 (String message) { This. Message3 =message; } Public voidGetMessage1 () {System.out.println ("India Message1:" +message1); } Public voidGetMessage2 () {System.out.println ("India Message2:" +message2); } Public voidGetMessage3 () {System.out.println ("India Message3:" +message3); }}
(3) Writing Mainapp.java
PackageCom.tutorialspoint;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMainapp { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Beans.xml"); HelloWorld Obja= (HelloWorld) context.getbean ("HelloWorld"); Obja.getmessage1 (); Obja.getmessage2 (); Helloindia OBJB= (Helloindia) context.getbean ("Helloindia"); Objb.getmessage1 (); Objb.getmessage2 (); Objb.getmessage3 (); }}
(4) Beans.xml
<?xml Version = "1.0" encoding = "UTF-8"? ><beans xmlns = "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-4.0.xsd "><bean id = "HelloWorld"class= "Com.tutorialspoint.HelloWorld" > <property name = "Message1" value = "Hello world!" /> <property name = "Message2" value = "Hello Second world!" /> </bean> <bean id = "Helloindia"class= "Com.tutorialspoint.HelloIndia" parent = "HelloWorld" > <property name = "Message1" value = "Hello india!" /> <property name = "Message3" value = "Namaste india!" /> </bean> </beans>
(5) Run the Main method in Mainapp.java
Ii. Dependency Injection
each Java-based application has some objects that work together to present the work application that the end user sees. When writing complex Java applications, application classes should be as independent of other Java classes as possible to increase the likelihood of reusing these classes and test them independently of other classes when unit tests are in use. Dependency Injection (or cabling) helps to glue these classes together while maintaining their independence.
There are two common forms of dependency injection:
1.set injection (more commonly used)
2. Constructor injection
Set injection Example:
(1) Writing Texteditor.java
PackageCom.tutorialspoint; Public classTextEditor {Privatespellchecker spellchecker; //a setter method to inject the dependency. Public voidSetspellchecker (spellchecker spellchecker) {System.out.println ("Inside Setspellchecker." ); This. Spellchecker =spellchecker; } //A getter method to return Spellchecker Publicspellchecker Getspellchecker () {returnspellchecker; } Public voidspellcheck () {spellchecker.checkspelling (); }}
(2) Writing Spellchecker.java
Package Com.tutorialspoint; Public class spellchecker { public spellchecker () { System.out.println ("Inside spellchecker constructor. " ); } Public void checkspelling () { System.out.println ("Inside checkspelling.") ); }}
(3) Writing Beans.xml
<?xml Version = "1.0" encoding = "UTF-8"? ><beans xmlns = "Http://www.springframework.org/schema/beans" = "Http://www.w3.org/2001/XMLSchema-instance" = "http://www.springframework.org/schema/ Beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd "> for TextEditor Bean using inner bean-- class = "Com.tutorialspoint.TextEditor" > <property Name = "Spellchecker" > class = "Com.tutorialspoint.SpellChecker"/> </property> </bean> </beans>
(4) Writing Mainapp.java
package Com.tutorialspoint; import Org.springframework.context.ApplicationContext; import Org.springframework.context.support.ClassPathXmlApplicationContext; public class Mainapp { public static void main (string[] args) {applicationcontext context = New Classpathxmlapplicationcontext ("Beans.xml" = (texteditor) context.getbean ("TextEditor"
(5) Run the Main method in Mainapp.java
2. Inject the collection
Spring provides four sets of injection methods:
(1) Set; (2) List; (3) Map; (4) Props;
The demo example is as follows:
(1) Writing javacollection
PackageCom.tutorialspoint;ImportJava.util.*; Public classjavacollection {List addresslist; Set AddressSet; Map Addressmap; Properties Addressprop; //a setter method to set List Public voidsetaddresslist (List addresslist) { This. AddressList =AddressList; } //Prints and returns all the elements of the list. PublicList getaddresslist () {System.out.println ("List Elements:" +AddressList); returnAddressList; } //a setter method to set set Public voidSetaddressset (Set addressset) { This. AddressSet =AddressSet; } //Prints and returns all the elements of the Set. PublicSet Getaddressset () {System.out.println ("Set Elements:" +AddressSet); returnAddressSet; } //a setter method to set Map Public voidSetaddressmap (Map addressmap) { This. Addressmap =Addressmap; } //Prints and returns all the elements of the Map. PublicMap Getaddressmap () {System.out.println ("Map Elements:" +addressmap); returnAddressmap; } //a setter method to the Set property Public voidSetaddressprop (Properties addressprop) { This. Addressprop =Addressprop; } //prints and returns all the elements of the property. PublicProperties Getaddressprop () {System.out.println ("Property Elements:" +Addressprop); returnAddressprop; }}
(2) Writing Beans.xml
<?xml Version = "1.0" encoding = "UTF-8"? ><beans xmlns = "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-4.0.xsd "><!--Definition forJavacollection--<bean id = "Javacollection"class = "Com.tutorialspoint.JavaCollection" > <!--results in a setaddresslist (java.util.List)--- <property name = "AddressList" > <list> <value>INDIA</value> <val Ue>pakistan</value> <value>USA</value> <value>USA</value> & Lt;/list> </property> <!--results in a setaddressset (java.util.Set) call-to-<property n Ame = "AddressSet" > <set> <value>INDIA</value> <value>pakistan< ;/value> <value>USA</value> <value>USA</value> </set> </property> <!--results in a setaddressmap (JAVA.UTIL.MAP) Call--<property name = "Addressmap" > <map> <entry key = "1" value = "INDIA"/> <entry key = "2" value = "Pakis Tan "/> < entry key = "3" value = "USA"/> <entry key = "4" value = "USA"/> </map> </prope Rty> <!--results in a setaddressprop (java.util.Properties) call-to-<property name = "Address Prop "> <props> <prop key =" One ">INDIA</prop> <prop key =" One ">i ndia</prop> <prop key = "Both" >Pakistan</prop> <prop key = "three" >usa</pr op> <prop key = "Four" >USA</prop> </props> </property> </bean> </beans>
(3) Writing Mainapp.java
PackageCom.tutorialspoint;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMainapp { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Beans.xml"); Javacollection JC= (javacollection) context.getbean ("Javacollection"); Jc.getaddresslist (); Jc.getaddressset (); Jc.getaddressmap (); Jc.getaddressprop (); }}
(4) Run the Main method in Mainapp.java
The results are as follows:
Spring (v) Bean definition inheritance and dependency injection