Spring配置 bean

在 Spring 的 IOC 容器里配置 Bean

<bean id="helloWorld" class="com.xiya.spring.beans.HelloWorld">
    <property name="name" value="lgh"/>
</bean>

在 xml 文件中经过 bean 节点来配置 bean
id:Bean 的名称。
在 IOC 容器中必须是惟一的
若 id 没有指定,Spring 自动将权限定性类名做为 Bean 的名字
id 能够指定多个名字,名字之间可用逗号、分号、或空格分隔

  • 在 Spring IOC 容器读取 Bean 配置建立 Bean 实例以前, 必须对它进行实例化. 只有在容器实例化后, 才能够从 IOC 容器里获取 Bean 实例并使用.
  • Spring 提供了两种类型的 IOC 容器实现. 
    • BeanFactory: IOC 容器的基本实现.
    • ApplicationContext: 提供了更多的高级特性. 是 BeanFactory 的子接口.BeanFactory 是 Spring 框架的基础设施,面向 Spring 自己;ApplicationContext 面向使用 Spring 框架的开发者,几乎全部的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory
  • 不管使用何种方式, 配置文件时相同的
ApplicationContext 的主要实现类:
ClassPathXmlApplicationContext:从 类路径下加载配置文件
FileSystemXmlApplicationContext: 从文件系统中加载配置文件
ConfigurableApplicationContext 扩展于 ApplicationContext,新增长两个主要方法:refresh() 和 close(), 让 ApplicationContext 具备启动、刷新和关闭上下文的能力
ApplicationContext 在初始化上下文时就实例化全部单例的 Bean。
WebApplicationContext 是专门为 WEB 应用而准备的,它容许从相对于 WEB 根目录的路径中完成初始化工做


依赖注入的方式:
一、属性注入
属性注入即经过 setter 方法注入Bean 的属性值或依赖的对象
属性注入使用 <property> 元素, 使用 name 属性指定 Bean 的属性名称,value 属性或 <value> 子节点指定属性值 
属性注入是实际应用中最经常使用的注入方式
<bean id="helloWorld" class="com.xiya.spring.beans.HelloWorld">
    <property name="name" value="lgh"/>
</bean>

二、构造器注入
经过构造方法注入Bean 的属性值或依赖的对象,它保证了 Bean 实例在实例化后就可使用。
构造器注入在 <constructor-arg> 元素里声明属性, <constructor-arg> 中没有 name 属性
<bean id="car" class="com.xiya.spring.beans.Car">
    <constructor-arg value="BMW"/>
    <constructor-arg value="china"/>
    <constructor-arg value="100000"/>
</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:bean的全类名,经过反射的方式在IOC容器中建立bean,因此要求bean中必须有无参数的构造器
        id:标识容器中的bean,且惟一
    -->
    <bean id="helloWorld" class="com.xiya.spring.beans.HelloWorld">
        <property name="name" value="lgh"/>
    </bean>

    <!--
        经过构造方法来配置bean的属性
    -->
    <bean id="car1" class="com.xiya.spring.beans.Car">
        <constructor-arg value="BMW" index="0"/>
        <constructor-arg value="beijing" index="1"/>
        <constructor-arg value="100000" index="2" type="double"/>
    </bean>
    <!--
        使用构造器注入属性值能够指定参数的位置和参数的类型!以区分重载的构造器
    -->
    <bean id="car2" class="com.xiya.spring.beans.Car">
        <constructor-arg value="Audi" index="0" type="java.lang.String"/>
        <constructor-arg value="shanghai" index="1" type="java.lang.String"/>
        <constructor-arg value="240" index="2" type="int"/>
    </bean>
</beans>

package com.xiya.spring.beans;

/**  * Created by N3verL4nd on 2017/3/22.  */ public class Car {
    private String brand;
    private String corp;
    private double price;
    private int maxSpeed;

    public Car(String brand, String corp, double price) {
        this.brand = brand;
        this.corp = corp;
        this.price = price;
    }

    public Car(String brand, String corp, int maxSpeed) {
        this.brand = brand;
        this.corp = corp;
        this.maxSpeed = maxSpeed;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", corp='" + corp + '\'' +
                ", price=" + price +
                ", maxSpeed=" + maxSpeed +
                '}';
    }
}


