Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3495190
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:05:48+00:00 2026-05-18T12:05:48+00:00

I would like to use Spring as a JNDI provider. This means that I

  • 0

I would like to use Spring as a JNDI provider. This means that I would like to configure a bean in my Spring context, which can be accessed via JNDI. This would look something like this:

<bean class="org.some.thing.here">
    <property name="beans">
        <map>
            <entry key="w/t/f">
                <bean class="some.thing.Else">
                     // rest ommitted
                </bean>
            </entry>
        </map>
    </property>
</bean>

Then, in my application (lets say a Controller), I want to be able to grab this bean via:

Context ctx = new InitialContext();
some.thing.Else bar = (some.thing.Else) ctx.lookup("w/t/f");

How could I go about doing this? I’ve looked at XBean, however the project looks out of date (doesn’t work with Spring 3.0.X I don’t think), and there is very little documentation.

Any other options? I would also considering rolling my own jndi provider class if it isn’t too hard to do.

EDIT: I should add that I don’t have an option using JNDI, I have a library we have to use which requires certain components to be loaded via JNDI. I would like to use Spring as the provider.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T12:05:49+00:00Added an answer on May 18, 2026 at 12:05 pm

    Why use JNDI at all? Just get the Spring ApplicationContext and get the bean from that.

    Assuming you initialized Spring using ContextLoaderListener in your webapp, you should be able to retrieve the application context from the ServletContext. From there you can get any bean you declared in Spring.

    ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    Object bean = context.getBean(some.thing.Else.class);
    

    If you have to use JDNI, then you can create a ServletContextListener that does something like the following in contextInitialized():

    ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    
    Object bean = context.getBean(some.thing.Else.class);
    
    Context initCtx = new InitialContext();
    Context springCtx = initCtx.createSubcontext("spring");
    
    springCtx.bind("bean", bean);
    

    Then, you should be able to lookup the Spring bean at “spring/bean” from the InitialContext.

    Two things to note:

    1. The context listener should probably also call initCtx.destroySubcontext(“spring”) in contextDestroy too.

    2. The java:comp/env namespace is read-only (in Tomcat at least), so you can’t put anything there.

    Asker edit: Just a couple more points of clarity…

    If you plan on referencing Spring beans via ApplicationContext, then you need a ContextLoaderListener defined in your web.xml. This must be defined before your custom listener class… like so:

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <listener>
        <listener-class>
        org.example.sandbox.MyCustomServletContextListener
      </listener-class>
    </listener> 
    

    Also, you can get the ServletContext that getWebApplicationContext uses from the ServletContextEvent, like so:

    @Override
    public void contextInitialized(ServletContextEvent contextEvent) {
        try {
            ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(contextEvent.getServletContext());
    
            // get a bean named "myCalendar" from the application context       
            Calendar cal = (Calendar)appContext.getBean("myCalendar");
    
            // bind via JNDI
            Context initialContext = new InitialContext();
            Context subCtx = initialContext.createSubcontext("sample");
            subCtx.bind("calendar", cal);
    
        } catch (NamingException e) { // ommitted }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use @Repository spring annotation to avoid adding bean in context.xml.
Spring: 2.5.6.SEC01 DWR: 2.0.5 I would like to use a session scoped bean from
I am using Spring Security 3.0.6. for modularity reasons, I would like to use
I would like to use GIO (which is part of GLIB) on Android by
I would like to use JNDI to look up Kerberos SRV records in a
I would like to be able to use Spring using setter injection into Scala
I would like to use jsf annotations and some spring annotations to inject a
I would like to use two different implementations for a DAO with Spring's testframework.
I would like use Spring.NET for IoC , DI, AOP and Caliburn as MVVVM
I would like to know if it's possible to use Spring to resolve the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.