http://www.springframework.org/schema/beans/spring-beans.xsd

org.springframework.beans.factory.config.BeanDefinition

<xsd:attribute name="name" type="xsd:string">...</xsd:attribute>
<xsd:attribute name="class" type="xsd:string">...</xsd:attribute>
<xsd:attribute name="parent" type="xsd:string">...</xsd:attribute>
<xsd:attribute name="scope" type="xsd:string">...</xsd:attribute>
<xsd:attribute name="abstract" type="xsd:boolean">...</xsd:attribute>
<xsd:attribute name="lazy-init" default="default" type="defaultable-boolean">...</xsd:attribute>
<xsd:attribute name="autowire" default="default">...</xsd:attribute>
<xsd:attribute name="depends-on" type="xsd:string">...</xsd:attribute>
<xsd:attribute name="autowire-candidate" default="default" type="defaultable-boolean">...</xsd:attribute>
<xsd:attribute name="primary" type="xsd:boolean">...</xsd:attribute>
<xsd:attribute name="init-method" type="xsd:string">...</xsd:attribute>
<xsd:attribute name="destroy-method" type="xsd:string">...</xsd:attribute>
<xsd:attribute name="factory-method" type="xsd:string">...</xsd:attribute>
<xsd:attribute name="factory-bean" type="xsd:string">...</xsd:attribute>

什么是bean

Defines a single (usually named) bean. A bean definition may contain nested tags for constructor arguments, property values, lookup methods, and replaced methods. Mixing constructor injection and setter injection on the same bean is explicitly supported.

常用属性详解

name

Can be used to create one or more aliases illegal in an (XML) id. Multiple aliases can be separated by any number of spaces, commas, or semi-colons (or indeed any mixture of the three).

   <bean id="person" class="cn.zno.Person">
<property name="car" ref="a"></property>
</bean>
<bean name="a;b,c d" id="car" class="cn.zno.Car">
<property name="price" value="10000"></property>
</bean>

ref 可以使用任意别名或者id

定义多个别名时会实例化该bean(触发call)

class

The fully qualified name of the bean's class, except if it serves only as a parent definition for child bean definitions.

只有作为父bean时,才可以不指定class

指定class的方式有两种:1.通过class属性 2.通过parent属性

parent

The name of the parent bean definition. Will use the bean class of the parent if none is specified, but can also override it. In the latter case, the child bean class must be compatible with the parent, i.e. accept the parent's property values and constructor argument values, if any. A child bean definition will inherit constructor argument values, property values and method overrides from the parent, with the option to add new values. If init method, destroy method, factory bean and/or factory method are specified, they will override the corresponding parent settings. The remaining settings will always be taken from the child definition: depends on, autowire mode, scope, lazy init.

作为父bean可以指定class 可以不指定class;继承这个父bean的子bean,可以不指定class(继承父bean的),也可以指定class(override)

如果是后一种情况(override),需要匹配父类的构造参数和属性。父类有的setter 子类也必须有(类型可以不同,但可以自动类型转换)。

