iT邦幫忙

2022 iThome 鐵人賽

DAY 8
0
Software Development

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

Day08 - Dependency Injection (3)

  • 分享至 

  • xImage
  •  

Review

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

  • 屬性賦null
  • 引用內部、外部的bean
  • 為list屬性賦值
  • 為map屬性賦值
  • 為properties賦值
  • 創建集合類型的bean(透過util schema)

今日將接續昨天的屬性賦值,再探討bean的重用與bean scope

為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="user08" class="com.swj.User">
        <property name="userInfo" ref="userInfo"></property>
        <property name="userInfo.dept" value="財務部"></property>
    </bean>
</beans>
@Test
public void testDay08(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean08.xml");
    User user= ioc.getBean("user08", User.class);
    System.out.println(user);
}

Result
https://ithelp.ithome.com.tw/upload/images/20220923/20128084yFYoDJ50gc.jpg

bean配置的重用

繼承重用

有的時候我麼可能值需要對現有的bean修改一點點東西,就可以透過bean標籤中的parent屬性達成

<?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="user08" class="com.swj.User">
        <property name="userInfo" ref="userInfo"></property>
        <property name="userInfo.dept" value="財務部"></property>
    </bean>
    <bean id="user08_reuse" class="com.swj.User" parent="user08">
        <property name="name" value="james"></property>
        <property name="userInfo" ref="userInfo"></property>
        <property name="userInfo.dept" value="財務部"></property>
    </bean>
</beans>
@Test
public void testDay08(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean08.xml");
    User user= ioc.getBean("user08", User.class);
    User userReuse= ioc.getBean("user08_reuse", User.class);
    System.out.println(user);
    System.out.println(userReuse);
}

Result
https://ithelp.ithome.com.tw/upload/images/20220923/20128084IHisRqFqK6.jpg

abstract template bean

透過bean標籤中的abstract屬性可以指定該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>
    <!--設定abstract屬性為true -->
    <bean id="user08" class="com.swj.User" abstract="true">
        <property name="userInfo" ref="userInfo"></property>
        <property name="userInfo.dept" value="財務部"></property>
    </bean>
    <bean id="user08_reuse" class="com.swj.User" parent="user08">
        <property name="name" value="james"></property>
        <property name="userInfo" ref="userInfo"></property>
        <property name="userInfo.dept" value="財務部"></property>
    </bean>
</beans>
@Test
public void testDay08(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean08.xml");
    User userReuse= ioc.getBean("user08_reuse", User.class);
    System.out.println(userReuse);
    User user= ioc.getBean("user08", User.class);
    System.out.println(user);
}

Result
https://ithelp.ithome.com.tw/upload/images/20220923/20128084M1j83V2y8V.jpg

Bean Scopes

singlton 單實例(預設)

  • 容器啟動完成就創建好對象

設定測試類別constructor

public class User {
    public User() {
        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="user08" class="com.swj.User">
        <property name="name"  value="james"></property>
        <property name="gender" value="male"></property>
    </bean>
</beans>
@Test
public void testDay08(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean08.xml");
    System.out.println("容器啟動完成....");
}

Result
https://ithelp.ithome.com.tw/upload/images/20220923/20128084OohCWgzUwU.jpg

prototype 多實例

  • 容器啟動不會創建多個實例
  • 獲取的時候才創建實例
<?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">
    <!-- scope設為prototype   -->
    <bean id="user08" class="com.swj.User" scope="prototype">
        <property name="name"  value="james"></property>
        <property name="gender" value="male"></property>
    </bean>
</beans>
@Test
public void testDay08(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean08.xml");
    System.out.println("容器啟動完成....");
    User user1= ioc.getBean("user08", User.class);
    User user2= ioc.getBean("user08", User.class);
    System.out.println(user1 == user2);
}

Result
https://ithelp.ithome.com.tw/upload/images/20220923/20128084wTljWRqUrO.jpg


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

尚未有邦友留言

立即登入留言