Reputation: 663
I'm currently struggeling with injecting an EJB and a ManagedBean into a Spring-Handler. My goal is to inject these two beans into this AuthenticationSuccessHandler
.
public class LoginAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
}
This handler is called by Spring upon a successful login and doesn't seem to process injection-annotations. The two beans, which should be injected, are of the following structure:
@javax.ejb.Stateless
public class EjbService {
}
@javax.enterprise.context.SessionScoped
@javax.inject.Named("cdiBean")
public class CdiBean implements Serializable {
}
So far I've tried various annotations like the following, but the variables stay null
:
public class LoginAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
@EJB
private UserDatabaseService userDatabaseService;
@Inject
private UserManagementBean userManagement;
}
Could somebody point me to the right way?
Upvotes: 0
Views: 897
Reputation: 321
Normally, you cannot inject CDI managed Bean into Spring managed Bean. You will need to use direct access to Bean Manager. For Example by using javax.enterprise.inject.spi.CDI class.
then you can do something like that:
public static <T> T getInstance(Class<T> type, Annotation... qualifiers) {
Set<Bean<?>> beans = getBeanManager().getBeans(type, qualifiers);
Bean<?> bean = getBeanManager().resolve(beans);
if(bean == null){
throw new UnsatisfiedResolutionException();
}
@SuppressWarnings("unchecked")
T instance = (T) getBeanManager().getReference(bean, type,
getBeanManager().createCreationalContext(bean));
return instance;
}
public static BeanManager getBeanManager() {
return CDI.current().getBeanManager();
}
Upvotes: 1