【发布时间】:2014-10-24 17:30:51
【问题描述】:
我在 Jetty 服务器上的 JNDI 配置有问题。我不能以任何方式配置它,以便在 Spring (3.0.5) 之后可以检索 JNDI 变量。
我有一些我不想存储在属性中的凭据,这样它就不会存在于 git repo 中。我的 Web 应用程序在 Jetty(最新版本 9.2.3)上运行,因此我想出了将这个凭据存储在 Jetty Web 应用程序上下文中的想法。 Jetty 通过jetty-env.xml 提供这样的解决方案。所以我在 WEB-INF/ 中创建了 jetty-env.xml 文件,如下所示:
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure id="webappCtx" class="org.eclipse.jetty.webapp.WebAppContext">
<New id="username" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg><Ref refid="webappCtx"/></Arg> <!-- scope -->
<Arg>server/username</Arg> <!-- name -->
<Arg type="java.lang.String">myUsername</Arg> <!-- value -->
</New>
<New id="password" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg><Ref refid="webappCtx"/></Arg> <!-- scope -->
<Arg>server/password</Arg> <!-- name -->
<Arg type="java.lang.String">qwerty</Arg> <!-- value -->
</New>
</Configure>
之后我在web.xml 中定义了如下绑定:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="MyWebApp" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>My Web App</display-name>
<!-- INITIALIZE SPRING -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- JETTY-ENV JNDI -->
<resource-ref>
<res-ref-name>server/username</res-ref-name>
<res-type>java.lang.String</res-type>
</resource-ref>
<resource-ref>
<res-ref-name>server/password</res-ref-name>
<res-type>java.lang.String</res-type>
</resource-ref>
</web-app>
并以这种方式配置了我的 Spring 上下文:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<jee:jndi-lookup id="serverUsername" jndi-name="server/username" expected-type="java.lang.String" resource-ref="true" />
<jee:jndi-lookup id="serverPassword" jndi-name="server/password" expected-type="java.lang.String" resource-ref="true"/>
<bean name="abstractServerConnectionFactory" class="my.package.ServerConnectionFactory" abstract="true">
<constructor-arg value="${server.host}"/>
<constructor-arg value="${server.port}"/>
<constructor-arg ref="serverUsername"/>
<constructor-arg ref="serverPassword"/>
</bean>
</beans>
之后,我在启用 --add-to-startd=jndi 的情况下启动 Jetty,并且在创建 serverUsername 和 serverPassword 时总是得到 javax.naming.NameNotFoundException。我尝试了很多修改,但似乎都没有。我试过了:
- 将
org.eclipse.jetty.plus.jndi.Resource更改为org.eclipse.jetty.plus.jndi.EnvEntry,然后通过web.xml中的resource-env-ref引用它 - 将资源范围设置为 JVM 而不是 webapp,因此将
<Arg><Ref refid="webappCtx"/></Arg>更改为<Arg></Arg>,如上所述 here - 在不使用 web.xml 的情况下添加自动绑定,如上所述 here
<Call name="bindToENC">
<Arg>server/username</Arg> <!-- binds server/username to java:comp/env/server/username for this webapp -->
</Call>
还有很多其他的,但它只是不起作用。请给我一个工作示例,如何让它工作。谢谢!
【问题讨论】: