2

I would have a question concerning JavaEE Beans. I have developed a long time with Spring and Hibernate and now I change to JavaEE - so therefore sorry if it is a very simple question but currently I have no idea how to handle my problem.

I have two Beans which are registered in JNDI (on JBoss - see above). - that is ok so far. If I use @EJB as dependency injection in my Servlet than it works fine:

@EJB(name = "jndi/TestClassBean")
public TestClass testClass;
@EJB(name = "jndi/OtherBean")
public OtherBean otherBean;

If I change @EJB with @Resource, than I get a NullPointerException:

@Resource(name = "jndi/TestClassBean")
public TestClass testClass;
@Resource(name = "jndi/OtherBean")
public OtherBean otherBean;

Does enyone know what to do in order to get it done with @Resource? I have nothing declared in deplyment descriptior - is it mandatory for @Resource to declare it in DD? Thanks!

16:39:51,482 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-7) JNDI bindings for session bean named OtherBean in deployment unit deployment ...

java:global/salzburgag-javaeetest-frontend/OtherBean!at.maxqu.test.OtherBeanLocal
java:app/...-javaeetest-frontend/OtherBean!at.maxqu.test.OtherBeanLocal
java:module/OtherBean!at.....test.OtherBeanLocal
java:global/...-javaeetest-frontend/OtherBean!at.....test.OtherBean
java:app/...-javaeetest-frontend/OtherBean!at.....test.OtherBean
java:module/OtherBean!at.maxqu.test.OtherBean

16:39:51,490 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-7) JNDI bindings for session bean named TestClassBean in deployment unit deployment ...

java:global/...-javaeetest-frontend/TestClassBean!at.....javaeesimpletest.TestClass
java:app/...-javaeetest-frontend/TestClassBean!at.....javaeesimpletest.TestClass
java:module/TestClassBean!at.maxqu.javaeesimpletest.TestClass
java:global/...-javaeetest-frontend/TestClassBean!at.....javaeesimpletest.TestClassBean
java:app/...-javaeetest-frontend/TestClassBean!at.....javaeesimpletest.TestClassBean
java:module/TestClassBean!at.....javaeesimpletest.TestClassBean

16:39:52,272 INFORMATION [javax.enterprise.resource.webcontainer.jsf.config] (MS

skaffman
  • 398,947
  • 96
  • 818
  • 769
quma
  • 5,233
  • 26
  • 80
  • 146
  • Please refer the below link [http://stackoverflow.com/questions/24900908/what-is-difference-between-ejb-and-resource-in-dependency-injection-in-ejb][1] [1]: http://stackoverflow.com/questions/24900908/what-is-difference-between-ejb-and-resource-in-dependency-injection-in-ejb – Sasikumar Murugesan Feb 12 '15 at 06:56
  • That doesn't answer the question. – luis.espinal Sep 11 '18 at 14:25

1 Answers1

0

Try to use lookup:

@Resource(lookup = "java:global/...-javaeetest-frontend/TestClassBean!at.....javaeesimpletest.TestClass")
public TestClass testClass;

@Resource(lookup = "java:global/salzburgag-javaeetest-frontend/OtherBean!at.maxqu.test.OtherBeanLocal")
public OtherBean otherBean;
Warren Nocos
  • 1,222
  • 1
  • 14
  • 29