Spring作为JNDI提供者? [英] Spring as a JNDI provider?

查看:108
本文介绍了Spring作为JNDI提供者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Spring用作JNDI提供程序。这意味着我想在Spring上下文中配置一个bean,可以通过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>

然后,在我的应用程序(比如说一个Controller)中,我希望能够抓住这个bean via:

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");

我怎么能这样做?我看过XBean,但项目看起来已经过时了(不适用于Spring 3.0.XI不要考虑),并且文档很少。

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.

还有其他选择吗?如果不是很难做的话,我也会考虑推出我自己的jndi提供者课程。

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

编辑:我应该补充说我不喜欢没有使用JNDI的选项,我有一个我们必须使用的库,需要通过JNDI加载某些组件。我想使用Spring作为提供者。

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.

推荐答案

为什么要使用JNDI?只需获取Spring ApplicationContext并从中获取bean。

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

假设您在webapp中使用ContextLoaderListener初始化Spring,您应该能够从中检索应用程序上下文ServletContext。从那里你可以获得你在Spring中声明的任何bean。

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);

如果你必须使用JDNI,然后你可以创建一个ServletContextListener,它在contextInitialized()中执行类似下面的操作:

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);

然后,您应该能够从InitialContext中的spring / bean中查找Spring bean。

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

有两点需要注意:


  1. 上下文监听器应该是也可以在contextDestroy中调用initCtx.destroySubcontext(spring)。

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

java:comp / env命名空间是只读的(至少在Tomcat中),所以你不能放任何东西。

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


Asker编辑:只需要更多清晰点...

Asker edit: Just a couple more points of clarity...

如果您计划通过<$引用Spring bean c $ c> ApplicationContext ,然后您需要在web.xml中定义 ContextLoaderListener 。这必须在您的自定义侦听器类之前定义...如下所示:

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> 

此外,你可以得到 ServletContext 那个 getWebApplicationContext 使用 ServletContextEvent ,如下所示:

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 }
}

这篇关于Spring作为JNDI提供者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