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() 方法则不会调用该方法