一、bean的实例化方法有3种,

1、构造器实例化

2、静态工厂方法实例化

3、实例工厂方法实例化

二、用构造器来实例化

<bean id="ShunDao" class="test.FTPDAO"  >

id或者name用于指定bean的名称,class用于指定bean的类型

三、静态工厂方法实例化

<bean id="ShunDao" class="test.FTPDAO" factory-method="getInstance">

factory-method用于指定工厂中创建Bean对象的方法,必须static

四、实例工厂方法实例化

<bean id="cal1" class="java.util.Calendar" factory-method="getInstance"></bean>
<bean id="cal3" class="java.util.Date" factory-bean="cal1" factory-method="getTime"></bean>

------------------------------------>>>>>>>>>>>>>>>>>>>>>>>>>>

1、命名bean

每个bean都有一个或者多个的的标识符。这些标识符必须在加载他们的容器里边唯一。一个bean经常有且只有一个标识符,但是如果需要超过一个的名字,可以考虑额外的别名。

基于xml的配置源文件,你可以使用id或者name属性去指定bean的标识符,这里有个问题,如果是名字和ID的不一致,内部又是如何处理的?

如果你不想再另外再给他们定义一个别名,你可以指定name属性,利用逗号(,),分号(;)或者空格。spring3.1以前id属性是xsd:ID类型的,这个是唯一的,3.1以后,xml文件是xsd:string这个xml文件不校验,但是spring需要它唯一。

如果一个bean没有明确指定ID或者name,容器将为bean生成一个唯一的名字。但是,如果你想用ref来引用,就必须定义一个名字。

Motivations for not supplying a name are related to using inner beans and autowiring collaborators.

这句话怎么理解??!!!!!!!!

Bean 命名约定,和java一样,用一个小写字符,驼峰式

在一个bean的定义里边,你可以给bean起多于一个的名字,一个ID,多个名字,名字name和别名alias是等价的

2、

Instantiating beans

A bean definition essentially is a recipe for creating one or more objects. The container looks at the recipe for a named bean when asked, and uses the configuration metadata encapsulated by that bean definition to create (or acquire) an actual object.

If you use XML-based configuration metadata, you specify the type (or class) of object that is to be instantiated in the

.

Within the container itself, these bean definitions are represented as BeanDefinition objects, which contain (among other information) the following metadata:

A package-qualified class name: typically the actual implementation class of the bean being defined.
Bean behavioral configuration elements, which state how the bean should behave in the container (scope, lifecycle callbacks, and so forth).
References to other beans that are needed for the bean to do its work; these references are also called collaborators or dependencies.

合作和依赖有什么区别?
Other configuration settings to set in the newly created object, for example, the number of connections to use in a bean that manages a connection pool, or the size limit of the pool.
This metadata translates to a set of properties that make up each bean definition.

Table 5.1. The bean definition

Property

class Instantiating beans

name Naming beans

scope Bean scopes

constructor arguments Dependency injection

properties Dependency injection

autowiring mode Autowiring collaborators

lazy-initialization mode Lazy-initialized beans

initialization method Initialization callbacks

destruction method Destruction callbacks

In addition to bean definitions that contain information on how to create a specific bean, the ApplicationContext implementations also permit the registration of existing objects that are created outside the container, by users. This is done by accessing the ApplicationContext’s BeanFactory via the method getBeanFactory() which returns the BeanFactory implementation DefaultListableBeanFactory. DefaultListableBeanFactory supports this registration through the methods registerSingleton(..) and registerBeanDefinition(..). However, typical applications work solely with beans defined through metadata bean definitions.

