bean 的各个属性
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 的各个属性的更多相关文章
- spring 的配置 bean>>property>>name属性
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Spring - bean的autowire属性(自动装配)
当我们要往一个bean的某个属性里注入另外一个bean,我们会使用<property> + <ref/>标签的形式.但是对于大型项目,假设有一个bean A被多个bean引用注 ...
- spring中bean的scope属性理解
bean的scope属性有prototype,singleton,request, session几个属性 spring和struts2整合的时候,struts2的action要配置成scope=&q ...
- Spring中bean标签的属性和值:
Spring中bean标签的属性和值: <bean name="user" class="com.pojo.User" init-method=" ...
- spring中bean的作用域属性singleton与prototype的区别
1.singleton 当一个bean的作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会 ...
- spring中bean的常用属性
一.scop scope用来配置bean对象是否是单例模式.单例模式是java的二十三种设置模式之一,指在这个项目运行过程中一 个类的对象只会实例化一次.一般,工厂类的对象都是单例模式.非单例模式叫多 ...
- spring练习,使用Eclipse搭建的Spring开发环境,使用set注入方式为Bean对象注入属性值并打印输出。
相关 知识 >>> 相关 练习 >>> 实现要求: 使用Eclipse搭建的Spring开发环境,使用set注入方式为Bean对象注入属性值并打印输出.要求如下: ...
- fastjson格式化bean的简易属性过滤器
fastjson的bean属性过滤器 有的时候,我们在接口开发时,一个完整的bean会包含很多属性,但是前端接口只需要其中的某几个属性时,应该在对json的返回要进行精简.下面直接看代码 packag ...
- bean中集合属性的配置
在实际的开发中,有的bean中会有集合属性,如下: package com.sevenhu.domain; import java.util.List; /** * Created by hu on ...
- Spring - bean的lazy-init属性(懒加载)
默认情况下,容器初始化的时候便会把bean实例化,通常这样做可以让一些配置或者bean实例化的异常在容器启动的时候就发现,而不是在N久之后.但有时候,我们希望某个可能不会用到但又不是100%不用的be ...
随机推荐
- linux nginx不区别大小写处理方法
# 把所有的目录及文件名全改成小写,注意之后新增目录及文件只使用小写命名 递归转换目录或文件名方法:python把指定目录下的递归所有目录和文件名转换成小写或大写(http://www.cnblogs ...
- Angular 安装
1.angular安装 npm install -g angular 2. 显示angular安装路径 npm config ls 3. angular 双向绑定 <!DOCTYPE html ...
- Window平台下React Native 开发环境搭建
1. 安装Node.js 2. 安装react-native-cli 命令行工具 npm install -g react-nativew-cli 3. 创建项目 $ react-native ini ...
- spring 配置文件中使用properties文件 配置
配置Bean载入properties文件: <bean id="propertyPlaceholderConfigurer" class="org.springfr ...
- Spring Mvc:用MultiPartFile上传单个文件,多个文件
1.单个文件上传步骤: 添加Apache文件上传jar包 首先需要下载两个apache上传文件的jar包,commons-fileupload-1.3.1jar,commons-io-2.4.jar ...
- Java-Runoob-高级教程-实例-环境设置实例:3.Java 实例 - 如何执行指定class文件目录(classpath)?
ylbtech-Java-Runoob-高级教程-实例-环境设置实例:3.Java 实例 - 如何执行指定class文件目录(classpath)? 1.返回顶部 1. Java 实例 - 如何执行指 ...
- Bootstrap-CL:进度条
ylbtech-Bootstrap-CL:进度条 1.返回顶部 1. Bootstrap 进度条 本章将讲解 Bootstrap 进度条.在本教程中,您将看到如何使用 Bootstrap 创建加载.重 ...
- [html][javascript]父子窗体传值
父窗体 <script type="text/javascript"> newwindow = window.open("b1.html",&quo ...
- php 数字格式化
php 数字格式化 1.位数不足前面补0 <?php for($i=1; $i<=17 ;$i++){ $var = sprintf("0%3d",$i); echo ...
- Django ORM-02
6.ForeignKey 相关操作 1.正向查找 正向查找:那么什么是正向查找,我们知道对于一对多或者多对一的情况,我们一般将ForeignKey设置在多的一边,比如我们的书籍与出版社一般是多对一的, ...