7.10.7 Providing a scope for autodetected components
通常spring管理的组件,默认和最常用的作用域是singleton.然而,一些时候你需要一个不一样的作用域可以通过@Scope作用域指定。仅仅在注解里提供作用域的名字就可以了
@Scope("prototype")
@Repository
public class MovieFinderImpl implements MovieFinder{
}
提供一个自定义的作用域解决办法而不是依靠基于注解的机制,实现ScopeMetadataResolver接口,确保包含一个默认的无参构造器,然后提供一个fully-qualified class name当配置scanner的时候
@Configuration
@ComponentScan(basePackages="org.example", scopeResolver=MyScopeResolover.class)
public class AppConfig{
...
}
当使用确定的non-singleton 作用域时,提供一个代理是必须的
ScopedProxyMode.NO, ScopedProxyMode.INTERFACES, ScopedProxyMode.targetClass