02Spring基於xml的IOC配置--實例化Bean的三種方式

maven依賴

    <dependencies>
        <!--IOC相關依賴-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

applicationContext.xmlspring

<?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">



</beans>

 

一、使用默認無參構造函數實例化bean

1.1 建立Student實體類app

public class Student {
    private String name;
    private Integer age;

    
    public void say() {
        System.out.println("I'm a Student");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

1.2 在applicationContext.xml文件中裝配Student對象dom

<?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">


    <!-- 裝配Studnt對象到IOC容器中 -->
    <bean id="student" class="com.demo.domain.Student"/>

</beans>

1.3 測試maven

    private ApplicationContext ac;

    @Before
    public void init() {
        //加載配置文件
        ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    }


    @Test
    public void getStudentObjectFromSrpingIoc() {
        //1. 根據bean的id去IOC容器中獲取Student對象
        Student student = (Student) ac.getBean("student");
        //2. say
        student.say();
    }

 

 

 2. 使用靜態工廠方法實例化bean

2.1 建立Teacher實體類函數

public class Teacher {

    public void say() {
        System.out.println("I'm a teacher");
    }
}

2.2 建立靜態工廠類測試

public class StaticFactory {

    /**
     * 用於建立Teacher對象
     * @return
     */
    public static Teacher createTeacher() {
        return new Teacher();
    }
}

 

2.3 使用靜態工廠方法裝配Teacher對象this

<?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">

    <!-- 裝配Studnt對象到IOC容器中 -->
    <bean id="student" class="com.demo.domain.Student"/>

    <!-- 使用靜態工廠方法將Teacher對象裝配到IOC容器中 -->
    <bean id="teacher" class="com.demo.factory.StaticFactory" factory-method="createTeacher"/>


</beans>

2.4 測試spa

    @Test
    public void getTeaccherObjectFromSrpingIoc() {
        //1. 根據bean的id去IOC容器中獲取Teacher對象
        Teacher teacher = (Teacher) ac.getBean("teacher");
        //2. say
        teacher.say();
    }

三、使用實例工廠方法實例化bean

3.1 建立Cat實體類code

public class Cat {
    public void say() {
        System.out.println("I'm a cat");
    }
}

3.2 建立實例工廠類xml

public class InstanceFactory {

    /**
     * 用於建立cat對象
     * @return
     */
    public Cat createCat() {
        return new Cat();
    }
}

3.3 使用實例工廠建立Cat對象

<?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">


    <!-- 裝配Studnt對象到IOC容器中 -->
    <bean id="student" class="com.demo.domain.Student"/>

    <!-- 使用靜態工廠方法將Teacher對象裝配到IOC容器中 -->
    <bean id="teacher" class="com.demo.factory.StaticFactory" factory-method="createTeacher"/>

    <!-- 使用實例工廠方法實例化bean -->
    <!-- 1. 裝配實例工廠-->
    <bean id="instanceFactory" class="com.demo.factory.InstanceFactory"/>
    <!-- 2. 使用實例工廠建立cat對象-->
    <bean id="cat" factory-bean="instanceFactory" factory-method="createCat"/>

</beans>

3.4 測試

    @Test
    public void getCatObjectFromSrpingIoc() {
        //1. 根據bean的id去IOC容器中獲取Cat對象
        Cat cat = (Cat) ac.getBean("cat");
        //2. say
        cat.say();
    }

四、小結

4.1 默認無參構造實例化bean

  Spring的IOC在使用bean標籤中的class屬性配置的類的全限定類名經過反射建立對象,反射時默認調用的是類無參構造方法來實例化該對象的。

4.2 靜態工廠方法實例化bean

  應用場景:靜態工廠方法實例化bean的目的是爲了將一個由靜態方法獲取的對象裝配到IOC容器中。

  在Spring的配置文件中配置bean標籤,經過factory-method來引用獲取靜態方法返回的對象並裝配到Spring的IOC容器中.

4.3 實例工廠方法實例化bean

  應用場景:是爲了將一個由非靜態方法返回的對象裝配到IOC容器中。

  一、 經過將實例工廠做爲一個bean裝配到IOC容器中

  二、 經過配置一個新的bean,使用factory-bean引用實例工廠這個bean,使用factory-method引用其中的非靜態方法獲取其返回的對象並將其裝配到IOC容器中。

相關文章
相關標籤/搜索