[Translate]11-spring bean-defined inheritance

Source: Internet
Author: User
Tags tutorialspoint

The bean definition in spring contains a lot of information, such as constructor parameters, property-specified dependencies, initialization methods, factory classes, and factory methods.

If each bean in the spring container repeatedly declares these properties, it is very annoying and very inefficient and error-prone. Fortunately, the spring bean definition can be

to inherit.

A child's bean definition can inherit all the attributes from a parent bean definition, and the child Bean's definition can override its inherited

The property defined by the parent bean.

You can use the Parent property in the definition of a bean to specify the bean definition that it needs to inherit. Let's look at an example:

1. Create a new package Com.tutorialspoint.bean_inherit and create a new HelloWorld and Helloindia class in the package. The contents are as follows:

Helloworld.java as follows:

 PackageCom.tutorialspoint.bean_inherit; 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); }}

Helloindia.java as follows:

 PackageCom.tutorialspoint.bean_inherit; 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); }}

2. In the SRC directory, create a new bean_inherits.xml configuration file with the following content:

<?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.tutorialspoint.bean_inherit." HelloWorld ">      < Propertyname= "Message1"value= "Hello world!"/>      < Propertyname= "Message2"value= "Hello Second world!"/>   </Bean>   <!--to specify the definition of the bean that needs to be inherited through the parent property -   <!--overrides the parent's Message1 property -   <!--inherits the Message2 property of the parent -   <!--newly added Helloindia's Message3 property -   <BeanID= "Helloindia"class= "Com.tutorialspoint.bean_inherit." Helloindia "Parent= "HelloWorld">      < Propertyname= "Message1"value= "Hello india!"/>      < Propertyname= "Message3"value= "Namaste india!"/>   </Bean></Beans>

3. Create a new Mainapp.java class in package Com.tutorialspoint.bean_inherit with the following contents:

 PackageCom.tutorialspoint.bean_inherit;ImportOrg.springframework.context.support.AbstractApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMainapp { Public Static voidMain (string[] args) {Abstractapplicationcontext context=NewClasspathxmlapplicationcontext ("Bean_inherits.xml"); HelloWorld Obja= (HelloWorld) context.getbean ("HelloWorld");        Obja.getmessage1 ();        Obja.getmessage2 (); Helloindia OBJB= (Helloindia) context.getbean ("Helloindia");        Objb.getmessage1 ();        Objb.getmessage2 ();                Objb.getmessage3 ();    Context.registershutdownhook (); }}

4. Run the code and check the results:

Bean-defined templates

If we only need to use the Helloindia class, it is cumbersome to define the HelloWorld class simply for inheritance. Fortunately spring supports bean-defined templates

The template for the bean definition is to add the <bean/> element to the abstract= "true" property without having to specify the class attribute. Other beans directly from the bean template

Underwriting We have modified the configuration file Bean_inherits.xml of the above code to read as follows:

<?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= "Beanteamplate"Abstract= "true">      < Propertyname= "Message1"value= "Hello world!"/>      < Propertyname= "Message2"value= "Hello Second world!"/>      < Propertyname= "Message3"value= "Namaste india!"/>    </Bean>   <!--to specify the template for the bean definition that needs to be inherited through the parent property -   <!--overrides the parent's Message1 property -   <!--inherits the Message2 property of the parent -   <!--inherits the Message3 property of the parent -   <BeanID= "Helloindia"class= "Com.tutorialspoint.bean_inherit." Helloindia "Parent= "Beanteamplate">      < Propertyname= "Message1"value= "Hello india!"/>   </Bean></Beans>

and change the Mainapp.java to the following content:

 PackageCom.tutorialspoint.bean_inherit;ImportOrg.springframework.context.support.AbstractApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMainapp { Public Static voidMain (string[] args) {Abstractapplicationcontext context=NewClasspathxmlapplicationcontext ("Bean_inherits.xml"); Helloindia OBJB= (Helloindia) context.getbean ("Helloindia");        Objb.getmessage1 ();        Objb.getmessage2 ();                Objb.getmessage3 ();    Context.registershutdownhook (); }}

Continue running the Mainapp.java class and check the results to see that it is working correctly:

[Translate]11-spring bean-defined inheritance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.