Reputation: 856
Am using struts2 , EJB 3.0 ... My requirement is to call the EJB layer from struts2 action class . I hope there are two ways in achieving this :
1.Using @EJB annotation in Action class
2.Using JNDI look up
I tried both ,
but the problem with JNDI lookup
is , eventhough am using correct naming , am getting NameNotFoundException
. So the ultimately , my team moved to other method which is using @EJB
annotation .
But when am using @EJB
annotation am getting null out of it , I think its not injected :
am getting the NullPointerException
code :
@EJB(mappedName="BeanLocal/local")
BeanLocal bean ;
Can any one suggest me what i have to do further ... Also if there is anylink in SOF , please do refer me as i found nothing related to this
Upvotes: 2
Views: 5589
Reputation: 343
I wrote following interceptors to solve this issue. Have a look and share any feedback you may have :
http://gauravwrites.blogspot.com/2014/11/ejb-injection-in-struts2-interceptor.html
Upvotes: 0
Reputation: 3358
I had this same problem and here is how I solved it.
As Shinosha said, the @EJB annotation will not work since the action classes are managed by the Struts container.
In order to use JNDI lookup, I had to make the bean @Remote and specify a mappedName. Then the code is as follows (it depends on the server your are using, in my case Weblogic):
Context ctx = new InitialContext();
MyBean bean= (MyBean) ctx.lookup("MyBeanMappedName#myapp.MyBean");
The lookup string should be the fully qualified name of the bean.
Upvotes: 2
Reputation: 2631
You can't use the traditional dependency injection in Struts 2 action classes because actions aren't managed. However there is a way to achieve this by using the CDI plugin or Guice. You can also use JNDI look up but the syntax depends on your server. Your best option is to check the documentation according to what you have (JBoss 7.1, Glassfish...)
Upvotes: 2