命名空间表

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. 再谈协方差矩阵之主成分分析PCA

    上次那篇文章在理论层次介绍了下协方差矩阵,没准很多人觉得这东西用处不大,其实协方差矩阵在好多学科里都有很重要的作用,比如多维的正态分布,再比如今天我们今天的主角——主成分分析(Principal Co ...

  2. django中间件templates写法

    def templates_context_process(request): from django.contrib.sites.models import Site from accounts.m ...

  3. JS 移动动画

    function moveElement(elementId, final_x, final_y,interval) {            if (!document.getElementById ...

  4. Spring Boot特性(转)

    摘要: 1. SpringApplication SpringApplication 类是启动 Spring Boot 应用的入口类,你可以创建一个包含 main() 方法的类,来运行 SpringA ...

  5. cocos2d-x游戏开发系列教程-超级玛丽07-CMGameMap(六)-马里奥跳跃

    当w键按下时,马里奥执行跳跃动作 执行跳跃动作也是在MarioMove函数中调用的

  6. 网易新闻首页iOS

    // //  ViewController.m //  wyy // //  Copyright © 2016年 zm. All rights reserved. // #import "V ...

  7. Ultra-QuickSort(归并排序+离散化树状数组)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 50517   Accepted: 18534 ...

  8. SQL查询各阶段的统计信息

            我们经常会遇到各种分类统计问题,须要将这些结果一次显示出来.这次老师提出的要求是我想看60分下面多少人.60~70多少人,70~80多少人.80~90多少人,90~100多少人.他们曾 ...

  9. stm32基础入门

    1.开发工具,初学者建议MDK,后期ivr 2.寄存器开发or库 版本开发:先寄存器开发,后期两者结合: 3.软件仿真or开发板,先软件仿真,后期两者结合: 建立工程: 1.包含三部分:start.u ...

  10. UIBezierPath详解

    使用UIBezierPath类可以创建基于矢量的路径,这个类在UIKit中.此类是Core Graphics框架关于path的一个封装.使用此类可以定义简单的形状,如椭圆或者矩形,或者有多个直线和曲线 ...