A detailed Java Spring Framework injection collection of beans _java

Source: Internet
Author: User
Tags set set java spring framework

Use the Value property and the ref attribute of the <property> label in your bean configuration file to reference the object, in both cases you can process a single value to a bean, if you want to pass multivariate values, such as the Java collection type list, Set, Map and Properties. To handle this situation, Spring provides four types of configuration elements for the following collection:

You can use <list> or <set> to connect any implementation java.util.Collection or arrays.

There are two situations (a) a direct collection of values and (b) passing a reference to a bean as one of the elements of the collection.

Example:
we use the Eclipse IDE, and then follow these steps to create a spring application:

Here are the contents of the Javacollection.java file:

Package Com.yiibai;

Import java.util.*;
  public class Javacollection {List addresslist;
  Set AddressSet;
  Map Addressmap;

  Properties Addressprop;
  A setter method to set List public void Setaddresslist (list addresslist) {this.addresslist = AddressList;
  }//Prints and returns all the elements of the list.
   Public List getaddresslist () {System.out.println ("List Elements:" + AddressList);
  return AddressList;
  }//A setter method to set set public void Setaddressset (set addressset) {this.addressset = AddressSet;
  }//Prints and returns all the elements of the Set.
   Public set Getaddressset () {System.out.println ("Set Elements:" + addressset);
  return addressset;
  }//A setter method to set Map public void Setaddressmap (Map addressmap) {this.addressmap = Addressmap;
  }//Prints and returns all the elements of the MAP.
   Public Map Getaddressmap () {System.out.println ("map Elements:" + addressmap); Return AddressmaP }//A setter method to set property public void Setaddressprop (Properties addressprop) {This.addressprop = Addre
  Ssprop;
  }//Prints and returns the elements of the property.
   Public Properties Getaddressprop () {System.out.println ("Property Elements:" + addressprop);
  return addressprop;

 }
}

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");

   Javacollection jc= (javacollection) Context.getbean ("Javacollection");

   Jc.getaddresslist ();
   Jc.getaddressset ();
   Jc.getaddressmap ();
   Jc.getaddressprop ();
  }


The following are all types of configured collections in the configuration file Beans.xml file:

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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 javacollection--> &  Lt;bean id= "javacollection" class= "Com.yiibai.JavaCollection" > <!--results in a setaddresslist (java.util.List) Call--> <property name= "AddressList" > <list> <value>INDIA</value> <valu
   e>pakistan</value> <value>USA</value> <value>USA</value> </list> 
    </property> <!--results in a setaddressset (java.util.Set) call--> <property name= "AddressSet" > <set> <value>INDIA</value> <value>Pakistan</value> &LT;VALUE&GT;USA&L T;/value> &LT;VALUE&GT;USA&LT;/value> </set> </property> <!--results in a setaddressmap (JAVA.UTIL.MAP) Call--> &L T;property name= "Addressmap" > <map> <entry key= "1" value= "INDIA"/> <entry key= "2" value= "Pakistan"/> <entry key= "3" value= "USA"/> "<entry key=" 4 "value=" USA "/> </map> < /property> <!--results in a setaddressprop (java.util.Properties) call--> <property name= "Addressprop"
      > <props> <prop key= "One" >INDIA</prop> <prop key= "two" >Pakistan</prop> <prop key= "three" >USA</prop> <prop key= "Four" >USA</prop> </props> </prop

 Erty> </bean> </beans>

After creating the source code and the bean configuration file, let's run the application. If the application goes smoothly, this will print the following information:

List Elements: [INDIA, Pakistan, USA, USA]
Set Elements: [INDIA, Pakistan, USA]
Map Elements: {1=india, 2=pakistan , 3=usa, 4=usa} property
Elements: {two=pakistan, One=india, Three=usa, Four=usa}

To inject a bean reference:
The following bean definition will help you understand how to inject the bean's references as one of the elements of the collection. You can even mix references and values together, as shown in the following illustration:

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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 Definition to handle references And values--> <bean id= "..." class= "> <!--passing bean reference for Java.util.List--> <
      Property name= "AddressList" > <list> <ref bean= "Address1"/> <ref bean= "Address2"/> <value>Pakistan</value> </list> </property> <!--passing bean reference for Java. Util. Set--> <property name= "AddressSet" > <set> <ref bean= "Address1"/> <ref bean= "ad Dress2 "/> <value>Pakistan</value> </set> </property> <!--passing Bean Refe
   Rence for Java.util.Map--><property name= "Addressmap" > <map> <entry key= "one" value= "INDIA"/> <entry key = "Two"

  value-ref= "Address1"/> <entry key = "three" value-ref= "Address2"/> </map> </property>

 </bean> </beans>

Using the above bean definitions, you need to define such a way that they should be able to handle the reference as well as the setter method.

Values injected with null and empty strings
If you need to pass an empty string as a value, as follows:

<bean id= "..." class= "Examplebean" > <property name=
  "email" value= "/>
</bean>

The previous example is equivalent to Java code: Examplebean.setemail ("")

If you need to pass a null value, as follows:

<bean id= "..." class= "Examplebean" > <property name=
  "email" ><null/></property>
</bean>

The previous example is equivalent to Java code: Examplebean.setemail (NULL)

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.