Java 注入 Applicationscope Bean
我尝试注入一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 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...