【问题标题】:Jetty 7: configuring JNDI for Start.javaJetty 7:为 Start.java 配置 JNDI
【发布时间】:2011-12-16 23:05:06
【问题描述】:

按照 Wicket 1.5 的引导,我正在将项目从 Jetty 6.1.25 转换为 7.5.0.v20110901。我现有的Start.java 包含以下设置,用于配置 JNDI:

    EnvConfiguration envConfiguration = new EnvConfiguration();
    URL url = new File("src/main/webapp/WEB-INF/jetty-env.xml").toURI().toURL();
    envConfiguration.setJettyEnvXml(url);

    bb.setConfigurations(new Configuration[]{new WebInfConfiguration(),
                         envConfiguration,
                         new org.mortbay.jetty.plus.webapp.Configuration(), new JettyWebXmlConfiguration(),
                         new TagLibConfiguration()});

那么我的jetty-env.xml 有以下内容:

<Configure class="org.mortbay.jetty.webapp.WebAppContext">

    <New class="org.mortbay.jetty.plus.naming.Resource">
        <Arg>jdbc/myapp</Arg>
        <Arg>
            <New class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                <Set name="driverClassName">com.mysql.jdbc.Driver</Set>
                <Set name="url">jdbc:mysql://localhost/myapp?characterEncoding=utf8</Set>
                <Set name="username">username</Set>
                <Set name="password">password</Set>
            </New>
        </Arg>
    </New>

</Configure>

这在 Jetty 6 中效果很好,但在 7 中,org.mortbay.jetty.plus.webapp.Configuration 似乎不存在(或者我可能缺少一个 Jar)。

有人能给我一些关于如何使用 Jetty 7 配置 JNDI 的指导吗?

【问题讨论】:

    标签: java jetty wicket jndi


    【解决方案1】:

    将以下内容放入 src/test/jetty/jetty-env.xml:

    <Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext">
        <New class="org.eclipse.jetty.plus.jndi.EnvEntry">
        <Arg>jdbc/mydatasource</Arg>
        <Arg>
            <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
                <Set name="Url">jdbc:mysql://localhost/mydatabase?characterEncoding=utf8</Set>
                <Set name="User">username</Set>
                <Set name="Password">password</Set>
            </New>
        </Arg>
        </New>
    </Configure>
    

    然后修改Start.java,定义如下属性:

    System.setProperty("java.naming.factory.url.pkgs", "org.eclipse.jetty.jndi");
    System.setProperty("java.naming.factory.initial", "org.eclipse.jetty.jndi.InitialContextFactory");
    

    并将以下配置添加到 WebAppContext:

    EnvConfiguration envConfiguration = new EnvConfiguration();
    URL url = new File("src/test/jetty/jetty-env.xml").toURI().toURL();
    envConfiguration.setJettyEnvXml(url);
    
    bb.setConfigurations(new Configuration[]{ new WebInfConfiguration(), envConfiguration, new WebXmlConfiguration() });
    

    我的blog 的详细信息。

    【讨论】:

      【解决方案2】:

      从 Jetty 7 开始,包名从 org.mortbay.jetty 更改为 org.eclipse.jetty

      此外,org.eclipse.jetty.plus.webapp.Configuration 在 7.2.0 版本中更名,新名称为 PlusConfiguration。我猜这是为了避免与org.eclipse.jetty.webapp.Configuration 发生名称冲突。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-21
        • 2013-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多