package com.xiya.spring.beans;

/**  * Created by N3verL4nd on 2017/3/22.  */ public class HelloWorld {
    private String name;

    public HelloWorld() {
        System.out.println("HelloWorld's constructor...");
    }

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

    public void hello() {
        System.out.println("hello " + name);
    }
}

package com.xiya.spring.beans;

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

/**  * Created by N3verL4nd on 2017/3/22.  */ public class Main {
    public static void main(String[] args) {
        /*
        //IOC容器为咱们执行建立对象与赋值操做
        HelloWorld helloWorld = new HelloWorld();
        //name属性赋值
        helloWorld.setName("lgh");*/


        //1、建立springIOC容器对象
        //ApplicationContext表明IOC容器
        //ClassPathXmlApplicationContext:ApplicationContext接口的实现类,该实现类从类路径下(src目录下)加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2、从IOC容器中获取bean实例
        //利用id返回到IOC容器中的bean
        HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
        //利用类型返回IOC容器中的bean,但要求IOC容器中必须只能有一个该类型的bean
        //HelloWorld helloWorld = context.getBean(HelloWorld.class);
        //3、调用hello()方法
        helloWorld.hello();


        Car car = (Car) context.getBean("car1");
        System.out.println(car);

        car = (Car) context.getBean("car2");
        System.out.println(car);
    }
}
字面值:可用字符串表示的值,能够经过 <value> 元素标签或 value 属性进行注入。
基本数据类型及其封装类、String 等类型均可以采起字面值注入的方式
若字面值中包含特殊字符,可使用 <![CDATA[]]> 把字面值包裹起来,或者使用XML里的转译字符。
<value><![CDATA[<Audi>]]]></value>
XML中的转译字符
&(逻辑与)  &amp;        
<(小于)    &lt;        
>(大于)    &gt;        
"(双引号)  &quot;      
'(单引号)  &apos;
<bean id="car1" class="com.xiya.spring.beans.Car">
    <constructor-arg value="beijing" index="1"/>
    <constructor-arg value="&lt;BMW&gt;" index="0"/>
    <constructor-arg value="100000" index="2" type="double"/>
</bean>
<!--
    使用构造器注入属性值能够指定参数的位置和参数的类型!以区分重载的构造器
-->
<bean id="car2" class="com.xiya.spring.beans.Car">
    <constructor-arg index="0" type="java.lang.String">
        <value><![CDATA[<Audi>]]]></value>
    </constructor-arg>
    <constructor-arg value="shanghai" index="1" type="java.lang.String"/>
    <constructor-arg index="2" type="int" value="240"/>
</bean>
组成应用程序的 Bean 常常须要相互协做以完成应用程序的功能. 要使 Bean 可以相互访问, 就必须在 Bean 配置文件中指定对 Bean 的引用
在 Bean 的配置文件中, 能够经过 <ref> 元素或 ref  属性为 Bean 的属性或构造器参数指定对 Bean 的引用. 
<bean id="person1" class="com.xiya.spring.beans.Person">
    <!--属性注入-->
    <!--<property name="name" value="lgh"/>
    <property name="age" value="24"/>
    <property name="car" ref="car2"/>-->
    <!--构造器注入-->
    <constructor-arg value="lgh"/>
    <constructor-arg value="24"/>
    <!--可使用propertyref属性创建bean之间的引用关系-->
    <constructor-arg index="2" ref="car1"/>
</bean>

也能够在属性或构造器里包含 Bean 的声明, 这样的 Bean 称为内部 Bean
<bean id="person2" class="com.xiya.spring.beans.Person">
    <property name="name" value="lgh"/>
    <property name="age" value="24"/>
    <!--内部bean,不可以被外部引用-->
    <property name="car">
        <bean class="com.xiya.spring.beans.Car">
            <constructor-arg value="BMW"/>
            <constructor-arg value="shanghai"/>
            <constructor-arg value="100000.0"/>
            <constructor-arg value="240"/>
        </bean>
    </property>
