IOC關於bean的對象創建和數據依賴注入

對象創建方式

無參構造器創建

默認的

<bean id="car" class="com.gec.bean.Car" >
<property name="brand">
<value>寶馬</value>
</property>
<property name="price">
<value>10000000.00</value>
</property>
<property name="owner">
<value>小澤老師</value>
</property>
</bean>

有參構造器創建

需要創建有參構造器

<bean id="car2" class="com.gec.bean.Car" >
<constructor-arg name="brand" index="0" value="寶
馬" />
<constructor-arg name="price" index="1"
value="20000000.00" />
<constructor-arg name="owner" index="2" value="小
蒼老師" />
</bean>

靜態工廠模式

通過工廠類的靜態方法,創建bean對象

package com.gec.factory;
import com.gec.bean.Car;
/*
* 定義一個工廠類
* 作用:創建Car對象
* */
public class CarFactory {
//定義一個靜態工廠,作用:創建一個Car對象
public static Car createCar()
{
return new Car("奔馳",1000000.00,"朱大爺");
}
}

配置applicationContext.xml文件

<!--通過靜態工廠,創建bean對象-->
<bean
id="car3"
class="com.gec.factory.CarFactory"
factory-method="createCar"/>

非靜態工廠模式創建

通過靜態工廠方法創建bean對象

package com.gec.factory;
import com.gec.bean.Car;
/*
* 非靜態方法工廠類
配置applicationContext.xml文件
九、依賴注入
1、簡介
針對對象bean的屬性值注入數據
2、set方法注入
a、針對基本數據類型
保證屬性,有對應的set方法
* */
public class CarFactory1 {
public Car createCar()
{
Car car=new Car();
car.setBrand("奇瑞");
car.setPrice(150000.00);
car.setOwner("lily");
return car;
}
}

配置applicationContext.xml文件

<!--配置工廠類對象-->
<bean id="carFactory1"
class="com.gec.factory.CarFactory1"/>
<!--配置通過非靜態工廠創建bean對象-->
<bean id="car4" factory-bean="carFactory1"
factory-method="createCar" />

依賴注入

簡介

針對對象bean的屬性值注入數據

set方法注入

針對基本數據類型

保證屬性,有對應的set方法
在這裏插入圖片描述

針對對象類型注入

通過ref標籤實現

<?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="user" name="user1" class="com.gec.bean.User">
        <property name="id">
            <value>1</value>
        </property>
        <property name="name">
            <value>dada</value>
        </property>
        <property name="phone">
            <value>10086</value>
        </property>
        <property name="sex">
            <value>boy</value>
        </property>
        <property name="boss">
            <ref bean="boss"></ref>
        </property>
    </bean>

    <bean id="boss" class="com.gec.bean.Boss">
        <property name="name">
            <value>Boss</value>
        </property>
    </bean>

    <!--<bean id="user2" class="com.gec.bean.User">-->
        <!--<constructor-arg name="id"  index="0" value="1"></constructor-arg>-->
        <!--<constructor-arg name="name"  index="1" value="dada2"></constructor-arg>-->
        <!--<constructor-arg name="sex"  index="2" value="boy"></constructor-arg>-->
        <!--<constructor-arg name="phone"  index="3" value="10086"></constructor-arg>-->
    <!--</bean>-->
</beans>

main實現

 public static void main(String[] args) {
        //獲取IOC容器對象,可以用一個ApplicationContext對象描述此IOC容器對象
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

        //根據配置好的bean中的id獲取bean對象
        User user = (User) ctx.getBean("user");
        String bossName =user.getBoss().getName();
        System.out.println(user + bossName);
    }

在這裏插入圖片描述

構造器注入

通過構造器參數賦值

public User(Integer id, String name, String sex, String phone) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.phone = phone;
    }
<bean id="user2" class="com.gec.bean.User">
        <constructor-arg name="id"  index="0" value="1"></constructor-arg>
        <constructor-arg name="name"  index="1" value="dada2"></constructor-arg>
        <constructor-arg name="sex"  index="2" value="boy"></constructor-arg>
        <constructor-arg name="phone"  index="3" value="10086"></constructor-arg>
    </bean>

p標籤寫法

簡化屬性值注入數據的配置實現
導入p標籤的命名空間

