2015年12月23日 星期三

內部 bean 和 Collection (Spring3.x 七)

※內部 bean

參考文件

Comic.java

public class Comic {
    private String comicName;
    // setter/getter...
}

Book.java

public class Book {
    private String bookName;
    private Comic comic;
    // setter/getter...
}

applicationContext.xml

<bean id="co" class="book.vo.Comic" p:comicName="七龍珠"/>
<bean id="book1" class="book.vo.Book">
    <property name="comic" ref="co" />
</bean>
    
<bean id="book2" class="book.vo.Book">
    <property name="comic">
        <bean class="book.vo.Comic">
            <property name="comicName" value="通靈王" />
        </bean>
    </property>
</bean>

測試類

ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    
Book book1 = (Book) appContext.getBean("book1");
Book book2 = (Book) appContext.getBean("book2");
    
System.out.println(book1.getComic().getComicName());
System.out.println(book2.getComic().getComicName());
    
((ClassPathXmlApplicationContext) appContext).close();

※結果:
七龍珠
通靈王

※book1為一般的參考寫法; book2就是內部的bean寫法



※Collection

參考文件


※放基本型別

Book.java

public class Book {
    private Set<String> comicNames;
    private List<Integer> prices;
    private Map<String, Integer> comicNameAndPrice;
    private Properties prop;
    // setter/getter...
}

※可以不用new實體,不過聽說舊版本要new實體

applicationContext.xml

<bean id="book" class="book.vo.Book">
    <property name="comicNames">
        <set>
            <value>七龍珠</value>
            <value>滾球王</value>
            <value>通靈王</value>
            <value>滾球王</value>
        </set>
    </property>
    
    <property name="prices">
        <list>
            <value type="java.lang.Integer">65</value>
            <value type="java.lang.Integer">70</value>
            <value type="int">75</value>
            <value type="int">70</value>
        </list>
    </property>
    
    <property name="comicNameAndPrice">
        <map>
            <entry key="七龍珠" value="65" />
            <entry key="滾球王" value="70" />
            <entry>
                <key><value>通靈王</value></key>
                <value>75</value>
            </entry>
        </map>
    </property>
    
    <property name="prop">
        <props>
            <prop key="aaa">111</prop>
            <prop key="bbb">222</prop>
            <prop key="ccc">333</prop>
        </props>
    </property>
</bean>

測試類

ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    
Book book = (Book) appContext.getBean("book");
System.out.println(book.getComicNames());
System.out.println(book.getPrices());
System.out.println(book.getComicNameAndPrice());
System.out.println(book.getProp());
    
((ClassPathXmlApplicationContext) appContext).close();

※結果:
[七龍珠, 滾球王, 通靈王]
[65, 70, 75, 70]
{七龍珠=65, 滾球王=70, 通靈王=75}
{bbb=222, aaa=111, ccc=333}

※可以看出Set真的會把相同的值去掉,而map有兩種寫法

※map可以和Properties互換,就是xml是<prop>,卻用map接; 相反也是一樣,只要注意Properties只能兩邊都是String,而Map用<String, String>即可



※放物件

Comic.java

public class Comic {
    private String comicName;
    // setter/getter...
}

Book.java

public class Book {
    private Set<Comic> comicNames;
    private List<Comic> prices;
    private Map<Comic, Integer> comicNameAndPrice;
    // setter/getter...
}

applicationContext.xml

<bean id="xxx1" class="book.vo.Comic" p:comicName="七龍珠" />
<bean id="xxx2" class="book.vo.Comic" p:comicName="通靈王" />
<bean id="xxx3" class="book.vo.Comic" p:comicName="滾球王" />
    
<bean id="book" class="book.vo.Book">
    <property name="comicNames">
        <set>
            <ref local="xxx1" />
            <ref local="xxx2" />
            <ref local="xxx3" />
            <ref local="xxx2" />
        </set>
    </property>
    
    <property name="prices">
        <list>
            <ref local="xxx1" />
            <ref local="xxx2" />
            <ref local="xxx3" />
            <ref local="xxx2" />
        </list>
    </property>
    
    <property name="comicNameAndPrice">
        <map>
            <entry key-ref="xxx1" value="65" />
            <entry key-ref="xxx2" value="70" />
            <entry key-ref="xxx3" value="75" />
        </map>
    </property>
</bean>

測試類

ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    
Book book = (Book) appContext.getBean("book");
    
Iterator<Comic> itSet = book.getComicNames().iterator();
while (itSet.hasNext()) {
    System.out.println(itSet.next().getComicName());
}
System.out.println();
    
Iterator<Comic> itList = book.getPrices().iterator();
while (itList.hasNext()) {
    System.out.println(itList.next().getComicName());
}
System.out.println();
    
Map<Comic, Integer> map = book.getComicNameAndPrice();
Iterator<Comic> itMap = map.keySet().iterator();
while (itMap.hasNext()) {
    System.out.println(itMap.next().getComicName());
}
System.out.println(map.values());
    
((ClassPathXmlApplicationContext) appContext).close();

※結果:
七龍珠
通靈王
滾球王

七龍珠
通靈王
滾球王
通靈王

七龍珠
通靈王
滾球王
[65, 70, 75]

※Properties就沒有關聯到物件的ref屬性了



※Collection的merge

參考文件

Book.java

public class Book {
    private List<Integer> prices;
    // setter/getter...
}

applicationContext.xml

<bean id="bookPapa" class="book.vo.Book">
    <property name="prices">
        <list>
            <value type="java.lang.Integer">65</value>
            <value type="java.lang.Integer">70</value>
        </list>
    </property>
</bean>
    
<bean id="bookSon" class="book.vo.Book" parent="bookPapa">
    <property name="prices">
        <list merge="true">
            <value type="int">75</value>
            <value type="int">80</value>
        </list>
    </property>
</bean>

測試類

ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    
Book bookPapa = (Book) appContext.getBean("bookPapa");
Book bookSon = (Book) appContext.getBean("bookSon");
    
System.out.println(bookPapa.getPrices());
System.out.println(bookSon.getPrices());
    
((ClassPathXmlApplicationContext) appContext).close();

※結果:
[65, 70]
[65, 70, 75, 80]

※bookSon增加屬性parent,還有list有個屬性merge

※set、list、map、prop都有merge

沒有留言:

張貼留言