</bean>
当 Bean 实例仅仅给一个特定的属性使用时, 能够将其声明为内部 Bean. 内部 Bean 声明直接包含在 <property> 或 <constructor-arg> 元素里, 不须要设置任何 id 或 name 属性
内部 Bean 不能使用在任何其余地方
可使用专用的 <null/> 元素标签为 Bean 的字符串或其它对象类型的属性注入 null 值
和 Struts、Hiberante 等框架同样,Spring 支持级联属性的配置。
<property name="car">
    <bean class="com.xiya.spring.beans.Car">
        <constructor-arg value="BMW"/>
        <constructor-arg>
            <null/>
        </constructor-arg>
        <constructor-arg value="100000.0"/>
        <constructor-arg value="240"/>
    </bean>
</property>

<constructor-arg index="2" ref="car1"/>
<property name="car.brand" value="Audi"/>
在 Spring中能够经过一组内置的 xml 标签(例如: <list>, <set> 或 <map>) 来配置集合属性.
配置 java.util.List 类型的属性, 须要指定 <list>  标签, 在标签里包含一些元素. 这些标签能够经过 <value> 指定简单的常量值, 经过 <ref> 指定对其余 Bean 的引用. 经过<bean> 指定内置 Bean 定义. 经过 <null/> 指定空元素. 甚至能够内嵌其余集合.
数组的定义和 List 同样, 都使用 <list>
配置 java.util.Set 须要使用 <set> 标签, 定义元素的方法与 List 同样.
Java.util.Map 经过 <map> 标签订义, <map> 标签里可使用多个 <entry> 做为子标签. 每一个条目包含一个键和一个值. 
必须在 <key> 标签里定义键
由于键和值的类型没有限制, 因此能够自由地为它们指定 <value>, <ref>, <bean> 或 <null> 元素. 
能够将 Map 的键和值做为 <entry> 的属性定义: 简单常量使用 key 和 value 来定义; Bean 引用经过 key-ref 和 value-ref 属性定义
使用 <props> 定义 java.util.Properties, 该标签使用多个 <prop> 做为子标签. 每一个 <prop> 标签必须定义 key 属性. 
<property name="cars">
    <!--使用list节点为list类型的属性赋值-->
    <list>
      <ref bean="car1"/>
      <ref bean="car2"/>
        <bean class="com.xiya.spring.beans.collections.Car">
            <constructor-arg value="Ford"/>
            <constructor-arg value="changan"/>
            <constructor-arg value="200000.0"/>
            <constructor-arg value="200"/>
        </bean>
    </list>
</property>
<!--配置Properties属性值-->
<bean id="dataSource" class="com.xiya.spring.beans.collections.DataSource">
    <property name="properties">
        <!--使用propsprop子节点来为Properties属性赋值-->
        <props>
            <prop key="user">root</prop>
            <prop key="password">root</prop>
        </props>
    </property>
</bean>
<bean id="newPerson" class="com.xiya.spring.beans.collections.NewPerson">
    <property name="name" value="xiya"/>
    <property name="age" value="24"/>
    <property name="cars">
        <map>
            <entry key="BMW" value-ref="car1"/>
            <entry key="Audi" value-ref="car2"/>
        </map>
    </property>
</bean>
使用基本的集合标签订义集合时, 不能将集合做为独立的 Bean 定义, 致使其余 Bean 没法引用该集合, 因此没法在不一样 Bean 之间共享集合.
可使用 util schema 里的集合标签订义独立的集合 Bean. 须要注意的是, 必须在 <beans> 根元素里添加 util schema 定义
<!--配置独立的集合bean,以供多个bean进行引用-->
<util:list id="cars">
    <ref bean="car1"/>
    <ref bean="car2"/>
</util:list>

<bean id="person4" class="com.xiya.spring.beans.collections.Person">
    <property name="name" value="lgh"/>
    <property name="age" value="24"/>
    <property name="cars" ref="cars"/>
