詳解Java的Spring框架中bean的注入聚集。本站提示廣大學習愛好者:(詳解Java的Spring框架中bean的注入聚集)文章只能為提供參考,不一定能成為您想要的結果。以下是詳解Java的Spring框架中bean的注入聚集正文
應用value屬性和應用<property>標簽的ref屬性在你的bean設置裝備擺設文件中的對象援用,這兩種情形下可以處置單值到一個bean,假如你想經由過程多元值,如Java Collection類型List, Set, Map 及 Properties。要處置這類情形,Spring供給了四品種型的以下聚集的設置裝備擺設元素:
可使用<list> 或<set> 來銜接任何完成java.util.Collection或數組。
會碰到兩種情形(a)將搜集的直接的值及(b)傳遞一個bean的援用作為聚集的元素之一。
例子:
我們應用Eclipse IDE,然後依照上面的步調來創立一個Spring運用法式:
這裡是JavaCollection.java文件的內容:
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 = addressProp; } // prints and returns all the elements of the Property. public Properties getAddressProp() { System.out.println("Property Elements :" + addressProp); return addressProp; } }
以下是MainApp.java文件的內容:
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 ClassPathXmlApplicationContext("Beans.xml"); JavaCollection jc=(JavaCollection)context.getBean("javaCollection"); jc.getAddressList(); jc.getAddressSet(); jc.getAddressMap(); jc.getAddressProp(); } }
以下是設置裝備擺設文件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-3.0.xsd"> <!-- Definition for javaCollection --> <bean id="javaCollection" class="com.yiibai.JavaCollection"> <!-- results in a setAddressList(java.util.List) call --> <property name="addressList"> <list> <value>INDIA</value> <value>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> <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="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> </property> </bean> </beans>
創立源代碼和bean設置裝備擺設文件完成後,讓我們運轉運用法式。假如運用法式一切順遂,這將打印以下信息:
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}
注入Bean援用:
上面bean界說將贊助您懂得若何注入bean的援用作為聚集的元素之一。乃至可以混雜援用和值都在一路,以下圖所示:
<?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 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="address2"/> <value>Pakistan</value> </set> </property> <!-- Passing bean reference 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>
應用下面的bean界說,須要界說如許一種方法,他們應當可以或許處置的參考,和setter辦法。
注入null和空字符串的值
假如須要傳遞一個空字符串作為值,以下所示:
<bean id="..." class="exampleBean"> <property name="email" value=""/> </bean>
後面的例子同等於Java代碼: exampleBean.setEmail("")
假如須要傳遞一個null值,以下所示:
<bean id="..." class="exampleBean"> <property name="email"><null/></property> </bean>
後面的例子同等於Java代碼:exampleBean.setEmail(null)