原则就是:子类没有的用父类的,子类有的用子类的。不包括:depends-on 、 autowire 、scope 、lazy-init ,这些值总是使用自己的(测试发现如果未指定,依旧使用父类的,而不使用自己的默认值。

scope

The scope of this bean: typically "singleton" (one shared instance, which will be returned by all calls to getBean with the given id), or "prototype" (independent instance resulting from each call to getBean). By default, a bean will be a singleton, unless the bean has a parent bean definition in which case it will inherit the parent's scope. Singletons are most commonly used, and are ideal for multi-threaded service objects. Further scopes, such as "request" or "session", might be supported by extended bean factories (e.g. in a web environment). Inner bean definitions inherit the singleton status of their containing bean definition, unless explicitly specified: The inner bean will be a singleton if the containing bean is a singleton, and a prototype if the containing bean has any other scope.

  • singleton 共享bean (默认)
  • prototype 独立bean
  • request
  • session

abstract

Is this bean "abstract", that is, not meant to be instantiated itself but rather just serving as parent for concrete child bean definitions? The default is "false". Specify "true" to tell the bean factory to not try to instantiate that particular bean in any case. Note: This attribute will not be inherited by child bean definitions. Hence, it needs to be specified per abstract bean definition.

默认不是抽象bean;抽象bean不能被实例化,只能被子类继承。

lazy-init

Indicates whether this bean is to be lazily initialized. If "false", it will be instantiated on startup by bean factories that perform eager initialization of singletons. The effective default is "false". Note: This attribute will not be inherited by child bean definitions. Hence, it needs to be specified per concrete bean definition. It can be shared through the 'default-lazy-init' attribute at the 'beans' level and potentially inherited from outer 'beans' defaults in case of nested 'beans' sections (e.g. with different profiles).

懒初始化,默认false ,即:非懒惰初始化。这是高效的,可以提前暴露错误(如果存在),如果是懒初始化,只有在call 时才初始化。

可以在 beans tag 里配置default-lazy-init ,这个值将作为lazy-init 的默认值(default),如果bean tag中指定了true or

false 则以bean 为准。

call的情景:1. name 属性定义多个别名 2. java类中使用bean

depends-on

The names of the beans that this bean depends on being initialized. The bean factory will guarantee that these beans get initialized before this bean. Note that dependencies are normally expressed through bean properties or constructor arguments. This property should just be necessary for other kinds of dependencies like statics (*ugh*) or database preparation on startup. Note: This attribute will not be inherited by child bean definitions. Hence, it needs to be specified per concrete bean definition.

初始化该bean之前必须先初始化谁(name or id 指定)。

init-method

The name of the custom initialization method to invoke after setting bean properties. The method must have no arguments, but may throw any exception. This is an alternative to implementing Spring's InitializingBean interface or marking a method with the PostConstruct annotation.

调用构造方法之后(new),调用setter之后。

destroy-method

The name of the custom destroy method to invoke on bean factory shutdown. The method must have no arguments, but may throw any exception. This is an alternative to implementing Spring's DisposableBean interface or the standard Java Closeable/AutoCloseable interface, or marking a method with the PreDestroy annotation. Note: Only invoked on beans whose lifecycle is under the full control of the factory - which is always the case for singletons, but not guaranteed for any other scope.

prototype 等不会调用该方法

singletons 会调用

不调用close() 方法则不会调用该方法

bean 的各个属性的更多相关文章

  1. spring 的配置 bean>>property>>name属性

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  2. Spring - bean的autowire属性(自动装配)

    当我们要往一个bean的某个属性里注入另外一个bean,我们会使用<property> + <ref/>标签的形式.但是对于大型项目,假设有一个bean A被多个bean引用注 ...

  3. spring中bean的scope属性理解

    bean的scope属性有prototype,singleton,request, session几个属性 spring和struts2整合的时候,struts2的action要配置成scope=&q ...

  4. Spring中bean标签的属性和值:

    Spring中bean标签的属性和值: <bean name="user" class="com.pojo.User" init-method=" ...

  5. spring中bean的作用域属性singleton与prototype的区别

    1.singleton 当一个bean的作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会 ...

  6. spring中bean的常用属性

    一.scop scope用来配置bean对象是否是单例模式.单例模式是java的二十三种设置模式之一,指在这个项目运行过程中一 个类的对象只会实例化一次.一般,工厂类的对象都是单例模式.非单例模式叫多 ...

  7. spring练习,使用Eclipse搭建的Spring开发环境,使用set注入方式为Bean对象注入属性值并打印输出。

    相关 知识 >>> 相关 练习 >>> 实现要求: 使用Eclipse搭建的Spring开发环境,使用set注入方式为Bean对象注入属性值并打印输出.要求如下: ...

  8. fastjson格式化bean的简易属性过滤器

    fastjson的bean属性过滤器 有的时候,我们在接口开发时,一个完整的bean会包含很多属性,但是前端接口只需要其中的某几个属性时,应该在对json的返回要进行精简.下面直接看代码 packag ...

  9. bean中集合属性的配置

    在实际的开发中,有的bean中会有集合属性,如下: package com.sevenhu.domain; import java.util.List; /** * Created by hu on ...

  10. Spring - bean的lazy-init属性(懒加载)

    默认情况下,容器初始化的时候便会把bean实例化,通常这样做可以让一些配置或者bean实例化的异常在容器启动的时候就发现,而不是在N久之后.但有时候,我们希望某个可能不会用到但又不是100%不用的be ...

随机推荐

  1. 解决Apache下生成静态页面乱码的问题

    我的空间存放在阿里云,服务器默认Apache编码设置为utf-8,而新的网站珠宝招聘网http://hr.izuans.com 采用GB2312编码,其他程序文件都OK,就是生成静态新闻页和其他单页面 ...

  2. 大数据框架hadoop的序列化机制

    Java内建序列化机制 在Windows系统上序列化的Java对象,可以在UNIX系统上被重建出来,不需要担心不同机器上的数据表示方法,也不需要担心字节排列次序. 在Java中,使一个类的实例可被序列 ...

  3. 解决 eclipse tomcat cannot create a server using the selected type

    解决的方法 1.退出eclipse: 2.打开 [工程目录下]/.metadata/.plugins/org.eclipse.core.runtime/.settings目录: 3.删除org.ecl ...

  4. java web 程序---购物车项目内容:

    1.项目介绍   典型电子商务系统(在线购物平台).模拟了当当系统部分功能.2.功能需求    1)用户管理模块(3天)user         实现登录.注册功能    2)产品浏览模块(2天)ma ...

  5. Train-Alypay-Cloud:分布式微服务中间件sofa 开发培训(第二次)

    ylbtech-Train-Alypay-Cloud:分布式微服务中间件sofa 开发培训(第二次) 1.返回顶部 1. 这是本次培训的内容,望各位提前配好环境.工具.2.6-2.7 我们在环球金融8 ...

  6. Web API 源码剖析之默认消息处理程序链--》路由分发器(HttpRoutingDispatcher)

    我们在上一节讲述了默认的DefaultServer(是一个类型为HttpServer的只读属性,详情请参考 Web API 源码剖析之全局配置).本节将讲述DefaultHandler(是一个Http ...

  7. 我的solr学习笔记--solr admin 页面 检索调试

    前言 Solr/Lucene是一个全文检索引擎,全文引擎和SQL引擎所不同的是强调部分相关度高的内容返回,而不是所有内容返回,所以部分内容包含在索引库中却无法命中是正常现象.      多数情况下我们 ...

  8. Navicat 入门使用方法

    Navicat 多重连接数据库的管理工具,支持连接到(MySQL.Oracle.PostgreSQL.SQLite .MariaDB )多类数据库,也支持多类数据库的管理和使用 1.Navicat 主 ...

  9. 有了 itchat, python 调用微信个人号从未如此简单(新增 py3 支持)

    itchat 是一个开源的微信个人号接口. 近期完成了 py3 与文档的完善,欢迎各位使用与测试. 使用不到三十行的代码,你就可以完成一个能够处理所有信息的微信机器人. 当然,该 api 的使用远不止 ...

  10. leetcode103

    class Solution { public: vector<vector<int>> zigzagLevelOrder(TreeNode* root) { vector&l ...