JBoss and JNDI using pojo javabean

Posted on 2007-08-09
Last Modified: 2013-12-02
Hello, this question is two part.

Part 1: How can I place a simple custom java object in jndi on jboss 4?  In Tomcat is could not be easier.  I simply have

< Context path="/myApp" ... >
   < Resource name="myPath/MyObject"
         location="localhost"/>  // this line will call getLocation in MyObject after instantiating it.

Now to use this I simply:

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
MyObject myObj = (MyObject) envCtx.lookup("myPath/MyObject");

Part 2: The above Tomcat config is very nice since it is app context specific.  I can now take myApp.war and copy/paste it as myAppQA.war.  No app config at all.  I simply add another context to server.xml except I change the name to myAppQA and the location="localhost" to location="".  Then when ever I deploy the either myApp or myAppQA it will get the different object even though the application code points to the same name.  In JBoss, it looks like any jndi reference is to be setup as app server wide, not app context wide.

I'm really stuck on this as I'm new to JBoss.  Thanks in advance.

Question by:dbronk_winscape_com
    LVL 6

    Expert Comment


    I believe that what you need is an <env-entry> in your web.xml file.

    Best regards,


    Author Comment

    But is there a way to set this at the server level like Tomcat allows?  Tomcat is beautiful about this.  In the server xml I can simply add a Resource entry to the context and then when the server comes up it will instantiate the class and inject any values placed in the Resource node into the object.  It then places that object into jndi.  This is awesome with Tomcat because now I can simply place my server configs, ie: database settings, and other server specific settings that would normally change when moving through dev, qa, prod, etc.  By adding a <env-entry> in my web.xml means that I have to configure before I deploy to each environment.  My way I configure one time and never again.

    Since jboss uses tomcat I was hoping there was a way to do this.

    LVL 6

    Accepted Solution

    Well, in this case you should use a similar approach.

    Basically, in the above example, the Spring configuration is loaded and added to JNDI in order to be available to all other deployed applications.

    The initial settings for the parameters can be specified in the MBean XML file.

    JBoss also offers a web console which alowes you to invoke methods on the MBean you have created.

    Hope this helps you,


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    -Xmx and -Xms are the two JVM options often used to tune JVM heap size.   Here are some common mistakes made when using them:   Assume BigApp is a java class file for the below examples. 1.         Missing m, M, g or G at the end …
    Upgrading Tomcat – There are a couple of methods to upgrade Tomcat is to use The Apache Installer is to download and unzip and run the services.bat remove|install Tomcat6 Because of the App that we are working with, we can only use Tomcat 6.…
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now