<bean id="user" class="com.gec.bean.User"
    p:id="1" p:name="dada3" p:phone="10089" p:sex="boy"
    />
 public static void main(String[] args) {
        //獲取IOC容器對象,可以用一個ApplicationContext對象描述此IOC容器對象
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

        //根據配置好的bean中的id獲取bean對象
        User user = (User) ctx.getBean("user");
        //String bossName =user.getBoss().getName();
        System.out.println(user);
    }

複雜類型注入

簡介

可以針對數組、集合(List、Set、Map)、Poperties等數據類型,進行數據注入

數組及List

針對list和List數據類型進行數據注入

<bean id="user" name="user1" class="com.gec.bean.User">
        <property name="id">
            <value>1</value>
        </property>
        <property name="name">
            <value>dada</value>
        </property>
        <property name="phone">
            <value>10086</value>
        </property>
        <property name="sex">
            <value>boy</value>
        </property>
       <property name="color">
           <list>
               <value>red</value>
               <value>blue</value>
           </list>
       </property>

        <property name="bossList">
            <list>
                <ref bean="boss1"/>
                <ref bean="boss2"/>
            </list>
        </property>
    </bean>

    <bean id="boss1" class="com.gec.bean.Boss">
        <property name="name">
            <value>Boss1</value>
        </property>
    </bean>
    <bean id="boss2" class="com.gec.bean.Boss">
        <property name="name">
            <value>Boss2</value>
        </property>
    </bean>
 public static void main(String[] args) {
        //獲取IOC容器對象,可以用一個ApplicationContext對象描述此IOC容器對象
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

        //根據配置好的bean中的id獲取bean對象
       User user = (User) ctx.getBean("user");
      ArrayList<String> arrayList= (ArrayList<String>) user.getColor();
        for (String s : arrayList) {
            System.out.println(s);
        }
        ArrayList<Boss> bosses= (ArrayList<Boss>) user.getBossList();
        for (Boss boss : bosses) {
            System.out.println(boss);
        }

    }

在這裏插入圖片描述

Set集合

與List集合相似,不同的就是不可存儲重複的對象數據,只能是單一的

<bean id="car" class="com.gec.bean.Car">
<property name="brand" value="寶馬"/>
<property name="price" value="1000000.00" />
<property name="colors">
d、map數據類型集合
以key-value方式存儲數據
<list>
<value>red</value>
<value>green</value>
<value>blue</value>
</list>
</property>
<property name="ownerList">
<list>
<ref bean="owner01" />
<ref bean="owner02" />
</list>
</property>
<property name="ownerCardNo">
<set>
<value>sn001</value>
<value>sn002</value>
<value>sn003</value>
<value>sn003</value>
</set>
</property>
</bean>

map數據類型集合

以key-value方式存儲數據

<property name="ownerMap">
<map>
<entry key="sn001"><ref bean="owner01"/>
</entry>
<entry key="sn002"><ref bean="owner02"/>
</entry>
</map>
</property>
HashMap<String, String> favoriteMap =
(HashMap<String, String>) ctx.getBean("emails");
for (Map.Entry<String, String> fav :
favoriteMap.entrySet()) {
System.out.println("key=" + fav.getKey() + "
value=" + fav.getValue());
}

properties數據類型

存儲一些常用的配置信息,例如:數據庫配置信息

<property name="properties">
<props>
<prop
key="driver_class">com.mysql.jdbc.Driver</prop>
<prop
key="url">jdbc:mysql://localhost:3306/db_1976</prop>
<prop key="username">root</prop>
<prop key="password">1111</prop>
</props>
</property>
</bean>
</beans>

通過util命名空間配置集合

等同於配置各種的集合bean(List,set,map),用bean創建集合

<!--配置List集合bean-->
<utils:list id="favoritelist" listclass="java.util.ArrayList">
<value>看書</value>
<value>看電影</value>
<value>打遊戲</value>
</utils:list>
<utils:set id="favoriteSet" setclass="java.util.HashSet">
<value>看書</value>
<value>看電影</value>
<value>看電影</value>
</utils:set>
<utils:map id="emails" map-class="java.util.HashMap">
<entry key="AM" value="會見客戶"/>
<entry key="PM" value="公司內部會話"/>
</utils:map>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章