Bean作用域
分类
| Scope | Description |
|---|---|
| singleton | (默认) 每个 Spring IoC 容器的单个 bean 定义范围限定为单个对象实例。 |
| prototype | 将单个 bean 定义的作用域限定为任意数量的对象实例。 |
| request | 将单个 bean 定义的范围限定为单个 HTTP 请求的生命周期。也就是说,每个 HTTP 请求都有一个在单个 bean 定义后面创建的 bean 实例。仅在可感知网络的 Spring ApplicationContext中有效。 |
| session | 将单个 bean 定义的范围限定为 HTTP Session的生命周期。仅在可感知网络的 Spring ApplicationContext上下文中有效。 |
| application | 将单个 bean 定义的范围限定为ServletContext的生命周期。仅在可感知网络的 Spring ApplicationContext上下文中有效。 |
| websocket | 将单个 bean 定义的范围限定为WebSocket的生命周期。仅在可感知网络的 Spring ApplicationContext上下文中有效。 |
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>
单例模式
每一次 get 时都返回同一个对象(hashCode相同)。

原型模式
每一次 get 时都返回一个新的对象(hashCode不同)。
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
自动装配
// 自动寻找id = set后参数名的对象装配<bean id="accountService" class="com.something.DefaultAccountService" autowire="byName"/>// 自动寻找属性相同的bean<bean id="accountService" class="com.something.DefaultAccountService" autowire="byType"/>
注意:自动装配时必须确保对应的bean是唯一的。
