我有一个用 RestEasy 实现的 jax-rs 服务,我正在使用 Jboss as7 并用 @Signleton 注释实现了一个signleton ejb。当服务器启动(@startup)时会启动signleton,我想将它注入(inject)到使用@EJB的jax-rs中。问题是这个类总是空的,我开始迷路了,因为在我看过的每个教程中,这就是他们注入(inject) ejb 的方式。
我应该使用任何特殊的 xml 文件吗?我究竟做错了什么?
@Path("/")
public class Service extends Application{
@EJB
private GlobalStore store;
public Service(){
fromejbstore = store.getSentiment();//null pointer is thrown
}
}
Signleton ejb 是:
@Singleton
@Startup
public class GlobalStore {
Sentiment sentiment;
@PostConstruct
public void initialize() {
//do something
}
public Sentiment getSentiment(){return sentiment;}
}
最佳答案
我已将代码更改为如下所示并避免注入(inject):
`try{
InitialContext ctx=new InitialContext();
localRef = (myEjb) ctx.lookup("java:global/appName/EjbName");
}catch(NamingException ne){
System.out.println("\n[MyRestService]NamingException: "+ne);
ne.printStackTrace();
}`
找不到@EJB 注释不起作用的原因,可能这与RestEasy 有关。
关于jakarta-ee - RestEasy @EJB 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10331041/