命名空间表

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

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>

Spring笔记 - Bean xml装配的更多相关文章

  1. Spring 之通过 XML 装配 bean

    1.关于 使用传统标签还是 c- p- 命名空间定义的标签, 我的观点是能用  c- p- 命名空间定义的标签 就不用 传统标签(这样会比较简洁... 2.强依赖使用构造器注入,可选性依赖使用属性注入 ...

  2. Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

    1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...

  3. Spring温故而知新 - bean的装配(续)

    按条件装配bean 就是当满足特定的条件时Spring容器才创建Bean,Spring中通过@Conditional注解来实现条件化配置bean package com.sl.ioc; import ...

  4. Spring温故而知新 - bean的装配

    Spring装配机制 Spring提供了三种主要的装配机制: 1:通过XML进行显示配置 2:通过Java代码显示配置 3:自动化装配 自动化装配 Spring中IOC容器分两个步骤来完成自动化装配: ...

  5. Spring 中Bean的装配方式

    最近又买了一本介绍SSM框架的书,是由黑马程序员编写的,书上讲的很好理解,边看边总结一下.主要总结一下bean的装配方式. Bean的装配可以理解为依赖系统注入,Bean的装配方式即Bean依赖注入的 ...

  6. Spring温故而知新 – bean的装配

    Spring装配机制 Spring提供了三种主要的装配机制: 1:通过XML进行显示配置 2:通过Java代码显示配置 3:自动化装配 自动化装配 Spring中IOC容器分两个步骤来完成自动化装配: ...

  7. Spring ( 三 ) Spring的Bean的装配与生命周期、专用测试

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.对象的生命周期 1.IOC之Bean的生命周期 创建带有生命周期方法的bean public cla ...

  8. java开发两年,连Spring中bean的装配都不知道?你怎么涨薪啊

    Spring 1.1.1.1 创建一个bean package com.zt.spring; public class MyBean { private String userName; privat ...

  9. [spring]spring的bean自动装配机制

    7.bean的自动装配 是spring满足bean依赖的一种方式 spring会在上下文中自动寻找,并自动给bean装配属性 spring的装配方式: (1)手动装配 在people类中依赖了cat和 ...

随机推荐

  1. /etc/group文件详解

    Linux /etc/group文件与/etc/passwd和/etc/shadow文件都是有关于系统管理员对用户和用户组管理时相关的文件.linux /etc/group文件是有关于系统管理员对用户 ...

  2. Oracle Hint用法总结

    1. /*+ALL_ROWS*/ 表明对语句块选择基于开销的优化方法,并获得最佳吞吐量,使资源消耗最小化. 例如: SELECT /*+ALL+_ROWS*/ EMP_NO,EMP_NAM,DAT_I ...

  3. JUnit4的使用2

    package com.imooc.test.aware; import org.junit.Test; import org.junit.runner.RunWith; import org.jun ...

  4. hibernate Criteria查询 2.3

    Criteria对象提供了一种面向对象的方式查询数据库.Criteria对象需要使用Session对象来获得一个Criteria对象表示对一个持久化类的查询 查询所有 Session session ...

  5. 浙江大学PAT上机题解析之2-11. 两个有序链表序列的合并

    已知两个非降序链表序列S1与S2,设计函数构造出S1与S2的并集新非降序链表S3. 输入格式说明: 输入分2行,分别在每行给出由若干个正整数构成的非降序序列,用-1表示序列的结尾(-1不属于这个序列) ...

  6. html = data.decode('gbk').encode('utf-8')

    html = data.decode('gbk').encode('utf-8')此处encode编码要与html文件内charset=utf-8的格式一致,如果不一致,浏览器打开乱码,文本编辑器正常 ...

  7. Android短彩信源码解析-短信发送流程(二)

    转载请注明出处:http://blog.csdn.net/droyon/article/details/11699935 2,短彩信发送framework逻辑 短信在SmsSingleRecipien ...

  8. Codeforces 57C Array dp暴力找到规律

    主题链接:点击打开链接 的非增量程序首先,计算, 如果不增加的节目数量x, 非减少一些方案是x 答案就是 2*x - n 仅仅需求得x就可以. 能够先写个n3的dp,然后发现规律是 C(n-1, 2* ...

  9. Eclipse快捷键 10个最有用的快捷键(转载)

    现在很多开发人员都在用eclipse.用开发工具,就是为了方便,方便你开发你的软件,方便你管理你的工程,而开发工具提供各种功能大部分会有对应的快捷键,下面就列出了eclipse的快捷键.   Ecli ...

  10. 字符设备驱动1:新的方式添加cdev + 在open函数中将文件私有数据指向设备结构体

    本例中,驱动入口处,使用cdev_add添加驱动,这点也可与字符设备驱动0:一个简单但完整的字符设备驱动程序对比一下. 另外主要讲xx_open实现文件私有数据指向设备结构体. 引子: 偶然看到,在j ...