The inheritance of beans and the injection of internal beans in the Java Spring Framework _java

Source: Internet
Author: User
Tags java spring framework

Bean's definition inheritance
The bean definition can contain a lot of configuration information, including the constructor parameters, property values, such as initialization methods, static factory method names, and other containers specific information.

The child bean definition inherits configuration data from the parent definition. A child's definition can overwrite some values, or add additional as needed.

The Spring bean defines inheritance regardless of the inheritance of the Java class, but the concept of inheritance is the same. You can define a parent bean defined as a template and other child beans can inherit the desired configuration from the parent bean.

When using xml-based configuration metadata, indicates that a child bean definition uses the parent bean specified by the current property as the value of this property.

For example:
Let's use the Eclipse IDE, and then follow these steps to create a spring application:

Here's what we've defined as "HelloWorld" beans that have two properties in Message1 and Message2 profile beans.xml. The next "Helloindia" Bean has been defined as "HelloWorld" by a child bean using the parent attribute. The child bean inherits the Message2 property and overrides the Message1 property and introduces one more attribute Message3.

<?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-3.0.xsd ">

 <bean id= "HelloWorld" class= "Com.yiibai.HelloWorld" >
  <property name= "Message1" value= "Hello World !" />
  <property name= "Message2" value= "Hello Second world!" />
 </bean>

 <bean id= "Helloindia" class= "Com.yiibai.HelloIndia"
  parent= "HelloWorld" >
  <property name= "Message1" value= "Hello india!" />
  <property name= "Message3" value= "Namaste india!" />
 </bean>

</beans>

Here are the contents of the Helloworld.java file:

Package Com.yiibai;

public class HelloWorld {
 private String message1;
 Private String Message2;

 public void SetMessage1 (String message) {
  this.message1 = message;
 }

 public void SetMessage2 (String message) {
  this.message2 = message;
 }

 public void GetMessage1 () {
  System.out.println ("World Message1:" + message1);
 }

 public void GetMessage2 () {
  System.out.println ("World Message2:" + message2);
 }


Here are the contents of the Helloindia.java file:

Package Com.yiibai;

public class Helloindia {
 private String message1;
 Private String Message2;
 Private String Message3;

 public void SetMessage1 (String message) {
  this.message1 = message;
 }

 public void SetMessage2 (String message) {
  this.message2 = message;
 }

 public void SetMessage3 (String message) {
  this.message3 = message;
 }

 public void GetMessage1 () {
  System.out.println ("India Message1:" + message1);
 }

 public void GetMessage2 () {
  System.out.println ("India Message2:" + message2);
 }

 public void GetMessage3 () {
  System.out.println ("India Message3:" + message3);
 }


The following are the contents of the Mainapp.java file:

Package Com.yiibai;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mainapp {public
 static void Main (string[] args) {
  ApplicationContext context = 
    new Classpathxml ApplicationContext ("Beans.xml");

  HelloWorld Obja = (HelloWorld) context.getbean ("HelloWorld");

  Obja.getmessage1 ();
  Obja.getmessage2 ();

  Helloindia OBJB = (helloindia) context.getbean ("Helloindia");
  Objb.getmessage1 ();
  Objb.getmessage2 ();
  Objb.getmessage3 ();
 }


Create the complete source code and Bean configuration file, let's run the application. If all goes well, this will print the following information:

World Message1:hello world!
World Message2:hello Second world!
India Message1:hello india!
India Message2:hello Second world!
India Message3:namaste india!

If you see here, we did not create "Helloindia" beans through message2 at the same time, but it passed because of the bean-defined inheritance.

Bean definition Template:
You can create a bean definition template that can be defined by other child beans in a way that doesn't take too much effort. In defining the bean definition template, you should not specify the class attribute, and you should specify the abstract attribute, as shown below, with the truth:

<?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-3.0.xsd ">

 <bean id= "Beanteamplate" abstract= "true" > <property name= "message1"
  value= "Hello world!" />
  <property name= "Message2" value= "Hello Second world!" />
  <property name= "Message3" value= "Namaste india!" />
 </bean>

 <bean id= "Helloindia" class= "Com.yiibai.HelloIndia"
  parent= "Beanteamplate" >
  <property name= "Message1" value= "Hello india!" />
  <property name= "Message3" value= "Namaste india!" />
 </bean>

</beans>

The parent bean cannot be instantiated itself because it is incomplete, and it is explicitly marked as abstract. When a definition is abstract this way, it is simply defined as a pure template bean, which acts as a parent definition of the child definition used.

Inject internal bean
as you know the Java inner class is defined within the scope of other classes, as well, the internal bean is a bean that is defined within the scope of the other bean. So the <property/> or <constructor-arg/> element <bean/> component is called the internal bean and it looks like this.

<?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-3.0.xsd ">

 <bean id= "Outerbean class=" ... "> <property name="
  target ">
   <bean id=" Innerbean "class=" ... "/>
  </property>
 </bean>

</beans>

For example:
We use the Eclipse IDE and then create a spring application,

Here are the contents of the Texteditor.java file:

Package Com.yiibai;

public class TextEditor {
 private spellchecker spellchecker;

 A setter method to inject the dependency.
 public void Setspellchecker (Spellchecker spellchecker) {
  System.out.println ("Inside setspellchecker.");
  This.spellchecker = spellchecker;
 }
 A getter method to return spellchecker public
 spellchecker Getspellchecker () {return
  spellchecker;
 } Public

 void SpellCheck () {
  spellchecker.checkspelling ();
 }
}

Here is another related class file Spellchecker.java content:

Package Com.yiibai;

public class Spellchecker {public
 spellchecker () {
  System.out.println ("Inside Spellchecker constructor.");
 }

 public void CheckSpelling () {
  System.out.println ("Inside checkspelling.");
 }
 


The following are the contents of the Mainapp.java file:

Package Com.yiibai;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mainapp {public
 static void Main (string[] args) {
  ApplicationContext context = 
    new Classpathxml ApplicationContext ("Beans.xml");

  TextEditor te = (texteditor) context.getbean ("TextEditor");

  Te.spellcheck ();
 }


The following are configuration file Beans.xml files that are configured to be based on setter injection, but use internal beans:

<?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-3.0.xsd ">

 <!--Definition for texteditor bean using inner bean-->
 <bean id= "TextEditor" Com.yiibai.TextEditor ">
  <property name=" Spellchecker ">
   <bean id=" Spellchecker "class=" Com.yiibai.SpellChecker "/>
  </property>
 </bean>

</beans>

Create the source code and bean configuration file to complete, let's run the application. If all goes well, this will print the following information:

Inside Spellchecker constructor.
Inside Setspellchecker.
Inside checkspelling.

Related Article

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.