Spring笔记 - Bean xml装配

本文详细介绍了Spring框架中各种核心配置选项,包括不同命名空间的功能介绍、Bean的配置方式、依赖注入方法、作用域管理、集合类型配置及初始化与销毁等关键特性。

命名空间表

 

 

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
should be wired.

context

Comes with elements for configuring the Spring application context, including the abil-
ity to autodetect and autowire beans and injection of objects not directly managed by
Spring.

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
scripts.

mvc

Enables Spring MVC capabilities such as annotation-oriented controllers, view control-
lers, and interceptors.

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-
tions as beans and support for property placeholder elements.

 

 

简单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"/>

singletonScopes the bean definition to a single instance per Spring container (default).
prototypeAllows a bean to be instantiated any number of times (once per use).
requestScopes a bean definition to an HTTP request. Only valid when used with a
web-capable Spring context (such as with Spring MVC).
sessionScopes a bean definition to an HTTP session. Only valid when used with a
web-capable Spring context (such as with Spring MVC).
global-sessionScopes 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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值