Scoped bean:将一个注入另一个 [英] Scoped bean: inject one into another

查看:53
本文介绍了Scoped bean:将一个注入另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在没有代理的情况下将一个会话作用域 bean 注入另一个会话作用域 bean?

How to inject a session scoped bean into another session scoped bean without proxy?

@Component
@Scope("session")
class Foo {
    @Inject Bar bar;
}

@Component
@Scope("session")
class Bar {
}

它报告错误没有匹配的bean".虽然 TARGET_CLASS 作用域代理可以解决这个问题,但为什么我需要一个相同作用域 bean 的代理?

It reports error "No matching bean". Though a TARGET_CLASS scope-proxy could resolve this problem, but why do I need a proxy for same scoped beans?

推荐答案

我的猜测是 - 因为在注入点 spring 不会根据当前 bean 的范围来区分注入的 bean.它需要一个代理来获取目标 bean(在本例中来自会话),而不管注入它的 bean 的范围.

My guess is - because at the injection point spring doesn't distinguish injected beans depending on the scope of the current bean. It needs a proxy to fetch the target bean (from the session in this case), disregarding the scope of the bean where it's injected into.

这篇关于Scoped bean:将一个注入另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