Spring学习笔记之Bean的实例化的更多相关文章

  1. Spring学习笔记——02 Bean的命名及实例化

    一.Bean的命名 前一篇讲到IoC是一个管理Bean的容器,Bean多数情况下都是通过XML文件进行配置的,其中Bean的命名有以下几种方式,现在梳理一下. 1. 不指定id,只配置类名 <b ...

  2. Spring学习笔记(3)——Bean的注入方式

    依赖注入 依赖注入支持属性注入.构造函数注入.工厂注入. 属性注入: 属性注入即通过setXxx()方法注入Bean的属性值或依赖对象 属性注入要求Bean提供一个默认的构造函数(无参构造函数),并为 ...

  3. Spring学习笔记—装配Bean

    在Spring中,对象无需自己负责查找或创建与其关联的其他对象.相反,容器负责把需要相互协作的对象引用赋予各个对象.创建应用对象之间协作关系的行为通常称为装配(wiring),这也是依赖注入的本质. ...

  4. spring学习笔记之---bean管理

    bean管理(xml) (一)spring的工厂类 FileSystemXmlApplicationContext 读取磁盘配置文件 (二)bean实例化的三种方式 (1)使用类构造器实例化(默认无参 ...

  5. Spring学习笔记(2)——Bean的配置

    要使应用程序中的Spring容器成功启动,需要以下三个方面的条件都具备: 1.Spring框架的类包都已经放到应用程序的类路径下 2.应用程序为Spring提供完备的Bean配置信息 3.Bean的类 ...

  6. Spring学习笔记之bean配置

    1.命名bean 每个bean都有一个或者多个的的标识符.这些标识符必须在加载他们的容器里边唯一.一个bean经常有且只有一个标识符,但是如果需要超过一个的名字,可以考虑额外的别名. 基于xml的配置 ...

  7. Spring学习笔记--注入Bean属性

    这里通过一个MoonlightPoet类来演示了注入Bean属性property的效果. package com.moonlit.myspring; import java.util.List; im ...

  8. spring学习笔记之---bean属性注入

    bean属性注入 (一)构造方法的属性注入 1.Student.java package entity; public class Student { private String name; pri ...

  9. spring学习笔记之---bean管理的注解方式

    bean管理的注解方式 (一)使用注解定义bean (1)常用注解 (2)实例 1.在pom.xml中进行配置 <dependencies> <dependency> < ...

随机推荐

  1. iOS 的 Safari 文件上传功能详解

    iOS 6 给 Safari 浏览器带来的另外一个功能是文件上传,终于 Safari 终于支持 input 输入框的文件类型了,并且还支持 HTML媒体捕获(HTML Media Capture). ...

  2. 探索 Pexpect,第 1 部分:剖析 Pexpect

    Pexpect 是一个用来启动子程序并对其进行自动控制的 Python 模块. Pexpect 可以用来和像 ssh.ftp.passwd.telnet 等命令行程序进行自动交互.本文章介绍 Pexp ...

  3. J2EE 第二阶段项目之部署项目、分工安排

    SVN 先通过使用教程,和能够介绍了解svn. svn使用教程总结   ;   svn功能介绍. 分工安排:我的任务就是项目统计. 1 效益统计 1 教育效益统计表 (教育效益统计表,增,改,查看,查 ...

  4. easyui 上传文件代码

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.IO;usi ...

  5. LinuxShell脚本攻略--第三章 以文件之名

    生成任意大小的文件文件权限.所有权和粘滞位创建不可修改文件生成空白文件查找符号链接及其指向目标head 与 tail只列出目录的其他方法在命令行中用 pushd 和 popd 快速定位(cd -)统计 ...

  6. Graph-tool简介 - wiki

    graph-tool is a Python module for manipulation and statistical analysis of graphs[disambiguation nee ...

  7. Implicit Object in JSP

    Implicit Object Description request The HttpServletRequest object associated with the request. respo ...

  8. css读书笔记4:字体和文本

    字体属性网页中的字体有3个来源:1.用户机器中安装的字体:2.保存在第三方网站上的字体.可以使用link标签把它们链接到页面中:3.保存在子集的web服务器上的字体.可以使用@font-face规则随 ...

  9. Java位操作全面总结

    转载: Java位操作全面总结 在计算机中所有数据都是以二进制的形式储存的.位运算其实就是直接对在内存中的二进制数据进行操作,因此处理数据的速度非常快.在实际编程中,如果能巧妙运用位操作,完全可以达到 ...

  10. jmeter 实时搜索结果

    因为JMeter 2.13你可以得到实时搜索结果发送到后端通过 后端侦听器 使用潜在的任何后端(JDBC.JMS网络服务,€Š) 通过提供一个实现类 AbstractBackendListenerCl ...