0

我尝试注入一个 applicationScoped Bean。我在JSF2 ApplicationScope bean 实例化时发现了类似的主题?并 在任何 Servlet 相关类中按名称获取 JSF 托管 bean

在 faces 上下文中使用 jsf 方式一切都很好(WebsitesController 是 AppScoped):

FacesContext context = FacesContext.getCurrentInstance();
WebsiteController websitesController = context.getApplication().evaluateExpressionGet(context, "#{websitesController}", WebsitesController.class);

随着两个溢出线程的注入,它不起作用。我的代码:

@ManagedBean(eager=true)
@ApplicationScoped
public class WebsitesController implements Serializable {
...}

现在我尝试了

@ManagedBean(name = "shopController")
@ViewScoped
public class ShopController {
    {Injection-Statement}
    private WebsitesController websitesController;

我尝试了以下陈述:

@ManagedProperty("#{websitesController}")
@Inject
@EJB

我的错是什么?

4

1 回答 1

1

我对 Glassfish 上的 ApplicationScope 也有疑问。你有 maven 或 ant webproject 吗?(使用 maven,我认为 ApplicationScope 不能按预期工作 - 使用 ant 可以)

现在回答你的问题:

当您使用 @Inject 时,您的 WebsiteController 需要 @Named 和 @ApplicationScope(其他导入为 jsf !!)并且您必须有一个 beans.xml - 然后激活 CDI。

当您使用 @EJB 时,您的 WebsiteController 需要 @Stateless 。

我希望我能帮助你...

于 2011-12-09T07:30:11.293 回答