How to call EJB in PhaseListener

假如想象 提交于 2019-12-25 02:32:51

问题


I use JSF 2 and EJB 3.1, all of them are deployed on Glassfish 3.1.

When i setup a class named MyInterceptor which is implemented PhaseListener, i can not revoke remote EJB interface inside it.

It notice "NullPointerException ..."

public class MyInterceptor implements PhaseListener {

@EJB(name="AuthorizationEJB", 
            beanInterface=AuthorizationService.class,
            mappedName="corbaname:iiop:localhost:3700#ejb/AuthorizationEJB")
public AuthorizationService authorizationService;

....

} 

When I call authorizationService.dosomestuff(), it raise error NullPointerException

How can i do to fix it?

Thank in advance


回答1:


In JSF 2.1 and earlier, PhaseListeners are unfortunately no injection targets (meaning you can't use injection in them). You can do your lookup programmatically via JNDI instead though.

In JSF 2.2 all JSF artifacts (including PhaseListeners) will be injection targets, but this will probably not help you now.

Unrelated to your question, but I'm not sure what you're trying to achieve by specifying the beanInterface in your annotation. Most likely you'll also don't need the name attribute and if your bean is a local bean you'll also don't need mappedName.




回答2:


Use a servlet filter instead of a JSF phase listener to do authorization. You can inject an @EJB in a @WebFilter.




回答3:


Yeah in web filter you could have just used plain @EJB. Maximimum you needed to add beanName if you had two beans implement same AuthorizationService interface.

Servlet filter is per request, I don't think you need to do security stuff at a certain phase from JSF's lifecycle (which is a more granular level than the whole http request).

For normal lookup you can do:

AuthorizationService.class.cast(new InitialContext().lookup("corbaname:iiop:localhost:3700#ejb/AuthorizationEJB")).dosomestuff();

in a try catch javax.naming.NamingException



来源:https://stackoverflow.com/questions/10061342/how-to-call-ejb-in-phaselistener

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!