命名空间表
aop |
Provides elements for declaring aspects and for automatically proxying @AspectJannotated classes as Spring aspects. |
beans |
The core primitive Spring namespace, enabling declaration of beans and how they |
context |
Comes with elements for configuring the Spring application context, including the abil- |
jee |
Offers integration with Java EE APIs such as JNDI and EJB. |
jms |
Provides configuration elements for declaring message-driven POJOs. |
lang |
Enables declaration of beans that are implemented as Groovy, JRuby, or BeanShell |
mvc |
Enables Spring MVC capabilities such as annotation-oriented controllers, view control- |
oxm |
Supports configuration of Spring’s object-to-XML mapping facilities. |
tx |
Provides for declarative transaction configuration. |
util |
A miscellaneous selection of utility elements. Includes the ability to declare collec- |
简单Bean配置
基本配置
<bean id="sonnet29" class="ch2_idol.Sonnet29"/>
构造器注入
<bean id="poeticJuggler" class="ch2_idol.PoeticJuggler">
<constructor-arg value="15"/>
<constructor-arg ref="sonnet29"/>
</bean>
静态方法注入
<bean id="stage" class="ch2_idol.Stage" factory-method="getInstance"/>
scoping方式默认是单例singletons.
<bean id="ticket" class="com.springinaction.springidol.Ticket" scope="prototype"/>
singleton | Scopes the bean definition to a single instance per Spring container (default). |
prototype | Allows a bean to be instantiated any number of times (once per use). |
request | Scopes a bean definition to an HTTP request. Only valid when used with a web-capable Spring context (such as with Spring MVC). |
session | Scopes a bean definition to an HTTP session. Only valid when used with a web-capable Spring context (such as with Spring MVC). |
global-session | Scopes a bean definition to a global HTTP session. Only valid when used in a portlet context. |
初始化bean调用方法,销毁方法
<bean id="auditorium" class="ch2_idol.Auditorium" init-method="turnOnLights" destroy-method="turnOffLights"/>
也可以不配置,但实现InitializingBean ,DisposableBean接口
也可以在头部设置 default-init-method="turnOnLights" default-destroy-method="turnOffLights"所有bean初始化时调用,除非它有
属性注入
<bean id="instrumentalist" class="ch2_idol.Instrumentalist" >
<property name="song" value="JingleBells"></property>
<property name="instrument" ref="saxophone"></property>
</bean>
内部类
<bean id="kenny" class="com.springinaction.springidol.Instrumentalist">
<property name="song"value="JingleBells"/>
<property name="instrument">
<bean class="org.springinaction.springidol.Saxophone"/>
</property>
</bean>
p标记
命名空间xmlns:p="http://www.springframework.org/schema/p
<bean id="kenny" class="com.springinaction.springidol.Instrumentalist"
p:song="JingleBells"
p:instrument-ref="saxophone"/>
集合
<list> | Wiring a list of values, allowing duplicates |
<set> | Wiring a set of values, ensuring no duplicates |
<map> | Wiring a collection of name-value pairs where name and value can be of any type |
<props> | Wiring a collection of name-value pairs where the name and value are both Strings |
<bean id="oneManBand1" class="ch2_idol.OneManBand">
<property name="instruments">
<list>
<ref bean="saxophone"/>
<ref bean="piano"/>
<ref bean="piano"/>
</list>
</property>
</bean>
<!-- 会去重 -->
<bean id="oneManBand" class="ch2_idol.OneManBand">
<property name="instruments">
<set>
<ref bean="saxophone"/>
<ref bean="piano"/>
<ref bean="piano"/>
</set>
</property>
</bean>
<property name="instruments">
<map>
<entry key="GUITAR" value-ref="guitar"/>
<entry key="CYMBAL" value-ref="cymbal"/>
<entry key="HARMONICA" value-ref="harmonica"/>
</map>
</property>
<property name="instruments">
<props>
<prop key="GUITAR">STRUMSTRUMSTRUM</prop>
<prop key="CYMBAL">CRASHCRASHCRASH</prop>
<prop key="HARMONICA">HUMHUMHUM</prop>
</props>
</property>
空标记
<property name="instruments"><null/></property>