确切地说,@ Inject注释何时在Servlet中启动SessionScoped bean的注入? [英] When, exactly, @Inject annotation initiates injection of SessionScoped bean in Servlet?

查看:194
本文介绍了确切地说,@ Inject注释何时在Servlet中启动SessionScoped bean的注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Servlet中修改用户会话对象(SessionScoped bean-CDI),所以我必须以某种方式获取该bean.我以以下方式使用注射:

I need to modify a user session object (SessionScoped bean - CDI) in a Servlet, so I have to obtain that bean somehow. I used injection in the following way:

@Inject
private UserSession user;

其中UserSession是SessionScoped CDI bean. user 方法是从doPost或doGet servlet方法中调用的. 这很完美;每次@Inject批注注入适当的UserSession bean时,但我不理解如何实现此行为.

where UserSession is the SessionScoped CDI bean. user methods are called from either doPost or doGet servlet methods. This works perfectly; every time the @Inject annotation injects the appropriate UserSession bean, but I don't understand how this behavior is achieved.

我假设用@Inject注释的bean仅被注入一次(当创建对象-在这种情况下为Servlet实例时),但这显然是错误的假设.

I assumed that the beans, annotated with @Inject, are injected only once (when the object - Servlet instance in this case - is created), but it is obviously a wrong presumption.

那么,这些bean什么时候注入到servlet中?每个请求?当存在多个UserSession对象时,这种方法如何避免冲突(一个servlet实例-要处理的多个线程)?

So, when are these beans injected into the servlet? Per request? And how does this approach avoids conflicts (one servlet instance - multiple threads to deal with it) when there are multiple UserSession objects?

推荐答案

CDI使用代理模式.注入的实例实际上不是真实实例,而是一个代理,它根据当前上下文定位实际实例并将所有方法委托给它(例如EJB的工作方式).您的UserSession bean的自动生成的类大致如下:

The CDI uses the proxy pattern. The injected instance is actually not the real instance, but a proxy which locates the real instance depending on the current context and delegates all methods to it (like as how EJBs work). The autogenerated class of your UserSession bean looks roughly like this:

public UserSessionCDIProxy extends UserSession implements Serializable {

    public String getSomeProperty() {
        UserSession instance = CDI.resolveItSomehow();
        return instance.getSomeProperty();
    }

    public void setSomeProperty(String someProperty) {
        UserSession instance = CDI.resolveItSomehow();
        instance.setSomeProperty(someProperty);
    }

}

此机制允许您在较宽范围的实例中注入较窄范围的实例,并允许您仍在当前上下文中获得预期的实例.标准的JSF @ManagedProperty注释不支持它,仅仅是因为它不使用代理,而是直接注入所需的实例.这就是为什么无法通过@ManagedProperty注入范围较窄的对象的原因.

This mechanism allows you to inject instances of a narrower scope in instances of a broader scope and allows you to still get the expected instance in the current context. The standard JSF @ManagedProperty annotation doesn't support it, simply because it does not use a proxy, but injects the desired instance directly. That's why it's not possible to inject something of a narrower scope by @ManagedProperty.

  • Backing beans (@ManagedBean) or CDI Beans (@Named)?
  • Get JSF managed bean by name in any Servlet related class
  • When using @EJB, does each managed bean get its own @EJB instance?
  • How to choose the right bean scope?

这篇关于确切地说,@ Inject注释何时在Servlet中启动SessionScoped bean的注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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