Spring 学习之路(三):Spring IOC 详解(bean的配置)

本文详细介绍了Spring的IOC容器如何创建bean实例,包括通过XML配置文件初始化bean,BeanFactory与ApplicationContext的区别,以及bean的属性注入方式如setter注入和构造器注入。此外,还讲解了bean的字面值注入、引用注入、级联属性、集合属性赋值以及自动装配的byType和byName模式。提供了丰富的代码示例和配置细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring的ioc容器如何创建bean实例的?
  • 在 Spring 的 IOC 容器配置文件里(applicationContext.xml)设置需要初始化的Bean
<!-- 配置bean对象 class:bean的全类名,通过反射的方式在IOC容器中创建Bean的对象,所以要求Bean中必须要有无参构造器 
        id:标识容器中的bean,id值唯一 -->
    <bean id="helloWorld" class="com.zc.cris.HelloWorld">
        <property name="name" value="cris"></property>
    </bean>
  • 在实例化bean之前需要首先实例化Spring ioc 容器

    1. 在 Spring IOC 容器读取 Bean 配置创建 Bean 实例之前, 必须对它进行实例化. 只有在容器实例化后, 才可以从 IOC 容器里获取 Bean 实例并使用
    2. Spring 提供了两种类型的 IOC 容器实现

      1. BeanFactory: IOC 容器的基本实现.
      2. ApplicationContext: 提供了更多的高级特性. 是 BeanFactory 的子接口

      3. BeanFactory 是 Spring 框架的基础设施,面向 Spring 本身;ApplicationContext 面向使用 Spring 框架的开发者,几乎所有的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  • 从ioc容器中根据id获取bean对象实例
HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
  • ioc初始化bean对象的时候,就可以对其属性进行赋值,一般都是通过setter方法注入,也可以通过构造器进行注入
<!-- 通过setter方法注入属性值 -->
<bean id="helloWorld" class="com.zc.cris.HelloWorld">
        <property name="name" value="cris"></property>
    </bean>


    <!-- 通过构造器的方法为bean注入属性值 -->
    <bean id="car1" class="com.zc.cris.Car">
        <constructor-arg value="法拉利" index="0"></constructor-arg>
        <constructor-arg value="意大利" index="1"></constructor-arg>
        <constructor-arg value="2000000.0" type="java.lang.Double"></constructor-arg>
    </bean>

    <!-- 使用构造器注入属性值可以指定参数的位置和类型,以区分重载的构造器 -->
    <bean id="car2" class="com.zc.cris.Car">
        <constructor-arg value="宝马" type="java.lang.String"></constructor-arg>
        <constructor-arg value="德国" type="java.lang.String"></constructor-arg>
        <constructor-arg value="300" type="java.lang.Integer"></constructor-arg>
    </bean>

    测试代码如下:

    @Test
    void test() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Car car1 = (Car) applicationContext.getBean("car1");
        System.out.println(car1);
        Car car2 = (Car) applicationContext.getBean("car2");
        System.out.println(car2);
    }

console:

mark

  • bean中的字面值
    1. 用字符串表示的值,可以通过 元素标签或 value 属性进行注入
    2. 基本数据类型及其封装类、String 等类型都可以采取字面值注入的方式
    3. 若字面值中包含特殊字符,可以使用
    <!-- 使用构造器注入属性值可以指定参数的位置和类型,以区分重载的构造器 -->
    <bean id="car2" class="com.zc.cris.Car">
        <constructor-arg value="宝马" type="java.lang.String"></constructor-arg>
        <!-- 如果字面值包括特殊字符可以使用<![CDATA[]] 进行包裹 -->
        <!-- 属性值也可以使用value子节点进行配置 -->
        <constructor-arg type="java.lang.String">
            <value><![CDATA[<北京*>]]></value>
        </constructor-arg>
        <constructor-arg type="java.lang.Integer">
            <value>255</value>
        </constructor-arg>
    </bean>


    <bean id="person" class="com.zc.cris.Person">
        <property name="name" value="jack"></property>
        <property name="age">
            <value>23</value>
        </property>

console:

mark

  • bean的引用

    1. 组成应用程序的 Bean 经常需要相互协作以完成应用程序的功能. 要使 Bean 能够相互访问, 就必须在 Bean 配置文件中指定对 Bean 的引用
    2. 在 Bean 的配置文件中, 可以通过 元素或 ref 属性为 Bean 的属性或构造器参数指定对 Bean 的引用

    3. 也可以在属性或构造器里包含 Bean 的声明, 这样的 Bean 称为内部 Bean

    4. 当 Bean 实例仅仅给一个特定的属性使用时, 可以将其声明为内部 Bean. 内部 Bean 声明直接包含在 或 元素里, 不需要设置任何 id 或 name 属性

    5. 内部 Bean 不能使用在任何其他地方

