命名空间表

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. .net 中实现php rawurlencode方法(RFC3986)

    在对接api时候,经常需要对字符串进行各种编码处理,系统可能是异构的,实现语言也就可能是不一样的.所以经常会使人犯一些2B的错误! 比如:php实现的api中用了rawurlencode,文档中写,需 ...

  2. uva 11077 置换

    /** 给定一个置换,看能不能存在一个置换A^2 = B 思路; 循环节长度为偶数n的置换只能由循环节长度为长度2*n 的置换A*A 而变得.所以只需求出循环节,看循环节长度为偶数的个数是否为偶数个即 ...

  3. Qt分析:Qt中的两种定时器(可是QObject为什么要提高定时器呢,没必要啊。。。)

    Qt有两种定时器,一种是QObject类的定时器,另一种是QTimer类的定时器.   (1)QObject类的定时器   QObject类提供了一个基本的定时器,通过函数startTimer()来启 ...

  4. perl学习(3) 列表

      列表或称为数组,和c语言中的数据类似,只是perl自己特殊的一些写法. 1.1.定义 一个列表或者数组可以包含数字,字符串,undef 值,或者任意不同类型的标量值的组合,但是这些元素的类型通常是 ...

  5. 宣布正式发布 Biz Talk Services、Azure Active Directory 和 Traffic Manager, 同时发布 Azure Active Directory 高级版预览

    除经济优势之外,云计算还在可转化为竞争优势的应用程序开发方面提供了更大的灵活性.我们很高兴看到每天创建的新 Windows Azure 订阅超过 1000 个,更令人兴奋的是,有一半客户使用价值更高的 ...

  6. IT第三天 - 数据类型、转换、Scanner使用

    IT第三天 上午 变量类型 1.6种数值类型:byte.short.int.long.float.double:其中byte是8个字节,short是16字节,int是32字节.long是64字节:日常 ...

  7. 解题报告 HDU1789 Doing Homework again

    Doing Homework again Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  8. Treasure Exploration(二分最大匹配+floyd)

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7455   Accepted: 3 ...

  9. Kali Linux 安全渗透教程&lt;第三更&gt;1.2 安全渗透所需工具

    了解了渗透測试的概念后.接下来就要学习进行渗透測试所使用的各种工具.在做渗透測试之前.须要先了解渗透所需的工具.渗透測试所需的工具如表1-1所看到的: 表1-1  渗透所需工具 splint unhi ...

  10. 【JavaScript】标签样式中多出了element.style

    是因为使用了js增加或修改了样式.