四、Spring范围和自动装配
Spring Scopes and Autowiring - 章节
Scopes
5 scopes
适用于任何配置
- Singleton
- Prototype
仅在支持web的Spring项目中有效
- Request
- Session
- Global
Singleton - 单例scope
One instantiation
默认的bean scope
每个Spring容器一个实例
@Scope注解
需要 AOP jar
示例:
Singleton - Java配置
Prototype
每个请求
保证独特的
与单例相反
Prototype - Java配置
Web Scopes
Spring MVC知识
Request
Session
GlobalSession
Autowired - 自动装配
@ComponentScan({“com.*"})
@Bean
By Name Instance Type
@Autowired - Java配置
Stereotypes
@Component
@Repository
@Service
Stereotypes注解-Java配置
自动装配到构造函数上
总结:
Scopes
- Singleton
- Prototype
Web scopes
- Request
- Session
- GlobalSession
Autowiring