Spring第三章:建立對象的三種方式

Spring 建立對象的三種方式

  1. 經過構造方法建立

    1.1 無參構造建立:默認狀況.

    1.2 有參構造建立:須要明確配置

      1.2.1 須要在類中提供有參構造方法

      

package com.bjsxt.pojo;

public class People {
    private int id;
    private String name;

    /**
     *注意這裏一旦使用了有參的構造器以後就必須生成這個
     * 無參的構造器否則spring會報錯No matching constructor found in class 'People'
     */
    public People() {

    }


    public People(int id, String name) {
        this.id = id;
        this.name = name;
    }

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

 

 

 

      1.2.2 applicationContext.xml 中設置調用哪一個構造方法建立對象

      

<?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="peo" class="com.bjsxt.pojo.People" />

    <!--使用有參構造器注入對象 須要set方法和有參構造器支持-->
    <bean id="peo2" class="com.bjsxt.pojo.People">
        <constructor-arg index="0" value="1"></constructor-arg>
        <constructor-arg index="1" value="zhangsan"></constructor-arg>
    </bean>

</beans>

 

        1.2.2.1 若是設定的條件匹配多個構造方法執行最後的構造方法
        1.2.2.2 index : 參數的索引,0 開始
        1.2.2.3 name: 參數名
        1.2.2.4 type:類型(區分開關鍵字和封裝類 int Integer)

    1.2.3測試類

package com.bjsxt.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bjsxt.pojo.People;
import com.bjsxt.pojo.PeopleFactory;

public class Test {
    public static void main(String[] args) {

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

        People peo = ac.getBean("peo",People.class);
        System.out.println(peo);

        People peo2 = ac.getBean("peo2",People.class);
        System.out.println(peo2);

    }
}

 

 

 

    1.2.4運行結果

 

二月 27, 2019 10:59:41 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@179d3b25: startup date [Wed Feb 27 22:59:41 CST 2019]; root of context hierarchy
二月 27, 2019 10:59:41 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
People [id=0, name=null]
People [id=1, name=zhangsan]

 

 

  2. 實例工廠

    2.1 工廠設計模式:幫助建立類對象.一個工廠能夠生產多個對象.

    2.2 實例工廠:須要先建立工廠,才能生產對象

 

    2.3 實現步驟:

      2.3.1 必需要有一個實例工廠

      

package com.bjsxt.pojo;

public class People {
    private int id;
    private String name;

    /**
     *注意這裏一旦使用了有參的構造器以後就必須生成這個
     * 無參的構造器否則spring會報錯No matching constructor found in class 'People'
     */
    public People() {

    }


    public People(int id, String name) {
        this.id = id;
        this.name = name;
    }

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

 

package com.bjsxt.pojo;

public class PeopleFactory {

    /**
     * 注意這裏千萬不能定義爲stataic的
     * @return
     */
    public  People newInstance(){
        return new People(1,"測試");
    }
}

 

 

      2.3.2 applicationContext.xml 中配置工廠對象和須要建立的對象

  

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

    <!--&lt;!&ndash; 使用無參構造器注入對象&ndash;&gt;
    <bean id="peo" class="com.bjsxt.pojo.People" />

    &lt;!&ndash;使用有參構造器注入對象 須要set方法和有參構造器支持&ndash;&gt;
    <bean id="peo2" class="com.bjsxt.pojo.People">
        <constructor-arg index="0" value="1"></constructor-arg>
        <constructor-arg index="1" value="zhangsan"></constructor-arg>
    </bean>-->

    <bean id="factory"  class="com.bjsxt.pojo.PeopleFactory"></bean>

    <bean id="peo3" factory-bean="factory" factory-method="newInstance"></bean>

</beans>

      2.3.3 測試類

package com.bjsxt.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bjsxt.pojo.People;
import com.bjsxt.pojo.PeopleFactory;

public class Test {
    public static void main(String[] args) {

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

//        People peo = ac.getBean("peo",People.class);
//        System.out.println(peo);
//
//        People peo2 = ac.getBean("peo2",People.class);
//        System.out.println(peo2);

        People peo = ac.getBean("peo3",People.class);
        System.out.println(peo);

    }
}

      2.3.4 測試結果  

二月 27, 2019 11:12:24 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@179d3b25: startup date [Wed Feb 27 23:12:24 CST 2019]; root of context hierarchy
二月 27, 2019 11:12:24 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
People [id=1, name=測試]

  3. 靜態工廠

    3.1 不須要建立工廠,快速建立對象.

    3.2 實現步驟

      3.2.1 編寫一個靜態工廠(在方法上添加 static)

        

package com.bjsxt.pojo;

public class PeopleFactory1 {

    /**
     * 注意這裏靜態工廠須要定義爲statis的
     * @return
     */
    public  static People newInstance(){
        return new People(1,"測試");
    }
}

 

package com.bjsxt.pojo;

public class People {
    private int id;
    private String name;

    /**
     *注意這裏一旦使用了有參的構造器以後就必須生成這個
     * 無參的構造器否則spring會報錯No matching constructor found in class 'People'
     */
    public People() {

    }


    public People(int id, String name) {
        this.id = id;
        this.name = name;
    }

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

 

 

      3.2.2 applicationContext.xml

    

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

    <!--&lt;!&ndash; 使用無參構造器注入對象&ndash;&gt;
    <bean id="peo" class="com.bjsxt.pojo.People" />

    &lt;!&ndash;使用有參構造器注入對象 須要set方法和有參構造器支持&ndash;&gt;
    <bean id="peo2" class="com.bjsxt.pojo.People">
        <constructor-arg index="0" value="1"></constructor-arg>
        <constructor-arg index="1" value="zhangsan"></constructor-arg>
    </bean>-->

    <!--<bean id="factory"  class="com.bjsxt.pojo.PeopleFactory"></bean>-->

    <!--<bean id="peo3" factory-bean="factory" factory-method="newInstance"></bean>-->

    <!--靜態工廠注入-->
    <bean id="peo4" class="com.bjsxt.pojo.PeopleFactory1" factory-method="newInstance" />

</beans>

 

 

 

      3.2.3 測試類

 

package com.bjsxt.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bjsxt.pojo.People;
import com.bjsxt.pojo.PeopleFactory;

public class Test {
    public static void main(String[] args) {

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

//        People peo = ac.getBean("peo",People.class);
//        System.out.println(peo);
//
//        People peo2 = ac.getBean("peo2",People.class);
//        System.out.println(peo2);

//        People peo3 = ac.getBean("peo3",People.class);
//        System.out.println(peo3);

        People peo4 = ac.getBean("peo4",People.class);
        System.out.println(peo4);

    }
}

 

 

 

      3.2.4運行結果

 

二月 27, 2019 11:16:54 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@179d3b25: startup date [Wed Feb 27 23:16:54 CST 2019]; root of context hierarchy
二月 27, 2019 11:16:55 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
People [id=1, name=測試]
相關文章
相關標籤/搜索