[toc]
bean在定义的时候可以指定范围,spring提供了6种范围,其中4种是web应用环境相关的.
| 范围 | 说明 |
| --- | --- |
|singleton | 默认的,一个bean定义,只有一个对象实例 |
| prototype | 一个bean定义,可以有任意对象实例 |
| request | web环境下,一个bean定义,每次http请求生命周期,对应一个对象实例 |
| session | web环境下,一个bean定义,每次http会话生命周期,对应一个对象实例 |
| application | web环境下,一个bean定义,每次servlet上下文生命周期,对应一个对象实例 |
|websocket |web环境下,一个bean定义,每次websocket请求生命周期,对应一个对象实例 |
## 1.5.1. The singleton scope
单例范围
![singletone](https://box.kancloud.cn/a1e5415e8969df3b1fa9ab3cf9bfe7b7_800x398.png)
spring的单例不同于(Gang of Four (GoF) patterns book)设计模式的单例,设计模式的单例是通过编码控制一个`ClassLoader`只创建一个类的对象实例.spring的单例则是一个容器一个bean.
xml定义如下
~~~
<bean id="accountService" class="com.foo.DefaultAccountService"/>
<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.foo.DefaultAccountService" scope="singleton"/>
~~~
## 1.5.2. The prototype scope
原型范围,每次注入其他bean或者调用`getBean()`方法,获取的都是新的对象实例
一般的规则是,有状态的bean使用原型范围,无状态的bean使用单例范围
![prototype](https://box.kancloud.cn/73bdfe9ece22e1ae54cfe4282664f27f_800x397.png)
xml定义如下:
~~~
<bean id="accountService" class="com.foo.DefaultAccountService" scope="prototype"/>
~~~
## 1.5.3. Singleton beans with prototype-bean dependencies
如果在单例范围的bean中注入原型范围的bean,考虑注入是发生在实例化过程中的,因此,在实例化单例范围的bean时,也只有一次注入的原型范围的bean机会,所以,单例bean中的原型,其实是同一个对象,
如果想在单例bean中,每次使用的原型bean都是新的,则参考[method injection](https://docs.spring.io/spring/docs/5.0.7.RELEASE/spring-framework-reference/core.html#beans-factory-method-injection)
## 1.5.4. Request, session, application, and WebSocket scopes
`request, session, application`, and` websocket`这几个范围只能在web环境中使用,例如`XmlWebApplicationContext`,如果使用其他非web环境的上下文,如`ClassPathXmlApplicationContext`,则会发生异常`IllegalStateException`
### Initial web configuration
为了能正常使用web环境下的几个范围,需要做一些初始化(单例和原型的并不需要),不同的环境有不同的初始化方式
如果是通过spring web mvc访问bean,请求会经过spring内部的`DispatcherServlet`处理,不再需要其他设置,`DispatcherServlet`已经暴露了所有的相关状态.
如果使用servlet2.5 web容器,请求会经过spring 外部的`DispatcherServlet`(例如JSF,Struts),需要注册`org.springframework.web.context.request.RequestContextListener ServletRequestListener`,对于servlet3.0,可以编码实现`WebApplicationInitializer `接口.其他,或更老的容器,添加下面的声明到`web.xm`l文件
~~~
<web-app>
...
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
...
</web-app>
~~~
TODO
- 正确打开本书的姿势
- 第一部分 Core
- 1. Ioc container
- 1.1. Introduction to the Spring IoC container and beans
- 1.2. Container overview
- 1.2.1. Configuration metadata
- 1.2.2. Instantiating a container
- 1.2.3. Using the container
- 1.3. Bean overview
- 1.3.1. Naming beans
- 1.3.2. Instantiating beans
- 1.4. Dependencies
- 1.4.1. Dependency Injection
- 1.4.2. Dependencies and configuration in detail
- 1.4.3. Using depends-on
- 1.4.4. Lazy-initialized beans
- 1.4.5. Autowiring collaborators
- 1.4.6. Method injection
- 1.5 Bean Scopes
- 1.6. Customizing the nature of a bean TODO
- 1.7. Bean definition inheritance TODO
- 1.8. Container Extension Points TODO
- 1.9. Annotation-based container configuration
- 1.9.1. @Required
- 1.9.2. @Autowired
- 1.9.3. Fine-tuning annotation-based autowiring with @Primary
- 1.9.4. Fine-tuning annotation-based autowiring with qualifiers TODO
- 1.9.5. Using generics as autowiring qualifiers TODO
- 1.9.6. CustomAutowireConfigurer TODO
- 1.10. Classpath scanning and managed components
- 1.10.1. @Component and further stereotype annotations
- 1.11. Using JSR 330 Standard Annotations TODO
- 1.12. Java-based container configuration
- 1.12.1. Basic concepts: @Bean and @Configuration
- 1.12.2. Instantiating the Spring container using AnnotationConfigApplicationContext
- 2. Resources
- 2.1. Introduction
- 2.2. The Resource interface
- 2.3. Built-in Resource implementations
- 2.3.1. UrlResource
- 2.3.2. ClassPathResource
- 2.3.3. FileSystemResource
- 2.3.4. ServletContextResource
- 2.3.5. InputStreamResource
- 2.3.6. ByteArrayResource
- 2.4. The ResourceLoader
- 2.5. The ResourceLoaderAware interface
- 2.6. Resources as dependencies
- 2.7. Application contexts and Resource paths
- 2.7.1. Constructing application contexts
- 2.7.2. Wildcards in application context constructor resource paths
- 2.7.3. FileSystemResource caveats
- 3. Validation, Data Binding, and Type Conversion
- 4. Spring Expression Language (SpEL)
- 5. Aspect Oriented Programming with Spring
- 5.1. Introduction
- 5.1.1. AOP concepts
- 5.1.2. Spring AOP capabilities and goals
- 5.1.3. AOP Proxies
- 5.2. @AspectJ support
- 5.2.1. Enabling @AspectJ Support
- 5.2.2. Declaring an aspect
- 5.2.3. Declaring a pointcut
- 5.2.4. Declaring advice
- 5.2.5. Introductions TODO
- 5.2.6. Aspect instantiation models TODO
- 5.2.7. Example
- 5.3. Schema-based AOP support TODO
- 5.4. Choosing which AOP declaration style to use TODO
- 5.5. Mixing aspect types TODO
- 5.6. Proxying mechanisms
- 5.6.1. Understanding AOP proxies
- 5.7. Programmatic creation of @AspectJ Proxies
- 5.8. Using AspectJ with Spring applications
- 5.8.1. Using AspectJ to dependency inject domain objects with Spring
- 5.8.2. Other Spring aspects for AspectJ
- 第二部分 Testing
- 第三部分 Data Access
- 1. Transaction Management
- 1.1. Introduction to Spring Framework transaction management
- 1.2 Advantages of the Spring Framework’s transaction support model
- 1.2.1. Global transactions
- 1.2.2. Local transactions
- 1.2.3. Spring Framework’s consistent programming model
- 1.3. Understanding the Spring Framework transaction abstraction
- 1.4. Synchronizing resources with transactions
- 1.4.1. High-level synchronization approach
- 1.4.2. Low-level synchronization approach
- 1.4.3. TransactionAwareDataSourceProxy
- 1.5. Declarative transaction management
- 1.5.1. Understanding the Spring Framework’s declarative transaction implementation
- 1.5.2. Example of declarative transaction implementation
- 1.5.3. Rolling back a declarative transaction
- 1.5.4. Configuring different transactional semantics for different beans
- 1.5.5. tx:advice元素的 settings
- 1.5.6. Using @Transactional
- 1.5.7. Transaction propagation
- 1.5.8. Advising transactional operations
- 1.5.9. Using @Transactional with AspectJ TODO
- 第四部分 web servlet
- 第五部分 Web Reactive
- 第六部分 Integration
- 第七部分 Languages