spring_IOC总结(二)--xml依赖注入

spring的bean对象--依赖注入

spring 创建bean对象细节

配置spring核心容器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">

    <!--把对象的创建交给spring管理-->
    <!--<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>-->
    <!--<bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"></bean>-->

    <!--spring对bean的管理细节-->
    <!--1. 创建bean的三种方式-->
    <!--第一种方式: 使用默认的构造函数创建
        在spring配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签。
        采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。
    -->
    <!--<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>-->
    <!--第二种方式: 使用普通的工厂模式创建对象(使用某个类中的方法创建对象,并存入spring容器)
        <bean id="instanceFactory" class="com.itheima.fatory.InstanceFactory"></bean>
        <bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>
    -->

    <!--第三种方式: 使用工厂种的静态方法创建对象(使用某个类中静态方法创建对象,并存入spring容器)
     <bean id="accountService" class="com.itheima.fatory.StaticFactory"  factory-method="getAccountService"></bean>
    -->

    <!--bean的作用范围
        bean标签的scope属性
            作用: 用于指定bean的作用范围
            取值:
                singleton: 单例模式(默认值)
                prototype: 多例模式
                request: 作用与web应用的请求范围
                session: 作用与web应用的会话范围
                global-session: 作用与集群环境的会话范围,当不是集群环境时,它就是session
    -->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" scope="prototype"></bean>
    <!-- bean的生命周期
        单例对象
            出生
            活着
            死亡
        多例对象
            出生

     -->
     <!-- bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" scope="prototype"
          init-method="init" destroy-method="destory"
    ></bean> -->
</beans>

获取核心容器的指定对象

public class Client {
    //获取spring的核心容器,并根据id获取对象
    public static void main(String[] args) {
        //1. 获取核心容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2. 根据id 获取bean对象
        IAccountService as1 = (IAccountService) ac.getBean("accountService");
        IAccountService as2 = (IAccountService) ac.getBean("accountService");
//        IAccountDao accountDao = ac.getBean("accountDao", IAccountDao.class);
//        System.out.println(accountService);
//        System.out.println(accountDao);
        System.out.println(as1 == as2);
    }
}

spring 依赖注入

bean.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">
    <!--
        spring中依赖注入
        依赖注入: Dependency Injection
        IOC作用:
            降低程序间的耦合(依赖关系)
        依赖关系的管理:
            以后都交给spring来维护
            在当前类需要用到其他类的对象,由spring为我们提供,我们只需要在配置文件中说明
        依赖关系的维护:
            就称之为依赖注入
        依赖注入:
            能注入数据:有三类
                基本类型和String
                其他bean类型(在配置文件中或者注解配置过的bean)
                复杂类型/集合类型
            注入的方式: 有三种
                第一种: 使用构造函数
                第二种: 使用set方法注入
                第三种: 使用注解提供
    -->
    <!--第一种 构造函数注入:
        使用的标签: constructor-arg(构造函数参数)
        标签属性:
            type:用于指定要注入的数据类型,该数据类型也是构造函数中的某一个或某些参数的类型
            index: 用于指定要注入的数据结构中指定索引的位置的参数赋值,索引从0 开始
            name: 用于指定给构造函数中的指定名称的参数赋值
            =========================以上三个用于指定构造函数中的那个参数赋值============================
            value: 用于提供基本类型和String类型的数据
            ref: 用于指定其他的bean类型数据。它指的就是出现spring的核心容器出现过的bean对象。

       优势:
            在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功。
       弊端:
            改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据,也必须提供。
    -->

    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
        <constructor-arg name="name" value="张三"></constructor-arg>
        <constructor-arg name="age" value="18"></constructor-arg>
        <constructor-arg name="birthday" ref="now" ></constructor-arg>
    </bean>
    <!--配置日期对象: Date now = new Date()-->
    <bean id="now" class="java.util.Date"></bean>

    <!--第二种 set方法注入             更常用的方法
        使用的标签: property
        标签属性:
            name: 用于指定注入时set方法名称
            value: 用于提供基本类型和String类型的数据
            ref: 用于指定其他的bean类型数据。它指的就是出现spring的核心容器出现过的bean对象。
        优势:
            创建对象时没有明确的限制,可以直接使用默认构造函数
        弊端:
            如果有某个成员必须有值,则获取对象有可能set方法没有执行
    -->
    <bean id="accountService2" class="com.itheima.service.impl.AccountServiceImpl2">
        <property name="name" value="李四"></property>
        <property name="age" value="21"></property>
        <property name="birthday" ref="now"></property>
    </bean>

    <!--复杂类型注入/集合类型注入
        用于给List结构集合注入的标签
            list array set
        用于Map结构集合注入的标签:
            map props
    -->
    <bean id="accountService3" class="com.itheima.service.impl.AccountServiceImpl3">
        <property name="myStrs">
            <array>
                <value>AAA</value>
                <value>BBB</value>
            </array>
        </property>

        <property name="myList">
            <list>
                <value>AAA</value>
                <value>BBB</value>
            </list>
        </property>
        <property name="mySet">
            <list>
                <value>AAA</value>
                <value>BBB</value>
            </list>
        </property>

        <property name="myMap">
            <map>
                <entry key="testA" value="AAA"></entry>
                <entry key="testB" value="BBB"></entry>
            </map>
        </property>

        <property name="myProps">
            <props>
                <prop key="testA">AAA</prop>
                <prop key="testB">BBB</prop>
            </props>
        </property>
    </bean>
</beans>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容