</bean>
•为了简化 XML 文件的配置,愈来愈多的XML文件采用属性而非子元素配置信息。
•Spring 从 2.5版本开始引入了一个新的p命名空间,能够经过<bean>元素属性的方式配置Bean的属性。
•使用 p 命名空间后,基于XML的配置方式将进一步简化

Spring IOC 容器能够自动装配 Bean. 须要作的仅仅是在 <bean> 的 autowire 属性里指定自动装配的模式
byType(根据类型自动装配): 若 IOC 容器中有多个与目标 Bean 类型一致的 Bean. 在这种状况下, Spring 将没法断定哪一个 Bean 最合适该属性, 因此不能执行自动装配.
byName(根据名称自动装配): 必须将目标 Bean 的名称和属性名设置的彻底相同.
constructor(经过构造器自动装配): 当 Bean 中存在多个构造器时, 此种自动装配方式将会很复杂. 不推荐使用
在 Bean 配置文件里设置 autowire 属性进行自动装配将会装配 Bean 的全部属性. 然而, 若只但愿装配个别属性时, autowire 属性就不够灵活了. 
autowire 属性要么根据类型自动装配, 要么根据名称自动装配, 不能二者兼而有之.
通常状况下,在实际的项目中不多使用自动装配功能,由于和自动装配功能所带来的好处比起来,明确清晰的配置文档更有说服力一些
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="address" class="com.xiya.spring.beans.autowire.Address" p:city="beijing" p:street="dawanglu"/>
    <bean id="car" class="com.xiya.spring.beans.autowire.Car" p:brand="Audi" p:price="400000"/>
    <bean id="person" class="com.xiya.spring.beans.autowire.Person" p:name="lgh" p:address-ref="address" p:car-ref="car"/>
    <!--
        可使用autowire属性指定自动装配的方式
        byName根据bean的名字和当前beansetter风格的属性名进行自动装配,若没有匹配的,则不进行装配。
        byType根据bean的类型和当前bean的属性的类型进行自动装配。若IOC容器由1个以上的配型匹配,则抛异常。
    -->
    <!--<bean id="person" class="com.xiya.spring.beans.autowire.Person" p:name="autowire" autowire="byType"/>-->
</beans>
Spring 容许继承 bean 的配置, 被继承的 bean 称为父 bean. 继承这个父 Bean 的 Bean 称为子 Bean
子 Bean 从父 Bean 中继承配置, 包括 Bean 的属性配置
子 Bean 也能够覆盖从父 Bean 继承过来的配置
父 Bean 能够做为配置模板, 也能够做为 Bean 实例. 若只想把父 Bean 做为模板, 能够设置 <bean> 的abstract 属性为 true, 这样 Spring 将不会实例化这个 Bean
并非 <bean> 元素里的全部属性都会被继承. 好比: autowire, abstract 等.
也能够忽略父 Bean 的 class 属性, 让子 Bean 指定本身的类, 而共享相同的属性配置. 但此时 abstract 必须设为 true
<!--
    抽象beanbeanabstract属性为truebean,它不能被IOC容器实例化,只能被继承
    若某一个beanclass属性没有指定,则该bean必须是一个抽象bean
    -->
<bean id="address" class="com.xiya.spring.beans.autowire.Address"
      p:city="beijing" p:street="dawanglu" abstract="false"/>

<!--bean配置的继承:使用beanparent属性指定继承哪一个bean的配置-->
<bean id="address2" parent="address" p:street="wudaokou "/>
Spring 容许用户经过 depends-on 属性设定 Bean 前置依赖的Bean,前置依赖的 Bean 会在本 Bean 实例化以前建立好
若是前置依赖于多个 Bean,则能够经过逗号,空格或的方式配置 Bean 的名称
<!--要求在配置bean时,必须有一个关联的car,或者说person这个bean依赖于car这个bean-->
<bean id="person" class="com.xiya.spring.beans.autowire.Person"
      p:name="lgh" p:address-ref="address2" depends-on="car"/>
相关文章
相关标签/搜索