码迷,mamicode.com
首页 > 编程语言 > 详细

On the way learning spring 3

时间:2015-10-23 06:45:59      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:

Spring-12 Factory Beans and Methods

 


static method in Class person

non-static method in personFactory

 

 

Spring-13 The P Namespace

注意Constructor 中的Arg 和建立的Bean之间的关系

p: tag 和 property tag 不要同时使用

 

Spring-14 Setting List Property

In beans.xml file

Insert constructor argument ----> (Right click constructor argument)Insert List Element ---->(Right click list element)Insert Value Element 

    <bean id="basket"
        class="com.caveofprogramming.spring.test.FruitBasket">
    <constructor-arg value="patrick‘s basket"></constructor-arg>
    <constructor-arg>
        <list>
            <value>apple</value>
            <value>peach</value>
            <value>pear</value>
            <value>orange</value>
            <value>banana</value>
        </list>
    </constructor-arg>
    </bean>

 Difference between ‘set‘ and ‘list‘

 

‘set‘ remove duplicate value.

 

Spring-15 Lists of Beans

 beans.xml
<bean id="jungle" class="com.caveofprogramming.spring.test.Jungle"> <property name="largest" ref="elephant"></property> <property name="animals"> <list> <ref bean="lion" /> <ref bean="snake" /> <ref bean="elephant" /> </list> </property> </bean>

App.java

Jungle jungle =(Jungle)context.getBean("jungle"); System.out.println(jungle);


Result
Largest: Animal [name=Richard, type=elephant] Others: Animal [name=Igor, type=lion] Animal [name=Bob, type=snake] Animal [name=Richard, type=elephant]

 

Spring-16 Inner Beans

 

Wrong edition

<bean id="jungle" class="com.caveofprogramming.spring.test.Jungle">
        <property name="largest">
            <bean id="elephant" class="com.caveofprogramming.spring.test.Animal">
                <property name="name" value="Richard"></property>
                <property name="type" value="elephant"></property>
            </bean>                   (inner bean)
        </property>
        <property name="animals">
            <list>
                <ref bean="lion" />
                <ref bean="snake" />
                <ref bean="elephant" />   (This part need to be deleted)
            </list>
        </property>
    </bean>

Reason: If you got an bean defined as an inner bean, you cannot refer to it somewhere else

 

<bean id="jungle" class="com.caveofprogramming.spring.test.Jungle">
        <property name="largest">
            <bean id="elephant" class="com.caveofprogramming.spring.test.Animal">
                <property name="name" value="Richard"></property>
                <property name="type" value="elephant"></property>
            </bean>
        </property>
        <property name="animals">
            <list>
                <bean id="lion" class="com.caveofprogramming.spring.test.Animal">
                    <property name="name" value="Igor"></property>
                    <property name="type" value="lion"></property>
                </bean>

                <bean id="snake" class="com.caveofprogramming.spring.test.Animal">
                    <property name="name" value="Bob"></property>
                    <property name="type" value="snake"></property>
                </bean>
            </list>
        </property>
    </bean>

 

 Spring-17 Property map

Insert property(foods)--->Insert props--->Insert prop element

    <bean id="jungle" class="com.caveofprogramming.spring.test.Jungle">
        <property name="foods">

        <props>
            <prop key="monkey">banana</prop>
            <prop key="panda">bamboo</prop>
            <prop key="snake">eggs</prop>
        </props>
        </property>
    </bean>

 

Spring-18 Arbitrary Maps as Bean Properties

Insert property(animals)--->insert Map--->insert Entity(entity key : lion   value-ref: lion)

 

<bean id="jungle"
        class="com.caveofprogramming.spring.test.Jungle">
        <property name="foods">

        <props>
            <prop key="monkey">banana</prop>
            <prop key="panda">bamboo</prop>
            <prop key="snake">eggs</prop>
        </props>
        </property>
        <property name="animals">
            <map>
                <entry key="lion" value-ref="lion"></entry>
                <entry key="snake" value-ref="snake"></entry>
                <entry key="elephant" value-ref="elephant"></entry>
            </map>
        </property>
    </bean>

 

On the way learning spring 3

标签:

原文地址:http://www.cnblogs.com/patrickspring/p/4903319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!