spring beans

示例类:

public classJugglerimplementsPerformer{
privateintbeanBags=3;
public Juggler(){
}
public Juggler(intbeanBags){
this.beanBags=beanBags;
}
public voidperform()throwsPerformanceException{
System.out.println("JUGGLING"+beanBags+"BEANBAGS");
}
}

定义bean:

<bean id="duke" class="com.springinaction.springidol.Juggler"/>

带构造函数参数的bean定义:

<bean id="duke"
class="com.springinaction.springidol.Juggler">
<constructor-argvalue="15"/>
</bean>


如果构造参数包含对象,那么

1. 构造函数的对象入口必须是interface

2. 在xml里面定义这个要传入构造的实现bean

3. 按照如下方法定义:

<bean id="sonnet29" class="com.springinaction.springidol.Sonnet29"/>

<bean id="poeticDuke"
class="com.springinaction.springidol.PoeticJuggler">
<constructor-argvalue="15"/>
<constructor-argref="sonnet29"/>
</bean>

这个适应于构造:

public PoeticJuggler(intbeanBags,Poempoem){
super(beanBags);
this.poem=poem;
}

以上配置,对应与一下老式代码:

Poem sonnet29=newSonnet29();
Performerduke=newPoeticJuggler(15,sonnet29);


如果一个bean没有public的构造,即不允许new 怎么办?

比如单例,不能new ,只能通过static getinstance,那么,bean需要如下配置:

<bean id="theStage"
class="com.springinaction.springidol.Stage"
factory-method="getInstance" />

factory-method 就是解决之道


其他地方使用这个bean都使用ServiceTest2

加载执行bean:

ApplicationContextctx=newClassPathXmlApplicationContext("com/springinaction/springidol/spring-idol.xml");
Performerperformer=(Performer)ctx.getBean("duke");
performer.perform();


Bean Scoping 生存周期

spring默认会尽量使用同一个实例提供给bean的使用者,但是这个可以通过配置来实现指定:

scpoe

<bean id="ticket"
class="com.springinaction.springidol.Ticket"scope="prototype"/>

以上配置会导致每次获取ticket都会new 一个新的实例

下面列举出所有的scope:

1. singleton   这个是默认的,确保单例(?)

2. prototype  每次使用都创建新的实例

3. request 确保在http的一个request里面,得到的是同一个,但是有个前提,必须Only valid when used with a web-capable Spring context (such as with Spring MVC).

4. session 同上,确保在一个session里面唯一

5. global-session    Scopes a bean definition to a global HTTP session. Only valid when used in a portlet context.


值得注意,spring的singleton不同于java的,spring的局限于一个context里面,而不同的可以独立创建


BEAN的出入口接口定义:

spring支持给于一个bean定义其创建和消亡的回调接口,定义如下:

<bean id="auditorium"
class="com.springinaction.springidol.Auditorium"
init-method="turnOnLights"
destroy-method="turnOffLights"/>

开灯,关灯就是2个public函数,他们将在对应的时机被调用

当bean被实例化后,init-menthod立即被调用

当bean的实例被容器清理出来前,destroy-menthod被立即调用

当然,在一个容器内,可以设置默认的init和destroy方法,设置办法如下:

<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-3.0.xsd"
default-init-method="turnOnLights"
default-destroy-method="turnOffLights">...
</beans>

如果下面定义的bean存在turnOnLights,就会被调用,对于没有写这2个方法的bean,什么也不会发生

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值