Java 注入 Applicationscope Bean

发布于 2024-12-09 14:28:48 字数 1093 浏览 1 评论 0原文

我尝试注入一个 applicationScoped Bean。 我在 JSF2 ApplicationScope bean instantiation time? 中找到了类似的主题 在任何 Servlet 相关类中按名称获取 JSF 托管 bean

使用 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

我的错是什么?

I try to inject a applicationScoped Bean.
I found similar topics at JSF2 ApplicationScope bean instantiation time? and
Get JSF managed bean by name in any Servlet related class

With the jsf way in faces context all is fine (WebsitesController is AppScoped):

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

With the injections of the two overflow threads it doesn't work. My Code:

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

and now i tried

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

I tried following statements:

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

Whats my fault?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月苍老的讽刺 2024-12-16 14:28:48

我在 Glassfish 上的 ApplicationScope 也有问题。你有 Maven 或 ant Web 项目吗? (使用maven我认为ApplicationScope不能按预期工作 - 使用ant它可以)

现在回答你的问题:

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

当你使用@EJB时,那么你的WebsiteController就需要@Stateless。

我希望我能帮助你...

I have also a problem with ApplicationScope on Glassfish. Do you have an maven or ant webproject? (With maven i think the ApplicationScope doesn´t work as expected - with ant it does)

Now to your question:

When you use @Inject, then your WebsiteController need @Named and @ApplicationScope (other imports as jsf !!) and you must have an beans.xml - then CDI is activated.

When you use @ EJB, then yout WebsiteController need @Stateless .

I hope i can help you...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文