iT邦幫忙

2022 iThome 鐵人賽

DAY 7
0
Software Development

這些年,我們早該學會的Spring Framework系列 第 7

Day07 - Dependency Injection (2)

  • 分享至 

  • xImage
  •  

Review

Container的職責在於創建、配置與組裝bean,昨天我們學到了

  • getBean的幾種方式
    • 透過設定檔的id
    • 透過類別
    • 透過類別與id,最推薦此種方法(因為不需要再轉型)
  • 配置bean透過Constructor
  • 配置bean透過p-namespace配置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;
}

為bean的各種屬性賦值

為屬性賦null

有一種情況在物件中有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
https://ithelp.ithome.com.tw/upload/images/20220922/20128084cfStptzYIT.jpg

引用其他bean(外部)

<?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
https://ithelp.ithome.com.tw/upload/images/20220922/20128084iG4JN9GUua.jpg

引用其他bean(內部)

<?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
https://ithelp.ithome.com.tw/upload/images/20220922/201280844V2191eI4b.jpg

為List屬性賦值

  • 透過list標籤賦值
  • 引用物件時使用<ref/>標籤
<?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);
}

https://ithelp.ithome.com.tw/upload/images/20220922/201280848t8LSoc8DN.jpg

為Map屬性賦值

  • 透過map標籤賦值
  • value引用物件時使用value-ref屬性
<?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
https://ithelp.ithome.com.tw/upload/images/20220922/20128084uNyRzq517z.jpg

為Properties屬性賦值

  • 透過props標籤賦值
  • 因為k=v都是String,值寫在標籤體內
<?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
https://ithelp.ithome.com.tw/upload/images/20220922/20128084c8jSeQpEHk.jpg

創建集合類型的bean

  • 透過util名稱空間設置
  • 創建預設的map為LinkedHashMap
<?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
https://ithelp.ithome.com.tw/upload/images/20220922/20128084hfaDpBHi3w.jpg


上一篇
Day06 - Dependency Injection (1)
下一篇
Day08 - Dependency Injection (3)
系列文
這些年,我們早該學會的Spring Framework30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言