将嵌入式 Tomcat 从 v6 更改为 v7 会导致 InitialContext 查找失败
我正在使用 JUnit 测试用例来使用嵌入式 Tomcat 来测试我的 Web 服务。在 Tomcat 6 下一切都工作正常,但是当我将项目切换到 Tomcat 7 时,我就陷入困境了。
设置嵌入式 Tomcat 服务器的测试代码如下:
Tomcat 6
Embedded container = new Embedded();
container.setCatalinaHome("C:\\Program Files\\Apache Software Foundation\\Tomcat 7.0.11");
container.setRealm(new MemoryRealm());
container.setName("Catalina");
Engine engine = container.createEngine();
container.addEngine(engine);
Host host = container.createHost("localhost", "/DecoderServiceTest");
Context rootContext = container.createContext("/DecoderServiceTest", System.getProperty("user.dir") + "/build/web");
host.addChild(rootContext);
engine.setName("Catalina");
engine.addChild(host);
engine.setDefaultHost("localhost");
container.addEngine(engine);
Connector connector = container.createConnector(InetAddress.getLocalHost(), 4321, false);
container.addConnector(connector);
container.start();
由于嵌入式 API 在版本 6 和 7 之间发生了变化,我将自己的代码更改为以下内容:
Tomcat 7
Tomcat tomcat = new Tomcat();
tomcat.setBaseDir("C:\\Program Files\\Apache Software Foundation\\Tomcat 7.0.11");
tomcat.setPort(1234);
tomcat.addWebApp("/DecoderServiceTest", System.getProperty("user.dir")+"/build/web");
tomcat.setHostname("localhost");
tomcat.start();
当我执行时,实际的 Web 服务启动正常JUnit 测试(我可以使用我的 Web 浏览器并查看正在提供的 WSDL)。
但是,在我的 Web 服务的构造函数中,我根据 web.xml
文件(位于 System.getProperty("user.dir")+" 中的值初始化了一些变量/build/web/WEB-INF/web.xml"
),如下:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
int thumbnailSize = (Integer) envCtx.lookup("thumbnail-pixel-size");
我的 web.xml 文件包含以下条目:
<env-entry>
<env-entry-name>thumbnail-pixel-size</env-entry-name>
<env-entry-type>java.lang.Integer</env-entry-type>
<env-entry-value>64</env-entry-value>
</env-entry>
当我尝试创建 envCtx 时 对象我得到一个 NamingException 并显示以下消息 名称 java:comp 未在此上下文中绑定
。我很困惑,因为它在 Tomcat 6 中工作得很好。我是否在 Tomcat 7 的设置中遗漏了之前在 Tomcat 6 设置中定义的某些内容?
I'm using JUnit test cases to exercise my web service using embedded Tomcat. Under Tomcat 6 everything was working fine, but when I switched my project to Tomcat 7 I'm coming unstuck.
The test code to setup the embedded Tomcat server is as follows:
Tomcat 6
Embedded container = new Embedded();
container.setCatalinaHome("C:\\Program Files\\Apache Software Foundation\\Tomcat 7.0.11");
container.setRealm(new MemoryRealm());
container.setName("Catalina");
Engine engine = container.createEngine();
container.addEngine(engine);
Host host = container.createHost("localhost", "/DecoderServiceTest");
Context rootContext = container.createContext("/DecoderServiceTest", System.getProperty("user.dir") + "/build/web");
host.addChild(rootContext);
engine.setName("Catalina");
engine.addChild(host);
engine.setDefaultHost("localhost");
container.addEngine(engine);
Connector connector = container.createConnector(InetAddress.getLocalHost(), 4321, false);
container.addConnector(connector);
container.start();
As the embedded API has changed between versions 6 and 7, I've changed my own code to the following:
Tomcat 7
Tomcat tomcat = new Tomcat();
tomcat.setBaseDir("C:\\Program Files\\Apache Software Foundation\\Tomcat 7.0.11");
tomcat.setPort(1234);
tomcat.addWebApp("/DecoderServiceTest", System.getProperty("user.dir")+"/build/web");
tomcat.setHostname("localhost");
tomcat.start();
The actual web service starts up fine when I execute the JUnit test (I can use my web browser and see the WSDL being served up).
However, in the constructor of my web service I intialise some variables based on the values in the web.xml
file (which is located in System.getProperty("user.dir")+"/build/web/WEB-INF/web.xml"
), as follows:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
int thumbnailSize = (Integer) envCtx.lookup("thumbnail-pixel-size");
Where my web.xml
file contains the following entry:
<env-entry>
<env-entry-name>thumbnail-pixel-size</env-entry-name>
<env-entry-type>java.lang.Integer</env-entry-type>
<env-entry-value>64</env-entry-value>
</env-entry>
When I try and create the envCtx
object I get a NamingException with the message that Name java:comp is not bound in this Context
. I'm confused because it worked fine with Tomcat 6. Have I missed something in the setup of Tomcat 7 that I had previously defined in the setup of Tomcat 6?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Mark Thomas 通过 tomcat-users 邮件列表
noreferrer">在服务器启动之前 。这对我有用(我猜他们改变了 6 和 7 之间的默认行为)。
Mark Thomas via the tomcat-users mailing list suggested
before the server is started. This worked for me (I guess they changed the default behaviour between 6 and 7).