CDI豆注射液 [英] CDI beans injection

查看:164
本文介绍了CDI豆注射液的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在@SessionScoped bean中注入@ApplicationScoped bean的正确方法吗?这会导致我的应用程序范围的Bean被存储在每个用户的会话中吗?

Is this a correct approach to inject @ApplicationScoped bean in @SessionScoped bean? will this lead my application scoped bean to be stored in the session of every user?

我有一个应用程序范围的Bean,其中包含我们在所有系统用户之间共享的一些值,现在我需要在会话Bean的方法中获取这些值.

I have an application scoped bean that contains some values we share among all the system users, and now I need to get that values within a method in a session bean.

推荐答案

将相同或更大范围的bean注入另一个bean是完全合法的,并且在JSF或CDI bean中都是正确的,就像您提供的示例一样.

Injecting a bean of the same or a broader scope in another bean is completely legal and correct either in JSF or CDI beans, like the example you provided.

CDI bean和JSF托管bean之间的区别在于,当您尝试将范围较窄的bean注入另一个bean(例如,将@RequestScoped bean注入到@SessionScoped一个)中时,只有您正在使用CDI @Named bean,而在使用JSF @ManagedBean时则无法使用.

The difference between CDI beans and JSF managed beans regarding that is when you try to inject a bean of a narrower scope in another bean (e.g inject @RequestScoped bean into @SessionScoped one), which is only possible as long as you are using CDI @Named beans, while not possible when working with JSF @ManagedBean.

CDI bean之所以可能实现这一点,与其代理模式机制有关,与JSF机制(基于调用设置器以便直接注入物理实例)相比,它更加灵活.

The reason why this is possible for CDI beans is related to their Proxy Pattern mechanism, which is more flexible compared with the JSF mechanism (based on invoking the setters in order to directly inject a physical instance).

此代理机制允许CDI容器将引用传递给代理,而不是传递注入的Bean(除非Bean具有默认作用域@Dependent).因此,该代理将负责处理对注入的bean的所有调用,并将它们转发/重定向到正确的bean实例.

This proxy mechanism, allow the CDI container to pass a reference to a proxy instead of the injected bean (unless a bean has the default scope @Dependent). Therfore, that proxy will be responsbale of handling all calls to the injected bean and forward / redirect them to the correct bean instance.

  • CDI: Contexts and Dependency Injection for the Java EE platform - Client proxies
  • Java EE 6 @javax.annotation.ManagedBean vs. @javax.inject.Named vs. @javax.faces.ManagedBean
  • Backing beans (@ManagedBean) or CDI Beans (@Named)?
  • ManagedProperty in CDI @Named bean returns null

这篇关于CDI豆注射液的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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