I have a jax-rs service implemented with RestEasy, I am using Jboss as7 and have implemented a signleton ejb with the @Signleton annotation. The signleton is started when the server starts (@startup) and I would like to inject it into the jax-rs usign the @EJB. The problem is that the class is always null and I am started to get lost because in every tutorial I have looked this is the way they are injecting the ejb. Should I use any special xml file? what am I doing wrong?
@Path("/")
public class Service extends Application{
@EJB
private GlobalStore store;
public Service(){
fromejbstore = store.getSentiment();//null pointer is thrown
}
}
the Signleton ejb is:
@Singleton
@Startup
public class GlobalStore {
Sentiment sentiment;
@PostConstruct
public void initialize() {
//do something
}
public Sentiment getSentiment(){return sentiment;}
}
I have changed my code to look like this and avoid the injection:
`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();
}`
Could not find the reason the @EJB annotation is not working, probably this has something to do with the RestEasy.
AFAIK, the @EJB annotation injection doesn't work for POJOs - try turning your Service class into a stateless session bean (by adding @Stateless annotation on the class level) and the GlobalStore injection should work like a charm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With