spring學習筆記(二)

注入參數詳解

1.       直接注入值

<bean id=」car」 class=」com.smart.attr.Car」> java

           <property name=」speed」> spring

                    <value>2000</value> spa

           </property> xml

</bean> 對象

也能夠將value做爲property屬性使用,當value值包涵xml標籤或其餘可能引發xml異常的符號時,應該使用<![CDATE[String]]>,把其中內容當字符串解析 字符串

2.       應用其餘Bean

<bean id=」person」 class=」com.smart.person」> it

         <property name=」car」> io

                    <ref bean=」car」/> class

        </property> 配置

</bean>

<bean id=」car」 class=」com.smart.attr.Car」/>

<ref >屬性有bean/local/parent

3.       集合類型屬性

重要包括ListSetMapProperties

(1)       List

<bean id=」person」 class=」com.smart.person」>

             <list>

                       <value>讀書</value>

                       <value>看報</value>

                       <value>游泳</value>

             </list>

</bean>

能夠直接注入字符串,也能夠經過<ref>注入其餘BeanSet相似

(2)       Map

<bean id=」person」 class=」com.smart.person」>

    <property name=」name」>

                              <map>

                                       <entry>

                                                <key><value>key</value></key>

                                                <value>value</value>

                                        </entry>

                            </map>

    </property>

</bean>

 

(3)       Properties

Properties能夠看做Map的特例,Map的鍵值可使任何類型的對象,而Properties鍵值只能是字符串

 

<bean id=」person」 class=」com.smart.person」>

                    <property name=」name」>

                             <props>

                                       <prop key=」key」>value</prop>

                             </props>

                    </property>

</bean>

4.       經過util命名空間配置集合類型的Bean以及p簡化配置

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:p="http://www.springframework.org/schema/p"

       xmlns:util="http://www.springframework.org/schema/util"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

       http://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/util

       http://www.springframework.org/schema/util/spring-util-3.1.xsd>

…..

           <util:list id=」favorite」 list-class=」java.util.ArrayList」>

                    <value>value</value>

           </util:list>

           <util:set id=」」 set-class=」」>

                    <value></value>

           </util:list>

           <util:map id=」」 map-class=」」>

                    <entry key=」」 value=」」 />

           </util:map>

           <! --list set支持 value-type map支持key-typevalue-type 限定值類型 -- >

……

           <! –p命名空間簡化配置 -- >

           <bean id=」car」 class=」」

                p:brand=」」

                p:maxSpeed=」」

                p:price=」」 />

                   

           <bean id=」person」 class=」」

                p:car-ref=」car」/>

</bean>

相關文章
相關標籤/搜索