其实bean也是咱们最熟悉的,每注册一个bean就是给spring的IOC容器中添加一个对象,在spring初始化的时候就已经帮咱们建立好了,咱们须要用的时候直接调用就好了。web
几个重要的属性介绍:spring
id:bean的惟一标识符,也便是对象的变量名websocket
class:属性定义Bean的类型,并使用彻底限定的类名。session
name:功能至关于取别名(这里的name能够取多个别名)下面就是取多个别名的例子app
<bean id="user" class="com.xuan.pojo.User" name="user3,user4,user5,user6,user7">
<constructor-arg ref="student"></constructor-arg>
<constructor-arg ref="teacher"></constructor-arg>
</bean>
scope:bean的做用域socket
Scope | Description |
---|---|
singleton | (默认)为每一个Spring IoC容器将单个bean定义的做用域限定为单个对象实例。 |
prototype | Scopes a single bean definition to any number of object instances. |
request | 将单个bean定义的范围限定为单个HTTP请求的生命周期。也就是说,每一个HTTP请求都有一个在单个bean定义后面建立的bean实例。Only valid in the context of a web-aware Spring ApplicationContext . |
session | 将单个bean定义的范围限定为HTTP的生命周期Session 。Only valid in the context of a web-aware Spring ApplicationContext . |
application | 将单个bean定义的做用域限定为的生命周期ServletContext。 Only valid in the context of a web-aware Spring ApplicationContext . |
websocket | 将单个bean定义的做用域限定为的生命周期WebSocket 。 Only valid in the context of a web-aware Spring ApplicationContext . |
singleton(单例):容器只建立一个对象spa
prototype(原型):每一次从容器get的时候都建立一个新的对象prototype
怎么用?code
使用ApplicationContext的getBean方法里面传入别名或者自己的名字(bean指定的id属性)都是能够获取到制定的数据的。xml
<alias name="user" alias="user2"></alias>
何时用?
通常用于团队开发,多个配置文件经过导入的方式实现整合。
<import resource="applicationContext.xml"></import>