每次使用@Inject都会注入一个新实例 [英] @Inject is injecting a new instance every time i use it
问题描述
我有以下问题。
(jboss 7.1 as,jsf 2和myFaces扩展过滤器)
I have the following issue. (jboss 7.1 as, jsf 2 and myFaces extension filter)
两个托管bean。 (我已经清理了代码)。我想从NextBean检索LoginBean。问题在于,每次我注入bean时,都会得到一个LoginBean的新实例,而不是当前实例。我已经尝试过@Named( name)之类的东西。 Bean本身(都是)都从jsf页面调用,并且所有数据都已设置,但是每次都注入一个新实例,而我无法弄清楚自己在做什么错!!! $ b
Two managed beans. (I have cleaned up the code). I want to retrieve LoginBean from NextBean. The problem is that every time i inject the bean i get a new instance of LoginBean and not the current one. I have tried things like @Named("name"). The bean itself (both) are invoked from the jsf pages and all data is set, but a new instance is injected every time and i cant figure out what i'm doing wrong!!!
@Named
@SessionScoped
public class LoginBean implements Serializable {
private static final long serialVersionUID = -8825740128333470396L;
@EJB private UserDetailDao userDetailDao;
private String description;
public String getDscription() { return description; }
public String submit() {
userDetailDao.doSomething("test");
description = "testing";
return "next.xhtml";
}
}
和第二个bean
@Named
@SessionScoped
public class NextBean extends SomeBean implements Serializable {
private static final long serialVersionUID = -4654827059950265300L;
@Inject private LoginBean loginBean;
public String submit() {
String description = loginBean.getDscription();
return "login.xhtml";
}
}
已设置说明,我可以看到在jsf页面中,但是我无法从NextBean访问它,因为它每次都会注入一个新实例!但是数据在会话中!
The "Description" is set and i can see it in the jsf page, but i cant access it from NextBean since it injects a new instance every time! But the data is in the session!
:/
有什么提示吗?
预先感谢!
Any tips? Thanks in advance!
推荐答案
我在给豆子命名错误!并且未使用正确的@Produces:/
I was naming the beans wrong! and did not use proper @Produces :/
感谢您的时间小伙子们。
Thanks for your time lads.
这篇关于每次使用@Inject都会注入一个新实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!