Spring 3 JNDI look up in glassfish3

2019-06-05 00:41发布

I want to look up some properties from JNDI configured in glassfish v3 server. I want to do it using spring. Here is my spring configuration:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
                           http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                           http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
                           http://www.springframework.org/schema/jee
                           http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

    <!--
        JNDI look ups.
     !-->
    <jee:jndi-lookup id="properties"
                     jndi-name="java:comp/env/jndi/ws_properties"
                     expected-type="java.util.Properties"/>

</beans>

I have mapped jndi/ws_properties in sun-web.xml and web.xml files. Problem is that this lookup always gives me null properties. But if I do it in java code:

    try {
        InitialContext context = new InitialContext();
        properties = (Properties) context.lookup("jndi/ws_properties");
    } catch (NamingException e) {
        LOGGER.error("", e);
    }

It is ok. I see my properties keys and values.

Could somebody tell me where is the problem here?

1条回答
霸刀☆藐视天下
2楼-- · 2019-06-05 01:18

This is probably because of your "jndi-name" property.

You don't have to put "java:comp/env/" in the name.

The "resource-ref" property defaults to true and unless you set it to false, it will automatically add the java:comp/env to the name.

查看更多
登录 后发表回答