Container的職責在於創建、配置與組裝bean,昨天我們學到了
今日將探討對於bean的各式各樣的屬性賦值,先設定一下今日的Class
public class User {
    private String name;
    private String gender = "female";
    private Integer age;
    private UserInfo userInfo;
    private List<UserRole> roles;
    private Map<String,Object> maps;
    private Properties properties;
    // toString、getter、setter略
}
public class UserRole {
    String roleName;
}
public class UserInfo {
    private String title;
    private String dept;
}
有一種情況在物件中有default值,但我們在某些情況下希望給他null值,例如面例子的gender
<?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.xsd">
    <bean id="user07" class="com.swj.User">
        <property name="gender"><null/></property>
    </bean>
</beans>
@Test
public void testDay07(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean07.xml");
    User user= ioc.getBean("user07", User.class);
    System.out.println(user.getGender() ==null);
}
Result
<?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.xsd">
    <bean id="userInfo" class="com.swj.UserInfo">
        <property name="title" value="manager"></property>
        <property name="dept" value="risk management"></property>
    </bean>
    <bean id="user07" class="com.swj.User">
        <property name="userInfo" ref="userInfo"></property>
    </bean>
</beans>
@Test
public void testDay07(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean07.xml");
    User user= ioc.getBean("user07", User.class);
    //確認引用成功
    System.out.println(user);
    //確認引用對象為同一個
    UserInfo info = ioc.getBean("userInfo", UserInfo.class);
    System.out.println(user.getUserInfo() == info);
}
Result
<?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.xsd">
    <bean id="user07" class="com.swj.User">
        <property name="userInfo">
            <bean id ="userInfo_01" class="com.swj.UserInfo">
                <property name="title" value="vice president"></property>
                <property name="dept" value="accounting"></property>
            </bean>
        </property>
    </bean>
</beans>
@Test
public void testDay07(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean07.xml");
    User user= ioc.getBean("user07", User.class);
    //確認引用成功
    System.out.println(user);
    //container無法取得內部引用的bean
    UserInfo info = ioc.getBean("userInfo_01", UserInfo.class);
    System.out.println(user.getUserInfo() == info);
}
Result
<?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.xsd">
    <bean id="role" class="com.swj.UserRole">
        <property name="roleName" value="部門主管"></property>
    </bean>
    <bean id="user07" class="com.swj.User">
        <property name="roles">
            <list>
                <bean class="com.swj.UserRole">
                    <property name="roleName" value="行政窗口"></property>
                </bean>
                <ref bean="role"/>
            </list>
        </property>
    </bean>
</beans>
@Test
public void testDay07(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean07.xml");
    User user= ioc.getBean("user07", User.class);
    System.out.println(user);
}

<?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.xsd">
    <bean id="role" class="com.swj.UserRole">
        <property name="roleName" value="部門主管"></property>
    </bean>
    <bean id="user07" class="com.swj.User">
        <property name="maps">
            <map>
                <entry key="key1" value="hello"></entry>
                <entry key="key2" value="100"></entry>
                <entry key="key3" value-ref="role"></entry>
                <entry key="key4">
                    <bean class="com.swj.UserRole">
                        <property name="roleName" value="測試者"></property>
                    </bean>
                </entry>
            </map>
        </property>
    </bean>
</beans>
@Test
public void testDay07(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean07.xml");
    User user= ioc.getBean("user07", User.class);
    System.out.println(user);
}
Result
<?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.xsd">
    <bean id="user07" class="com.swj.User">
        <property name="properties">
           <props>
               <prop key="username">james</prop>
               <prop key="password">test123</prop>
           </props>
        </property>
    </bean>
</beans>
@Test
public void testDay07(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean07.xml");
    User user= ioc.getBean("user07", User.class);
    System.out.println(user);
}
Result
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
    <bean id="role" class="com.swj.UserRole">
        <property name="roleName" value="部門主管"></property>
    </bean>
    <bean id="user07" class="com.swj.User">
        <property name="maps" ref="myMap"></property>
    </bean>
    <util:map id="myMap">
        <entry key="key1" value="hello"></entry>
        <entry key="key2">
            <value>100</value>
        </entry>
        <entry key="key3" value-ref="role"></entry>
        <entry key="key4">
            <bean class="com.swj.UserRole">
                <property name="roleName" value="測試者"></property>
            </bean>
        </entry>
    </util:map>
</beans>
@Test
public void testDay07(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean07.xml");
    User user= ioc.getBean("user07", User.class);
    System.out.println(user.getMaps());
    System.out.println(user.getMaps().getClass());
}
Result