Spring in Action学习笔记-Bean组装

10月 1, 2014 |

1 spring的两大核心功能

依赖注入和aop ,作用是程序解耦
2 构造函数注入
<bean id="" class="">
<constructor-arg value="15"/>
<constructor-arg ref="sonnet29"/>
<!-- 注入内部类-->
<constructor-arg>
<bean? class="com.springinaction.springidol.Sonnet29"/>
</constructor-arg>
</bean>

3 工厂方法注入
<bean id="theStage"
class="com.springinaction.springidol.Stage"
factory-method="getInstance" />

4 bean 的范围scope
<bean id="ticket"
class="" scope="prototype"/>
singleton 一个容器一个实例
prototype 每次使用生成一个新的实例
request? 一个http request一个实例
session 一个http session一个实例

5 初始化和清理
1)使用bean的init-method和destroy-method属性
<bean id="auditorium"?? class=""
init-method="turnOnLights"
destroy-method="turnOffLights"/>
2)实现InitializingBean 和 DisposableBean 接口
InitializingBean.afterPropertiesSet()
DisposableBean.destroy()

3)使用beans 的default-init-method="init" 和 default-destroy-method="destroy" 属性。
指定默认的初始化和清理函数,如果某个bean没有声明该方法,什么也不做

6向bean 属性注入
<bean id="kenny" class="">
<!--注入简单值-->
<property name="song" value="JingleBells"/>
<!--注入引用-->
<property name="instrument" ref="saxophone"/>
<!--注入一个新产生的内部类(inner class)-->
<property name="instrument">
<bean?? class="org.springinaction.springidol.Saxophone"/>
</property>
</bean>

7,p命名空间
用于将<property>属性标签转化为p:lable属性。
<bean id="kenny" class="com.springinaction.springidol.Instrumentalist"
p:song="JingleBells"
p:instrument-ref="saxophone"/>

8,组装 集合

9,组装 空(null)
<property name="someNonNullProperty"><null/></property>,

10 SpEL
<property name="song" value="#{songSelector.selectSong()?.toUpper()Case()}"/>
?. NullException安全的访问
<property name="multiplier"value="#{T(java.lang.Math).PI}"/>
T()访问类的静态信息
<property name="cityNames"
value="#{cities.?[population gt 100000].![name]}"/>
.? 条件过滤
.!映射属性组装成一个新的Collection

<util:list>将bean组装成List对象。

 

Posted in: spring practise

Comments are closed.