我将 JSF 2.1 与 CDI 和 JBoss 7.1.1 一起使用
是否可以在超类变量中注入 CDIprincipal
并转换为派生类?例如MyUserPrincipal
是派生类。如果我写@Inject Principal principal
我从调试(和重载的 toString() 方法)中知道MyUserPrincipal
代理类将被注入到变量中principal
。但我无法将此实例转换为MyUserPrincipal
实例。
下面是我解决问题的 2 次尝试:
public class MyUserPrincipal implements Principal, Serializible{
MyUserPrincipal (String name){
}
public myMethod() { }
}
//Attempt 1:
public class MyCdiClass2 implements Serializable{
//MyUserPrincipal proxy instance will be injected.
@Inject Principal principal;
@PostConstruct init() {
MyUserPrincipal myPrincipal = (MyUserPrincipal) pincipal; //<--- Fails to cast! (b)
myPrincipal.myMethod();
}
}
//Attempt 2:
public class MyCdiClass1 implements Serializable{
@Inject MyUserPrincipal myPrincipal; //<---- Fails to inject! (a)
@PostConstruct init() {
//do something with myPrincipal
}
}