Bean definition Inheritance
A bean definition can contain a lot of configuration information, including parameters for constructors, property values, container specifics such as initialization methods, static factory method names, and so on. The definition of the child bean inherits the configuration data of the vice definition. A child definition can override some values as needed, or add other values.
The inheritance of the Spring bean definition is unrelated to the inheritance of the Java class, but the concept of inheritance is the same. You can define a parent bean's definition as a template and other child beans to inherit the desired configuration from the parent bean.
When you use XML-based configuration metadata, by using the Parent property, you specify the parent bean as the value of the property to indicate the self-bean definition.
Bean.xml: In this configuration file we define a "HelloWorld" bean with two attribute message1,message2, and then use the Parent property to define the "Helloindia" Bean as the child of "HelloWorld". This word bean inherits the properties of Message2, overrides the properties of Message1, and introduces an attribute Message3.
<?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= "HelloWorld"class= "Com.tuorialsponit.HelloWorld"> < Propertyname= "Message1"value= "Hello World"/> < Propertyname= "Message2"value= "Hello Second World"></ Property> </Bean> <BeanID= "Helloindia"class= "Com.tuorialsponit.HelloIndia"Parent= "HelloWorld"> < Propertyname= "Message1"value= "Hello India"></ Property> < Propertyname= "Message3"value= "Namaste India"></ Property> </Bean> </Beans>
Content of Helloworld.java:
PackageCom.tuorialsponit; Public classHelloWorld {PrivateString Message1; PrivateString Message2; PublicString GetMessage1 () {returnMessage1; } Public voidsetMessage1 (String message1) { This. Message1 =Message1; } PublicString GetMessage2 () {returnMessage2; } Public voidsetMessage2 (String message2) { This. Message2 =Message2; } }
Helloindia.java:
PackageCom.tuorialsponit; Public classHelloindia {PrivateString Message1; PrivateString Message2; PrivateString Message3; PublicString GetMessage1 () {returnMessage1; } Public voidsetMessage1 (String message1) { This. Message1 =Message1; } PublicString GetMessage2 () {returnMessage2; } Public voidsetMessage2 (String message2) { This. Message2 =Message2; } PublicString GetMessage3 () {returnMessage3; } Public voidSetMessage3 (String message3) { This. Message3 =Message3; }}
Mainapp.java content:
PackageCom.tuorialsponit;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMainapp { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Beans.xml"); HelloWorld obj1= (HelloWorld) context.getbean ("HelloWorld"); System.out.println (Obj1.getmessage1 ()); System.out.println (Obj1.getmessage2 ()); System.out.println ("-----------------------"); Helloindia Obj2= (Helloindia) context.getbean ("Helloindia"); System.out.println (Obj2.getmessage1 ()); System.out.println (Obj2.getmessage2 ()); System.out.println (Obj2.getmessage3 ());//String message = Obj.getmessage ();//System.out.println (message); }}
Execution Result:
Here you can observe that we did not pass the message2 while creating the "Helloindia" bean, but because of the inheritance of the bean definition, it passed the message2.
Bean definition Template
<?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= "Beantemplate"Abstract= "true"> < Propertyname= "Message1"value= "Hello World"/> < Propertyname= "Message2"value= "Hello dddddddddsecond World"></ Property> < Propertyname= "Message3"value= "Hello world1"/> </Bean> <BeanID= "HelloWorld"class= "Com.tuorialsponit.HelloWorld"> < Propertyname= "Message1"value= "Hello World"/> < Propertyname= "Message2"value= "Hello Second World"></ Property> </Bean> <BeanID= "Helloindia"class= "Com.tuorialsponit.HelloIndia"Parent= "Beantemplate"> < Propertyname= "Message1"value= "Hello India"></ Property> < Propertyname= "Message3"value= "Namaste India"></ Property> </Bean> </Beans>
The parent bean (abstract) itself cannot be instantiated because it is incomplete, and it is also explicitly marked as abstract. When a definition is abstract, it is used only as a purely template bean definition to act as a parent definition of a child definition.
Springbean Defining inheritance