Spring in action 学习(2)创建Bean

2种方式

-BeanFactory
    XMLBeanFactory(newFileSystemResource..)
-ApplicationContext
    ClassPathXmlApplicatioContext("foo.xml");
    FileSystemXmlApplicationContext("c:/foo.xml");

注入属性

通过构造函数
-<constructor-arg value="15">
-<constructor-arg ref="xxx">
set注入
-<property name="song" value="15">
-<property name="song" ref="xxx">

spring 装配支持的集合类型

- <list>
- <set>
- <map>
- <props> 键值对,不过都是string类型

对应于配置文件中,以instruments为例
<property name="instruments">
    <list>
        <ref bean="guitar"/>
        <ref bean="cymbal"/>
        <ref bean="harmonica"/>
    </list>
</property>

set类似

<map>
    <entry key="GUITAR" value-ref="guitar"/>
    <entry....>
</map>
entry:  key key-ref,  value value-ref.

<prop key="GUITAR">STRUM STRUM STRUM</prop>

设置属性值为空:
<property name="someNonNullProperty"><null/></property>

自动装配

byName

<bean id="kenny" class="xxx">
    <property name="instrument" ref="saxophone"/>
</bean>
<ben id="instrument" class="xxx">

则上面可变为:
<bean id="kenny" class="xxx" autoware="byName">

byType

constructor

autodetect

默认自动装配

<beans default-autowire="byName">
    ...

控制Bean创建

每次都创建一个新的bean

<bean id=".." calss="..."
    scope="prototype"/>

scope

singleton  :一个实例
prototype  :多个实例
request    :http请求
session    :http会话
global-session :全局http会话

单例类

factory-method="getInstance" />

初始化和销毁

<bean id="xxx" class="xxx"
    init-method="init"
    destroy-method="clean">

高级Bean装配

abstract="true"  为抽象类
<bean id="xxx" parent="xxx.class"> 声明该bean的父类是什么
可以覆盖相应属性值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值