Tomcat 8嵌入式-ContextResource已被弃用的替代方案

3

希望您一切顺利。

我想把我的嵌入式Tomcat源代码从Tomcat 7迁移到Tomcat 8。 但是我在org.apache.catalina.deploy.ContextResource方面遇到了问题,而且迄今为止我还没有其他使用方法的想法。

我相信org.apache.catalina.deploy在Tomcat 8中已经被弃用。

完整源代码:

public class Tomcat8LocalServer {
public static void main(String[] args) {
    String baseDir = (System.getProperty("basedir") == null ? System.getProperty("user.dir") : System
            .getProperty("basedir")) + "/public_html/epi_html";

    Tomcat tomcat = new Tomcat();
    tomcat.enableNaming();
    try {
        Context context = tomcat.addWebapp("/epi", baseDir);

        bindDataSource(context);

        tomcat.getConnector().setPort(Integer.valueOf(System.getProperty("tomcat.embedded.port", "8080")));
    }
    catch (ServletException ex) {
        throw new IllegalStateException("failed to add webapp", ex);
    }

    try {
        long start = System.currentTimeMillis();
        tomcat.start();
        System.out.println("[Tomcat embedded] Server started in " + (System.currentTimeMillis() - start) + " ms");
        System.setProperty("tomcat.embedded.enabled", "true");
    }
    catch (LifecycleException ex) {
        throw new IllegalStateException("failed to start tomcat server", ex);
    }
    tomcat.getServer().await();
}

private static void bindDataSource(Context context) {

     ContextResource res = new ContextResource();

    res.setName("jdbc/EPIZIV_DS");
    res.setType("javax.sql.DataSource");

    res.setProperty("username", System.getProperty("tomcat.embedded.datasource.username", "EPIZIV"));
    res.setProperty("password", System.getProperty("tomcat.embedded.datasource.password", "EPIZIV"));
    res.setProperty("driverClassName",
            System.getProperty("tomcat.embedded.datasource.driver", "oracle.jdbc.OracleDriver"));
    res.setProperty("url",
            System.getProperty("tomcat.embedded.datasource.url", "jdbc:oracle:thin:@localhost:1521:xe"));
    res.setProperty("maxActive", System.getProperty("tomcat.embedded.datasource.max.active", "5"));
    res.setProperty("maxIdle", System.getProperty("tomcat.embedded.datasource.max.idle", "1"));

    context.getNamingResources().addResource(res);
}

堆栈跟踪快照: 堆栈跟踪

我的问题:

  1. 有没有办法替换 org.apache.catalina.deploy.ContextResource
  2. 如何在嵌入式Tomcat 8源代码中初始化/绑定数据源属性?
1个回答

1

问题已解决。你的建议让我能够修复它。 将“org.apache.catalina.deploy.ContextResource”更改为“org.apache.tomcat.util.descriptor.web.ContextResource;” 教训是: 检查你的Java构建路径。我没有意识到我的类路径仍然指向Tomcat 7 jar,即使我已经更改了那个包。 顺便说一下,感谢@Stefan的快速响应。 - epiziv

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接