[英]Prototype scoped bean with conditional annotation
我尝试在每个具有可能不同的实现的请求上重新加载bean。
在我的控制器中,我每次从ApplicationContext检索bean:
@Controller
public class LabelsController implements ApplicationContextAware {
private ApplicationContext applicationContext;
@RequestMapping("/...")
public ModelAndView labelConcerns() {
System.out.println("Here I ask for a fresh bean");
InconsistentCaseDetector inconsistentCaseDetector = applicationContext.getBean(InconsistentCaseDetector.class);
InconsistentCaseDetector是一个原始接口。 我有几个实现注释如下(具有不同的条件):
@Component
@Conditional(SkosFormatSelected.class)
@Scope(value = "prototype", proxyMode = ScopedProxyMode.INTERFACES)
public class InconsistentCaseDetectorImpl implements InconsistentCaseDetector {
...
条件的一个例子:
public class SkosFormatSelected implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
System.out.println("Is skos format selected ?");
return Formats.getCurrent().equals(Formats.SKOS);
}
但条件类的matches
方法仅在启动时调用。 在启动过程中,会显示日志说明(“是否选择了skos格式?”,“是否选择了xml格式?”...),然后显示:
org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [/home/..../.../WEB-INF/classes/tests/model/vocabulary/skos/algorithm/InconsistentCaseDetectorImpl.class]
但是,在每个请求中,条件不再执行,并且它始终与所服务的实现相同。 这是日志在运行时显示的内容:
Here I ask for a fresh bean
163326 [http-nio-8080-exec-23] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'inconsistentCaseDetectorImpl'
...
163328 [http-nio-8080-exec-23] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'scopedTarget.inconsistentCaseDetectorImpl'
163329 [http-nio-8080-exec-23] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'scopedTarget.inconsistentCaseDetectorImpl'
因此,当我请求bean时,它从缓存中返回一个实例,而不是创建一个新实例。 后来,但我不知道在哪里,似乎创建了一个新的,仍然没有执行条件子句。
采用
@Autowired
@RequestMapping("/...")
public ModelAndView labelConcerns(final InconsistentCaseDetector inconsistentCaseDetector) {
...
}
应该管用。
顺便说说,
@Component
移动到接口(DRY / SRP),也可以移动@Scope
。 ApplicationContext
而不是使用接口ApplicationContextAware
(DIP)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.