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. string,char*及CString类型的相互转换

    首先先介绍一下什么是CString CString是MFC的字符串类,它不是基本类型,而是对字符串的封装,它是自适应的,在UNICODE环境下就是CStringW,在非UNICODE环境下就是CStr ...

  2. Web service 框架比较CXF xfire aisx2 aisx

    Web 服务框架.它还体现了从 Axis 1.x 系列获得的经验和最近两年在 Web 服务领域的发展.推出 Axis2 的主要原因之一是从速度和内存方面获得更好的性能——不过还添加了一些新特性和功能. ...

  3. Tomcat服务器端口的配置

    一.Tomcat服务器端口的配置 Tomcat的所有配置都放在conf文件夹之中,里面的server.xml文件是配置的核心文件. 如果想修改Tomcat服务器的启动端口,则可以在server.xml ...

  4. 学习blus老师js(2)--深入JavaScript

    1.函数传参 可变参(不定参):arguments 参数的个数可变,参数数组   例1.求和 求所有参数的和 <!DOCTYPE HTML> <html> <head&g ...

  5. HDU 3641 Pseudoprime numbers(快速幂)

    Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11336   Accepted: 4 ...

  6. Office 2016激活教程(附KMS激活软件)

    首先下载office 2016办公软件文件名:cn_office_professional_plus_2016_x86_x64_dvd_6969182.isoSHA1:277926A41B472EE3 ...

  7. CentOS–root密码忘记的解决办法

    一.重启系统,如图:GRUB: 在引导装载程序菜单上,用上下方向键选择你忘记密码的那个系统键入“e”  来进入编辑模式.   2.接下来你可以看到如下图所示的画面,然后你再用上下键选择最新的内核(这里 ...

  8. Group By 和Having总结

    1.Group By 概述 “Group By”从字面意义上理解就是根据“By”指定的规则对数据进行分组 所谓的分组就是将一个“数据集”划分成若干个“小区域”,然后针对若干个“小区域”进行数据处理. ...

  9. mac下的一个类似“_kbhit()”实现

    #include <sys/select.h> #include <termios.h> #include <sys/ioctl.h> int _kbhit() { ...

  10. 迷你MVVM框架 avalonjs 0.96发布

    本版本主要是性能优化与 fix BUG,改进如下: 处理notifySubscribers中的BUG,它在标准浏览器不会移除那些无用的视图刷新函数.详见这里 重构modelBindling.SELEC ...