In Spring, objects aren’t responsible for finding or creating the other objects that they need to do their jobs. Instead, they’re given references to the objects that they collaborate with by the container.

Spring is a container-based framework. But if you don’t configure Spring, then it’s an empty container and doesn’t serve much purpose.

1.通过构造函数:定义一个bean

  • <bean id="duke" class="com.springinaction.springidol.Juggler" /> //使用默认构造函数
  • 指明构造函数:参数列表顺序严格按照构造函数中的顺序
<bean id="duke"class="com.springinaction.springidol.Juggler"><constructor-arg value="15" />
</bean>
  • <bean id="poeticDuke"
    class="com.springinaction.springidol.PoeticJuggler"><constructor-arg value="15" /><constructor-arg ref="sonnet29" />
    </bean>

2.通过工厂方法:定义一个bean

  • <bean id="theStage" class="com.springinaction.springidol.Stage"factory-method="getInstance" />

3.bean作用域:

  • singleton:单例,默认的类型
  • prototype:每次需要时生成新的
  • request:
  • session:
  • global-session:
  • <bean id="ticket"
    class="com.springinaction.springidol.Ticket" scope="prototype" />

4.bean初始化:

  • init-method
  • 实现 InitializingBean 接口
    public interface InitializingBean
    {void afterPropertiesSet() throws Exception;
    }

5.bean销毁:

  • destroy-method
  • 实现DisposableBean 接口

6.定义全局的初始化和销毁函数:在Beans标签中定义

  • default-init-method
  • default-destroy-method

7.注入属性

  • 注入简单的值:可以是String,数值类型:int、float、double,布尔值;注入的方式都是以字符串的形式,spring会根据属性的类型,转换成相应的类型
  • <property name="song" value="Jingle Bells" />
    <property name="age" value="37" />
  • 注入bean:
    <property name="instrument" ref="saxophone" />
  • 注入内部bean:
    <property name="instrument"><bean class="org.springinaction.springidol.Saxophone" />
    </property>

    在构造函数中也可以使用:顺序还是依据构造函数定义的来

    <constructor-arg><bean class="com.springinaction.springidol.Sonnet29" />
    </constructor-arg>
  • 注入集合

  • list和set可以是任何collection的实现类,甚至是 数组;

    <property name="instruments"><list><ref bean="guitar" /><ref bean="cymbal" /><ref bean="harmonica" /></list>
    </property><map><entry key="GUITAR" value-ref="guitar" /><entry key="CYMBAL" value-ref="cymbal" /><entry key="HARMONICA" value-ref="harmonica" />
    </map><property name="instruments"><props><prop key="GUITAR">STRUM STRUM STRUM</prop><prop key="CYMBAL">CRASH CRASH CRASH</prop><prop key="HARMONICA">HUM HUM HUM</prop></props>
    </property>
  • 注入null值:
    <property name="someNonNullProperty"><null/></property>

8.p 命名空间

  • URI :http://www.springframework.org/schema/p ;xmlns:p="http://www.springframework.org/schema/p",看例子:

    <bean id="kenny" class="com.springinaction.springidol.Instrumentalist"p:song = "Jingle Bells"p:instrument-ref = "saxophone" />

spring wiring beans相关推荐

  1. Spring精华问答 | 什么是Spring inner beans?

    Spring框架是一个开源的Java平台,它提供了非常容易,非常迅速地开发健壮的Java应用程序的全面的基础设施支持.今天就让我们来看看关于Spring的精华问答吧. 1 Q:请解释Spring Be ...

  2. Spring Auto-Wiring Beans

    In Spring framework, you can wire beans automatically with auto-wiring feature. To enable it, just d ...

  3. java+spring+mysql配置_用spring的beans配置mysql数据库

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www. ...

  4. Spring的beans标签下可以有其他标签

    以前有对xsd(也就是schema文件)小做研究,有个小困惑,就是我们定义的元素只能使用定义的哪一些标签,比如<beans>下面就只能有自定义的哪一些,那为什么在引入<context ...

  5. Spring之beans详解

    首先,抛出一个问题,什么是 spring bean? 1) 它们是构成用户应用程序主干的对象. 2) Bean 由 Spring IoC 容器管理. 3) 它们由 Spring IoC 容器实例化,配 ...

  6. Spring配置文件beans.xml头部配置解释

    比如一个标准的beans.xml文件如下所示: <?xml version="1.0" encoding="UTF-8"?> <beans x ...

  7. 什么是Spring inner beans?

    在Spring 框架中,无论何时bean 被使用时,当仅被调用了一个属性.一个明智的做法是将这个bean声明为内部bean.内部bean 可以用setter 注入"属性"和构造方法 ...

  8. 标签系列一:spring 中beans解释以及beans标签里面的属性

    一.beans解释: 英文解释:The top level (typically root) element. Allows the definition of default values for ...

  9. 【Core Spring】二、装配beans

    在Spring中,对象不负责寻找和创建它们需要的其他对象.创建两个应用对象之间关联的动作是依赖注入的核心功能,通常称为装配. 创建beans和构建它们之间的关系是Spring的责任,但是告诉Sprin ...

最新文章

  1. LeetCode简单题之学生分数的最小差值
  2. 如何挖掘网站的核心关键词?
  3. 休息五分钟,学几个bash快捷键
  4. 超图桌面版制作分段专题图学习
  5. 【python图像处理】图像的增强(ImageEnhance类详解)
  6. JFinal开发环境搭建,JFinal开发案例
  7. python中main的作用_浅析python 中__name__ = '__main__' 的作用
  8. docker 部署java_使用Java EE 7,WildFly和Docker进行持续部署–(第1部分)
  9. linux查看目录下文件个球,球服务翻译……只找到以前版本的。新版的服务名称改了……...
  10. vmware workstation不可恢复错误:(vthread-7)
  11. JEPLUS之APP自定义插件——JEPLUS软件快速开发平台
  12. excel求回归直线方程的公式,excel2007用于拟合数据曲线方程,回归分析,趋势线...
  13. wxpython多个面板_wxpython笔记:Wxpython pannel切换
  14. 二叉树--二叉平衡树
  15. 串口控制器,电平脉冲触发,顺序轮换,间歇轮换,电磁阀继电器流水,8路,16路,32路
  16. 301、404、200、304、500等HTTP状态,代表什么意思?
  17. 一个总是令人记不住的vectorvectorPoint
  18. 最后完美解决pip没法用的问题
  19. Android系统的Ashmem匿名共享内存子系统分析(4)- Ashmem子系统的 Java访问接口
  20. 太原理工大学计算机院招生网,相洁-太原理工大学信息与计算机学院

热门文章

  1. QT学习(第一部分)
  2. 2022年学web前端还有出路吗?
  3. 我观察14年才发现,那些很努力却没成就的人都有一个特点
  4. 统计学习方法|决策树原理剖析及实现
  5. Spring源码深度解析(郝佳)-学习-RMI使用及Spring源码解读
  6. 用dw制作php网站模板,dw创建站点;如何用DW制作模板?
  7. 网络虚拟化技术要点及实践
  8. 黑马程序员------毕老师视频笔记第18-21天------IO输入与输出(4)
  9. 黑龙江中医药大学本科毕业论文答辩和论文选题PPT模板
  10. 老闪创业那些事儿(42)——产品技术规划通晒(下)