简体   繁体   English

@Inject在会话中创建一个新的bean实例

[英]@Inject create a new bean instance in the session

It is similar to @Inject is injecting a new instance every time i use it , but I cannot find answer in that thread. 它类似于@Inject每次使用它时都会注入一个新实例 ,但是我无法在该线程中找到答案。

I'm very new to both CDI and JSF, and I'm trying to use CDI instead of JSF annotations. 我对CDI和JSF都非常陌生,并且正在尝试使用CDI而不是JSF注释。 I want to retrieve Credential from MemberController. 我想从MemberController检索凭证。 The bean itself (both) are invoked from the jsf pages. Bean本身(两者)都是从jsf页面调用的。 The problem is the instance of Credential in MemberController always has null name/password, even I confirmed that the setter in Credential is hit. 问题是,即使我确认Credential中的setter命中,MemberController中Credential的实例也始终具有空名称/密码。 I don't understand why there are two instances of Credential. 我不明白为什么会有两个Credential实例。 I could get what I want through @ManagedBean+@ManagedProperty. 我可以通过@ ManagedBean + @ ManagedProperty获得想要的东西。 But I want to know how could I do the same thing with CDI. 但是我想知道如何使用CDI做同样的事情。

My Environment is JBoss 7.1.1+Java EE 6 我的环境是JBoss 7.1.1 + Java EE 6

Credential.Java Credential.Java

import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import java.io.Serializable;

@Named
@SessionScoped
public class Credential implements Serializable{
    private static final long serialVersionUID = 680524601336349146L;

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    private String password;
}

MemberController.Java MemberController.Java

@Named
@SessionScoped
public class MemberController implements Serializable {
    private static final long serialVersionUID = 8993796595348082763L;

    @Inject
    private Credential newCredential;

    public void login() {
        String msg = newCredential.getName()+":"+newCredential.getPassword();
    }
}

JSF page segment JSF页面段

<p:inputText id="username" label="Username" value="#{credential.name}" />
<p:password id="password" label="Password" value="#{credential.password}" />
<p:commandButton value="Login" action="members" actionListener="#{memberController.login}" />

The injection doesn't call a new instance (well the first time it's been called, or the injected bean has a requestscope). 注入不会调用新实例(第一次调用它,或者注入的bean具有requestscope)。 It is depending on the scope you use for your bean. 这取决于您为bean使用的范围。 The Scope you use will generate for each person using your application a new bean (their own sessionscope bean). 您使用的范围将为使用您的应用程序的每个人生成一个新bean(它们自己的sessionscope bean)。 The data inside this scope are only visible for this person. 此范围内的数据仅对此人可见。 If you want a global container for your application which is accessible by all users of your application, containing the same data for everyone, you should create a applicationscope or a singelton bean. 如果您希望应用程序的全局容器可供应用程序的所有用户访问,并且每个人都包含相同的数据,则应创建一个applicationscope或singelton bean。 With this annotations you create one container accessible for everybody during the lifetime of your application, as a central point for holding data. 使用此批注,您可以在应用程序的生存期内创建一个可供所有人访问的容器,作为保存数据的中心点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM