我在JBoss AS 7.1.1环境中使用RestEasy。
我出于安全原因实现了一个PreProcessInterceptor类。该类使用@Provider和@ServerInterceptor进行注释。 每次都会调用拦截器,这很好。
现在,困扰我的是以下几点。
我将带有@EJB注释的EJB注入到类中。调用PreProcessInterceptor时,表示EJB总是 null 。
@Provider
@ServerInterceptor
public class SecurityInterceptor implements PreProcessInterceptor
{
@EJB
private SomeEjb someEjbServiceFacade;
... some funny stuff
}
看起来像EJB:
@Stateless
public class SomeEjb extends AbstractServiceFacade
{
... some important stuff
}
有趣的部分,它通过查找工作:
Context ctx = new InitialContext();
SomeEjb asf = ( SomeEjb )ctx.lookup("java:global/mySuperApplication/SomeEjb" );
是否有人对此行为有解释?
提前致谢。
答案 0 :(得分:3)
您不能在任意类中注入EJB,而只能在Servlet,Filter,Listener,Stateless,Statefull,MessageDriven bean,Singleton等指定类型中注入EJB。如果你想在其他类型的类中使用EJB,那么就像你在上面指出的那样使用JNDI访问。
答案 1 :(得分:0)
尝试将@Stateless注释添加到SecurityInterceptor
课程中。
答案 2 :(得分:0)
什么是 拦截器?
Interceptors
与Java EE托管类结合使用,允许开发人员在associated target class
上与method invocations or lifecycle events
一起调用拦截器方法。
SomeEjb
也可以访问以下课程associated target class i.e., SecurityInterceptor
。
@Stateless
public class SomeEjb extends AbstractServiceFacade
{
... some important stuff
}
为了做到这一点,我们需要在SecurityInterceptor
的{{1}}中添加<context-param>
。
看起来应该是这样的!
web.xml