I'm using JSF 2.1 with CDI and JBoss 7.1.1
Is it possible to inject with CDI in a super class variable principal
and cast to derived class ? In example MyUserPrincipal
is derived class. If I write @Inject Principal principal
I know from debugging (and overloaded toString() method) that MyUserPrincipal
proxy class will be injected in the variable principal
. But I could not cast this instance to MyUserPrincipal
instance.
Below my 2 attempts to solve the problem:
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
}
}