[英]ManagedProperty with CDI Managed Beans is null in other Managed Beans
so I have three CDI Managed Beans and In two of these I'm injecting the loginMB
so I can use the Session
properties in it, but they are both throwing NullPointerException
: 所以我有三个CDI托管Bean ,在其中两个中,我注入了loginMB
以便可以在其中使用Session
属性,但是它们都抛出NullPointerException
:
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
@Named(value = "loginMB")
@SessionScoped
public class LoginMB implements Serializable{
private NomadUser usuarioEnSession;
private UserSettings configuracionUsuarioEnSession;
public void login(){}
// login method ....
// where I already verified that both usuarioEnSession & config... != null
}
MB 1: MB 1:
@Named(value = "fotoPerfilMB")
@RequestScoped
public class FotoPerfilMB implements Serializable {
@ManagedProperty(value = "#{loginMB}")
private LoginMB loginMB;
@PostConstruct
public void init(){
// this throws NullPOinterException
loginMB.getConfiguracionUsuarioEnSession();
}
}
MB 2: MB 2:
@Named(value = "imagenesMB")
@RequestScoped
public class ImagenesMB implements Serializable {
@ManagedProperty(value = "#{loginMB}")
private LoginMB loginMB;
@EJB
private UserImagesFacade servicioImagenes;
private int idUsuarioEnSession;
private NomadUser usuarioEnSession;
private List<UserImages> listaImagenes;
@PostConstruct
public void init() {
try {
usuarioEnSession = loginMB.getUsuarioEnSession();
idUsuarioEnSession = usuarioEnSession.getId();
listaImagenes = servicioImagenes.listaImagenes(idUsuarioEnSession);
} catch (NullPointerException e) {
// this is allways thrown
System.err.println("loginMB is null");
}
}
As you can see I've been playing around with try-catch
statements, printing the ManagedProperty
value but I just can't seem to understand why its null , here's the stack trace. 如您所见,我一直在try-catch
语句,打印ManagedProperty
值,但我似乎无法理解为什么它为null ,这是堆栈跟踪。 Thanks in advance. 提前致谢。
This happens when I remove the try-catch
statements, and I understand its because there's an Exception
being thrown in the @postConstruct
method and I now its NullPointerException
from the loginMB ManagedProperty
当我删除try-catch
语句时会发生这种情况,并且我理解它是因为@postConstruct
方法中引发了Exception
,现在我从loginMB ManagedProperty
了NullPointerException
org.jboss.weld.exceptions.WeldException: WELD-000049:
Unable to invoke public void co.nomad.managedBeans.FotoPerfilMB.init()
on co.nomad.managedBeans.FotoPerfilMB@672d4b74
It's pretty much normal, you're mixing CDI annotations with JSF annotations. 这很正常,您正在将CDI注释与JSF注释混合在一起。
This is a bad thing because JSF keeps its own managed bean container and CDI does pretty much the same. 这是一件坏事,因为JSF保留了自己的托管bean容器,而CDI几乎一样。 This kind of setup will lead at best to strange behaviours. 这种设置最多只会导致奇怪的行为。
I would advise to stick to CDI annotations or use only JSF annotations in managed beans and use Spring for the others beans such as services or else. 我建议在托管Bean中坚持使用CDI注释或仅使用JSF注释,并将Spring用于其他Bean(例如服务或其他)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.