<bean id="" class=""> <property name="屬性" value="35"/><property name="name" value="jack"/></bean> 2.2引入其他Bean<bean> <property name="" ref="其他bean的id"/></bean> 還可以注入內(nèi)部Bean。內(nèi)部Bean就是定義在其他Bean內(nèi)部的Bean。<bean> <property name="'> <bean class=""/> </property></bean><bean> <constructpr-arg > <bean class=""/> </constructor-arg></bean> 內(nèi)部Bean沒有id屬性,因?yàn)閕d屬性沒有必要,不會(huì)通過id來引用內(nèi)部Bean,這也突出了內(nèi)部Bean的最大缺點(diǎn):不能復(fù)用。內(nèi)部Bean僅適用于一次注入,而且不能被其他Bean引用。2.3使用命名空間p來裝配Bean的屬性<bean id="" class="" p:name="" p:age="" p:A-ref=""/>p:xxx來指定屬性。-ref標(biāo)識(shí)表示裝配的是引用而不是值。和<property>是等價(jià)的。2.4裝配集合 1.<list>:裝配list類型的值,元素允許重復(fù)。 2.<set>:裝配set類型的值,元素不允許重復(fù) 3.<map>:裝配map類型的值,名稱和值可以是任意類型。 4.<props>:裝配properties類型的值:名稱和值都是string類型。2.4.1 list和set<bean> <property name=""> <list> <ref bean=""/> <ref bean=""/> <ref bean=""/> </list> </property> <property name=""> <set> <value></value> <value></value> </set> </property></bean>list和set都可以用來裝配類型為Collection的任意實(shí)現(xiàn)或數(shù)組的屬性。意思就是如果屬性為L(zhǎng)ist list,可以用set來裝配。2.4.2裝配集合<property name=""> <map> <entry key="" value=""/> <entry key="" value-ref=""/> <entry key-ref="" value=""/> <entry key-ref="" value-ref=""/> </map> </property>key:指定map中entry中的鍵value:指定map中entry中的值key-ref:指定map中entry的鍵為上下文中其他Bean的引用。value-ref:指定map中entry的值為上下文中其他Bean的引用。<property name="">//鍵值都為string時(shí)使用,也可以使用map <props> <prop key=""></prop> <prop key=""></prop> <prop key=""></prop> </props> </property><property>:用來給Bean的屬性注入值或者引用<props>:用來定義一個(gè)properties類型的集合值<prop>:用來定義一個(gè)<props>中的成員。2.5裝配空值 spring可以把屬性設(shè)置為null值。<property name="" ><null/></property> 可能某個(gè)屬性要設(shè)置為null值,或者覆蓋自動(dòng)裝配的值就可以用這種方式。3.使用表達(dá)式裝配
上面講的方式是靜態(tài)的。要使用動(dòng)態(tài)的方式可以使用Spring表達(dá)式語言(SpEL),它通過運(yùn)行期的表達(dá)式將值或引用傳遞到Bean的屬性或構(gòu)造器中。字面值:<bean> <property name="" value="#{23}"/> <property name="" value="name is #{23}"/> <property name="" value="#{1e4}"/>//10000.0<property name="" value="#{'jack'}"/>//string類型的值</bean>引用Bean、properties和方法:SpEL可以通過id來引用其他的Bean<property name="" value="#{abc}"/>//引用Bean<property name="" value="#{abc.name}"/>//引用Bean中的屬性<property name="" value="#{abc.getA()}"/>//引用Bean中方法返回的值<property name="" value="#{abc.getA().toUpperCase()}"/>//引用Bean中方法返回的值,大寫,不能返回null,會(huì)報(bào)空指針異常<property name="" value="#{abc.getA()?.toUpperCase()}"/>//引用Bean中方法返回的值,大寫,?左邊為null,什么都不做,不是null執(zhí)行?后面的操作操作類:T{}會(huì)調(diào)用類作用域的方法和常量。可以訪問類的靜態(tài)方法和常量<property name="" value="#{T{java.lang.Math}.PI}"/><property name="" value="#{T{java.lang.Math}.random()}"/>數(shù)值運(yùn)算:<property name="" value="#{T{java.lang.Math}.PI*100}"/><property name="" value="#{abc.age + 10}"/><property name="" value="#{abc.age +' ' +abc.name}"/>比較值:<property name="" value="#{abc.age == 18}"/><property name="" value="#{abc.age <= 18}"/>正則表達(dá)式:匹配返回true,否則false<property name="" value="#{abc.email matches '[xxxxx]'}"/>操作集合: 使用<util:list>配置一個(gè)集合。<util:list id="aaa"> <bean class="" p:xxx="" p:xxx-ref=""/></util:list>訪問集合元素:<property name="" value="#{aaa[2]}"/><util-properties id="bbb" location="classpath:bbb.properties" />查詢集合成員:<property name="" value="#{aaa.?[age ==18]}"/>.?返回的是[]中為true的集合,.^[]和.$[]返回的是第一和最后匹配項(xiàng)。不要多過使用這些表達(dá)式
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注