1 Reply Latest reply on Nov 29, 2014 12:04 AM by Jay SenSharma

    Access simple bean (non-EJB) using JNDI

    Srinivas Kothuri Newbie

      I'm not able to access my bean class (not EJB) using a given JNDI name in JBoss 5.0.1, where the same is working in Tommcat 7.0.




              <Resource name="test/TestDAO" auth="Container"













      Servlet Java Client (in the same web app):

          Context initCtx = new InitialContext();

          MyBeanDAO beanDAO = (MyBeanDAO) initCtx.lookup("java:comp/env/test/TestDAO");



      And additionally for JBoss,











      I've been getting the below exception

          javax.naming.NameNotFoundException: test not bound

               at org.jnp.server.NamingServer.getBinding(NamingServer.java:771)

               at org.jnp.server.NamingServer.getBinding(NamingServer.java:779)

               at org.jnp.server.NamingServer.getObject(NamingServer.java:785)

               at org.jnp.server.NamingServer.lookup(NamingServer.java:396)

               at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:722)

               at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:682)

               at javax.naming.InitialContext.lookup(InitialContext.java:411)

               at ws.web.TestJNDI.doGet(TestJNDI.java:43)

      Please let me know if I'm missing anything and suggest me the right way of doing it in JBoss.

        • 1. Re: Access simple bean (non-EJB) using JNDI
          Jay SenSharma Master

          "context.xml" file based approach will not work in AS7/Wildfly.  So if you want to add a custom object in the JNDI tree of your JBoss for remote access then you may need to use the standard options like creating a class (example: MyCustomObjectFactory) by implementing "javax.naming.spi.ObjectFactory"  Then it can be easily accessed from the JNDI tree, locally or remotely.



          <subsystem xmlns="urn:jboss:domain:naming:1.1">
                  <object-factory name="java:jboss/exported/test2" module="test.jndi.demo" class="test.jndi.demo.MyCustomObjectFactory"/>


          The JNDI framework allows for object implementations to be loaded in dynamically via object factories. For example, when looking up a printer bound in the name space, if the print service binds printer names to References, the printer Reference could be used to create a printer object, so that the caller of lookup can directly operate on the printer object after the lookup. An ObjectFactory is responsible for creating objects of a specific type. refer to https://docs.oracle.com/javase/6/docs/api/javax/naming/spi/ObjectFactory.html


          Refer to [1] for a simple demo on the same:


          [1] Binding & Remote Lookup of Custom Objects in JNDI tree of JBoss AS7.1.1