Spring 創建Bean的實例的3種方式


 

創建Bean的實例有3種方式:

  • 構造器方式
  • 靜態工廠方式
  • 實例工廠方式

 


 

 

構造器方式

構造器方式是最簡單,也是最常用的。

 

寫一個Bean,

  • 提供帶參的構造器:使用帶參的構造器創建bean的實例。
  • 或者提供無參的構造器+setter方法:先使用無參的構造器創建對象,再調用setter方法注入依賴。

使用注解或xml文件配置bean,注入所需依賴。

 

此種方式是使用構造方法直接創建Bean的實例,不由工廠類負責生產Bean的實例。

 

 


 

 

靜態工廠方式

需要創建一個工廠類,在工廠類中寫一個靜態方法,返回該Bean的一個實例。

 

1、寫一個Bean,提供帶參的構造器

public class Student {
    private int no;
    private String name;
    private float score;

    public Student(int no, String name, float score) {
        this.no = no;
        this.name = name;
        this.score = score;
    }

    //......
}

 

 

2、寫一個工廠類,用靜態方法生產該Bean的實例

public class StudentFactory {
    public static Student createStudent(int no,String name,float score) {
        return new Student(no, name, score);
    }
}

 因為在工廠中使用的是靜態方法來生產該Bean的實例,所以稱為靜態工廠方式。

一個工廠可以有多個靜態方法,生產多個Bean的多種實例。

 

 

3、在xml文件中配置工廠類

    <bean name="student" class="com.chy.utils.StudentFactory" factory-method="createStudent">
        <constructor-arg index="0" value="1"/>
        <constructor-arg index="1" value="chy"/>
        <constructor-arg index="2" value="100"/>
    </bean>

配置的是class="工廠類",因為是靜態方法,直接通過工廠類調用,所以不需要工廠類的實例,也就不需要在xml文件中配置工廠類的實例。這個配置創建的是Student類的實例。

factory-method指定用哪個靜態方法生產bean,<constructor-arg>給靜態方法傳入參數。

 

注解簡化了配置,但注解只能進行簡單的配置,復雜的配置還得在xml中配置。

通常注解、xml結合使用,簡單的配置用注解,復雜的配置寫在xml中。

 

 

4、使用

Student student = applicationContext.getBean("student", Student.class);

 

 


 

 

也可以使用空參構造器+setter方法注入,要麻煩些。只有1、2步不同:

1、提供空參構造器、setter方法

public class Student {
    private int no;
    private String name;
    private float score;

    public void setNo(int no) {
        this.no = no;
    }

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

    public void setScore(float score) {
        this.score = score;
    }

    //......
}

 

2、先調用空參構造器創建實例,再調用setter方法賦值

public class StudentFactory {
    public static Student createStudent(int no,String name,float score) {
        Student student = new Student();
        student.setNo(no);
        student.setName(name);
        student.setScore(score);
        return student;
    }
}

 

 


 

 

實例工廠方式

工廠中生產該Bean的方法不是靜態的,在xml文件中需要同時配置工廠、該Bean。

 

1、寫一個Bean,提供帶參的構造器

 

2、寫一個工廠類,用實例方法生產Bean(方法不用static修飾)

 

3、在xml文件中同時配置工廠類、該Bean

    <bean name="studentFactory" class="com.chy.utils.StudentFactory" />
    <bean name="student" class="com.chy.bean.Student" factory-bean="studentFactory" factory-method="createStudent">
        <constructor-arg index="0" value="1"/>
        <constructor-arg index="1" value="chy"/>
        <constructor-arg index="2" value="100"/>
    </bean>

因為沒有使用static修飾方法,不能通過工廠類的類名來調用,只能通過工廠類的實例來調用,所以要配置工廠類的實例。

 

4、使用

Student student = applicationContext.getBean("student", Student.class);

 

同樣可以使用空參構造器+setter方法的方式。

 

 


 

 

上面靜態工廠、實例工廠的xml配置,都是直接生產目標類的實例。

創建實例需要傳入實參,把實參寫死在xml中,這顯然不可取。上面的xml配置不常用。

 

 

一般只管理工廠類的實例:

<bean name="studentFactory" class="com.chy.utils.StudentFactory"/>

scope默認就是單例,不必顯式配置。

 

在程序中通過工廠創建目標對象,實參可變化:

StudentFactory studentFactory = applicationContext.getBean("studentFactory", StudentFactory.class);
Student student = studentFactory.createStudent(1, "chy", 100);

 

 

靜態工廠甚至不必進行配置,直接通過類名調用靜態方法創建目標對象:

Student student = StudentFactory.createStudent(1, "chy", 100);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM