iT邦幫忙

2022 iThome 鐵人賽

DAY 5
0

Review

回到官網提供的IoC Container的圖,可以知道Spring Container將POJO與設定檔資訊合在一起,然後生產出物件,那麼這當中有什麼樣的細節呢,就是今日想要探討的主題。
ioc_flow
參考網址

  • POJO
public class User {
    private String name;
    private String gender;
    private String email;
    //getter、setter、toString 略
  • Configuration MetaData
<?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="user01" class="com.swj.User">
        <property name="name" value="James"></property>
        <property name="gender" value="male"></property>
        <property name="email" value="abc@test.com.tw"></property>
     </bean>
</beans>
  • The Spring Container
@Test
public void TestIoC(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean.xml");
    User user = (User)ioc.getBean("user01");
    System.out.println(user);
}

IoC你要知道的事

ApplicationContext

透過子類別ClassPathXmlApplicationContext將設定檔資訊載入到IoC容器中

設定檔bean的id作為取得物件的依據

在getBean的方法中傳入設定檔中設定的id

User物件創建的時間點

容器創建完成,物件也創建完成

稍微修改一下上面的範例,加入constructor

public class User {
    private String name;
    private String gender;
    private String email;
    public User() {
        System.out.println("user物件生成");;
    }
    //getter、setter、toString 略
@Test
public void TestIoC(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean.xml");
    System.out.println("IoC container prepared....");
    User user = (User)ioc.getBean("user01");
    System.out.println(user);
}

Result
result_object_create_timing

同一個id物件為Singleton

向容器取得同一個id兩次進行測試

@Test
public void TestIoC(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean.xml");
    System.out.println("IoC container prepared....");
    User user1 = (User)ioc.getBean("user01");
    User user2 = (User)ioc.getBean("user01");
    System.out.println(user1 == user2);
}

Result
result_object_singleton

找不到id拋出的異常訊息

NoSuchBeanDefinitionException

@Test
public void TestIoC(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean.xml");
    System.out.println("IoC container prepared....");
    User user = (User)ioc.getBean("user123456");
}

Result
result_object_exception

容器透過setter為POJO賦值

修改setter方法

public class User {
    public class User {
    private String name;
    private String gender;
    private String email;

    public User() {
        System.out.println("user物件生成");;
    }
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
    public String getName() {return name;}

    public void setName(String name) {
        System.out.println("調用setName");
        this.name = name;
    }
    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        System.out.println("調用setGender");
        this.gender = gender;
    }

    public String getEmail() {return email;}

    public void setEmail(String email) {
        System.out.println("調用setEmail");
        this.email = email;
    }
}
@Test
public void TestIoC(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean.xml");
    System.out.println("IoC container prepared....");
    User user = (User)ioc.getBean("user01");
}

Result
result_object_setter


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

尚未有邦友留言

立即登入留言