spring笔记2 bean的配置

2016年1月29日

9:36

spring笔记 bean的配置

在SpringIOC读取bean配置创建bean实例之前,必须对它进行实例化,spring提供了两种类型的ioc容器实现:

    -BeanFactory是spring框架的基础设施,面向spring本身

    -ApplicationContext面向使用spring框架的开发者(基本只会用到这种方式,不会用到BeanFactory)

    无论使用的是哪种方法,配置文件时都是相同的

 

    bean配置的具体属性:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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=”helloWorld” class=”com.firefly.spring.beans.HelloWorld”>
        <property name=”name” value=”Spring”></property>
    </bean>
 
    <bean id=”Phone1” class=”com.firefly.spring.beans.Phone”>
            <constructor-arg value=”xiaomi” index=”0”></constructor-arg>
            <constructor-arg value=”5.5” type=”double”></constructor-arg>
    </bean>
 
    <bean id=”Phone2” class=”com.firefly.spring.beans.Phone”>
            <constructor-arg value=”iphone” index=”0”></constructor-arg>
            <constructor-arg value=”5500” type=”int”></constructor-arg>
    </bean>
 
</beans>

    调用bean来得到实例的代码为:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.firefly.spring.beans;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class main {
 
    public static voidmain(String[] args){
        ApplicationContext ctx = new ClassPathXmlApplicationContext(“applicationContext.xml”);
        HelloWorld helloWorld = (HelloWorld)ctx.getBean(“helloWorld”);
        helloWorld.hello();
        Phone phone = (Phone)ctx.getBean(“Phone1”);
        System.out.println(phone);
        phone = (Phone)ctx.getBean(“Phone2”);
        System.out.println(phone);
    }
 
}

    id是为了调用的时候进行区分

    property是给实体类的成员赋值,name对应成员名,value是所需要的值,这种赋值方法必须要有对应的set方法在实体类中。

    constructor-arg是用于对应构造函数的赋值,用type属性区分不同的构造函数。

    

1
2
3
4
5
6
<bean id=”Phone1” class=”com.firefly.spring.beans.Phone”>
            <constructor-arg type=”java.lang.String”>
                <value><![CDATA[<xiaomi>]]></value>
            </constructor-arg>
            <constructor-arg value=”5.5” type=”double”></constructor-arg>
</bean>

    如果赋值字段中包含了特殊字符,则用【CDATA【所需要的值】】包裹起来

 

1
2
3
4
<bean id=”man” class=”com.firefly.spring.beans.Man”>
        <property name=”name” value=””></property>
        <property name=”phone” ref=”Phone2”></property>
</bean>

    ref属性可以实现bean之间的相互引用。

 

 

1
2
3
4
5
6
7
8
9
<bean id=”man1” class=”com.firefly.spring.beans.Man”>
        <property name=”name” value=”firefly”></property>
        <property name=”phone”>
            <bean class=”com.firefly.spring.beans.Phone”>
                <constructor-arg><null/></constructor-arg>
                <constructor-arg value=”2000” type=”int”></constructor-arg>
            </bean>
        </property>
</bean>

    以上为内部bean,为赋值null的专用方法。

 

 

1
2
3
4
5
6
7
8
9
<bean id=”man2” class=”com.firefly.spring.beans.collection.Man”>
        <property name=”name” value=”firefly”></property>
        <property name=”phone”>
            <list>
                <ref bean=”Phone1”/>
                <ref bean=”Phone2”/>
            </list>
        </property>
</bean>

    配置当成员变量为集合时。

 

 

        对map的配置

1
2
3
4
5
6
7
8
9
<util:list id=”phones”>
    <ref bean=”Phone1”/>
    <ref bean=”Phone2”/>
</util:list>
 
<bean id=”man3” class=”com.firefly.spring.beans.collection.Man”>
    <property name=”name” value=”firefly”></property>
    <property name=”phone” ref=”phones”></property>
</bean>

    用util标签将对集合的配置独立出来,以便于多个bean引用,要导入util命名空间包。

 

 

1
2
3
4
<!– 通过P命名空间进行配置 –>
    <bean id=”man4” class=”com.firefly.spring.beans.collection.Man”
        p:name=”fire” p:phone-ref=”phones”
    ></bean>

        通过P命名空间进行配置,要先导入P命名空间包。

 

已使用 Microsoft OneNote 2016 创建。