1. 程式人生 > >Spring創建對象的幾種方法

Spring創建對象的幾種方法

ack private log color 調用 print close cati 創建對象

一、通過構造器

無參構造器

<?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"
> <!--配置我們的Dog類 spring 框架在底層 通過反射的機制 執行了我們的構造方法--> <bean id="dog" class="cn.pb.dao.impl.Dog"></bean> </beans>

直接這樣配置一個bean的話,相當於是調用這個Dog類的無參構造器,如果無參構造器不在,Spring上下文創建對象的時候就會報錯。

無參構造器加setter方法註入field的值

類:

package com.mc.base.learn.spring.bean;

public
class Person { private String name; private Integer id; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) {
this.id = id; } @Override public String toString() { return "Person [name=" + name + ", id=" + id + "]"; } }

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 class="com.mc.base.learn.spring.bean.Person" id="person">
        <property name="name" value="LiuChunfu"></property>
        <property name="id" value="125"></property>
    </bean>
    
</beans>

就bean標簽下再用property標簽來設置name和value

如果有field是引用型的,就不用value,用ref = "xxx",xxx就是某個bean的id或者name

利用有參數的構造方法:

類代碼:

技術分享圖片
package com.mc.base.learn.spring.bean;

public class Person {

    private String name;
    private Integer id;
    
    public Person(String name, Integer id) {
        super();
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", id=" + id + "]";
    }

}
View Code

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 class="com.mc.base.learn.spring.bean.Person" id="person">
        <constructor-arg name="id" value="123"></constructor-arg>
        <constructor-arg name="name" value="LiuChunfu"></constructor-arg>
    </bean>
    
</beans>
View Code

就是在bean標簽下面,有個<constructor-arg>標簽來確定這個bean的構造器的參數咯。

二、靜態的工廠方法

package cn.pb.dao;

import cn.pb.dao.impl.Dog;

/**
 * AnimalFactory靜態工廠類
 */
public class AnimalFactory {
    /**
     * 可以看到程序沒有走構造方法
     */
    public AnimalFactory(){
        System.out.println("靜態工廠的無參構造====");
    }
//靜態工廠,不會走無參構造
    public static Animal getDog(){
        System.out.println("工廠中靜態獲取Dog實例的方法");
        return new Dog();
    }
}

然後配置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">

   

    <!--02.通過靜態工廠 來創建我們對象的實例
     工廠的本身構造不會執行 因為我們的方法是靜態的 !類只會被加載,不會被實例化!
     getDog必須是靜態的-->
    <bean id="dog" class="cn.pb.util.AnimalFactory" factory-method="getDog"></bean>  

</beans>

三、動態工廠——就非static的getDog方法

package cn.pb.dao;

import cn.pb.dao.impl.Dog;

/**
 * AnimalFactory動態工廠類
 */
public class AnimalFactory {
    /**
     * 程序會先創建工廠實例 再調用getDog()方法
     */
    public AnimalFactory(){
        System.out.println("動態工廠的無參構造====");
    }
//動態工廠 會先走無參構造  創建實例
    public Animal getDog(){
        System.out.println("工廠中動態工廠獲取Dog實例的方法");
        return new Dog();
    }
}

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

   

    <!--03.動態工廠創建 對象的實例-->
    <bean id="factory" class="cn.pb.util.AnimalFactory"></bean><!-- 調用哪個工廠裏的哪個方法  來創建對象  對象的id是dog-->
    <bean id="dog" factory-bean="factory" factory-method="getDog"/>

</beans>

這個Dog的對象呢,需要一個實例的一個getDog方法嘛,所以要先有這個實例Factory的bean,然後再在這個factory-bean上填這個factory的bean。

參考文章:

例子分別在這兩篇當中搞過來的hh

https://www.cnblogs.com/LiuChunfu/p/5574383.html——《Spring創建對象的方式3種方式》這個是真的三種方法,下面那個emm

https://www.cnblogs.com/lyb0103/p/7611826.html——《Spring筆記03(Spring創建對象的三種方式) 》工廠方法還有靜態工廠的用的是這篇的例子

Spring創建對象的幾種方法