代码说明:

    <bean id="person" class="com.zc.cris.Person">
        <property name="name" value="jack"></property>
        <property name="age">
            <value>23</value>
        </property>

        <!-- 可以使用property的 ref 属性建立bean之间的引用 
        <property name="car" ref="car2"></property>
        -->
        <!-- <property name="car">
            <ref bean="car2"/>
        </property> -->

        <!-- 内部bean,不能被外部其他bean引用,只能在当前bean内部使用 
        <property name="car">
            <bean class="com.zc.cris.Car">
                <constructor-arg value="奔驰" index="0"></constructor-arg>
                <constructor-arg value="德国" index="1"></constructor-arg>
                <constructor-arg value="234544.0" type="java.lang.Double"></constructor-arg>
            </bean>
        </property>
        -->
  • 关于级联属性,就是为当前bean引用的bean重新赋值
    <!-- 为级联属性赋值:属性必须先初始化后才能为级联属性赋值,否则异常,和struts2不同 -->
        <property name="car" ref="car1"></property>
        <property name="car.maxSpeed" value="210"></property>
    </bean>
  • 如何给bean的集合属性赋值
- 重新定义一个javabean

public class Person {

    private String name;
    private Integer age;
    private List<Car> cars;
    ...
}

- applicationContext.xml配置上面的bean

<bean id="person2" class="com.zc.cris.collectionProperty.Person">
            <property name="name" value="cirs"></property>
            <property name="age" value="24"></property>
            <property name="cars">
                <list>
                    <ref bean="car1" />
                    <ref bean="car2" />
                    <bean class="com.zc.cris.Car">
                        <constructor-arg value="奔驰" index="0"></constructor-arg>
                        <constructor-arg value="德国" index="1"></constructor-arg>
                        <constructor-arg value="234544.0" type="java.lang.Double"></constructor-arg>
                    </bean>
                </list>
            </property>
        </bean>

- 测试代码

    /*
     * 测试bean的集合属性赋值
     */
    @Test
    void testCollectionProperty() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Object bean = applicationContext.getBean("person2");
        System.out.println(bean);

    }
  • 测试bean的map类型属性
<bean id="person3" class="com.zc.cris.collectionProperty.Person2">
        <property name="name" value="james"></property>
        <property name="age" value="34"></property>
        <property name="cars">
            <map>
                <entry key="AA" value-ref="car1"></entry>
                <entry key="BB" value-ref="car2"></entry>
            </map>
        </property>
    </bean>

测试代码略,console可以下载源码自己试试
- 测试properties属性(通常用于设置数据库的参数)

- 定义一个dataSource类含有properties属性

public class DataSource {

    private Properties properties;
    ...
}

- applicationContext.xml

    <!-- 配置properties属性 -->
    <bean name="dataSource" class="com.zc.cris.collectionProperty.DataSource">
        <property name="properties">
            <!-- 使用props和prop子节点来为properties属性值赋值 -->
            <props>
                <prop key="user">root</prop>
                <prop key="password">123456</prop>
                <prop key="jdbcUrl">jdbc:mysql:///test</prop>
                <prop key="driverClass">com.mysql.jdbc.Driver</prop>
            </props>
        </property>
    </bean>

测试代码略,console可以下载源码自己试试

  • 使用 utility scheme 定义集合

    1. 使用基本的集合标签定义集合时, 不能将集合作为独立的 Bean 定义, 导致其他 Bean 无法引用该集合, 所以无法在不同 Bean 之间共享集合

    2. 可以使用 util schema 里的集合标签定义独立的集合 Bean. 需要注意的是, 必须在 根元素里添加 util schema 定义

    <!-- 配置单例的集合bean,以供多个bean进行引用,需要导入util 命名空间 -->
    <util:list id="cars">
        <ref bean="car1"/>
        <ref bean="car2"/>
    </util:list>

    <bean id="person4" class="com.zc.cris.collectionProperty.Person">
        <property name="name" value="哈登"></property>
        <property name="age" value="29"></property>
        <property name="cars" ref="cars"></property>
    </bean>
  • bean的命名还可以通过p命名空间简化
<!-- 通过p命名空间为bean的属性赋值,需要先导入p命名空间,相对于传统的方式更加的简洁 -->
    <bean id="person5" class="com.zc.cris.collectionProperty.Person" 
    p:age="12" p:name="库里" p:cars-ref="cars" >

    </bean>
XML 配置里的 Bean 自动装配(了解)
  • Spring IOC 容器可以自动装配 Bean. 需要做的仅仅是在 的 autowire 属性里指定自动装配的模式

    1. byType(根据类型自动装配): 若 IOC 容器中有多个与目标 Bean 类型一致的 Bean. 在这种情况下, Spring 将无法判定哪个 Bean 最合适该属性, 所以不能执行自动装配.

    2. byName(根据名称自动装配): 必须将目标 Bean 的名称和属性名设置的完全相同.

    <bean id="car" class="com.zc.cris.autowire.Car">
        <property name="name" value="保时捷"></property>
        <property name="price" value="1234567.0"></property>
    </bean>
    <bean id="address" class="com.zc.cris.autowire.Address" p:city="上海" p:province="北京"></bean>

    <!-- 
        可以使用autowire属性指定自动装配的方式
        byName 根据bean的名字和当前待装配的bean的setter方法对应的属性名进行自动装配,匹配则装配,不匹配就不装配
        byType 根据bean的类型和当前bean的属性的类型进行自动装配,若ioc容器有一个以上的类型匹配的bean(即有两个car),则会抛异常
        很少使用,因为不够灵活,只有在整合第三方框架的时候可能会用到
     -->
    <bean id="person" class="com.zc.cris.autowire.Person" p:name="steve" autowire="byName">

    </bean>

console:

mark

源代码点我

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值