Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring as a JNDI provider?

Tags:

java

spring

jndi

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.

like image 375
Polaris878 Avatar asked Feb 26 '23 14:02

Polaris878


1 Answers

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 }
}
like image 114
AngerClown Avatar answered Mar 07 '23 19:03

AngerClown