首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有mysql DataSource和gretty的javax.naming.NoInitialContextException

带有mysql DataSource和gretty的javax.naming.NoInitialContextException
EN

Stack Overflow用户
提问于 2017-02-13 01:15:18
回答 1查看 443关注 0票数 0

我正在制作一个spring mvc应用程序,使用gradle作为构建工具,Mysql作为jndi数据源,.I可以通过构建war并手动部署来运行它。但是,当我尝试使用gretty运行它时,我得到了以下异常

代码语言:javascript
复制
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

这是我的build.gradle文件:

代码语言:javascript
复制
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
//apply plugin: 'jetty' //too old, Jetty6, use gretty
apply plugin: 'org.akhikhl.gretty'
apply plugin: 'idea'
apply plugin: 'jacoco' //code coverage

def springVersion = "4.2.4.RELEASE"
def jdkVersion = 1.8
def junitVersion = "4.12"
def logbackVersion = "1.1.3"
def jclOverSlf4jVersion = "1.7.14"
def jstlVersion = "1.2"
def hamcrestVersion = "1.3"
def servletApiVersion = "3.1"
def mysqlVersion = "5.1.31"

sourceCompatibility = jdkVersion
targetCompatibility = jdkVersion

repositories {
    mavenLocal()
    mavenCentral()
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.10'
}

configurations.all {
    exclude group: "commons-logging", module: "commons-logging"
}

dependencies {

    compile 'org.slf4j:jcl-over-slf4j:' + jclOverSlf4jVersion
    compile 'ch.qos.logback:logback-classic:' + logbackVersion
    compile 'org.springframework:spring-webmvc:' +springVersion
    compile 'org.springframework:spring-context:' + springVersion
    compile 'org.springframework:spring-orm:' + springVersion
    compile 'javax.servlet:jstl:' + jstlVersion
    compile 'org.springframework:spring-test:' + springVersion
    compile 'mysql:mysql-connector-java:' + mysqlVersion
    //exclude the build in hamcrest
    testCompile('junit:junit:' + junitVersion) {
        exclude group: 'org.hamcrest'
    }
    testCompile 'org.hamcrest:hamcrest-library:' + hamcrestVersion
    //include in compile only, exclude in the war
    providedCompile 'javax.servlet:javax.servlet-api:' + servletApiVersion

}

//Gretty
buildscript {
  repositories {
    jcenter()
  }

  dependencies {
    classpath 'org.akhikhl.gretty:gretty:1.1.5'
  }
}

gretty {
  port = 9090
  contextPath = 'loanSharks'
  servletContainer = 'tomcat8'
}



//For Eclipse IDE only
eclipse {

  wtp {
    component {

      //define context path, default to project folder name
      contextPath = 'loanSharks'

    }

  }
}

jacoco {
    toolVersion = "0.7.5+"
    reportsDir = file("$buildDir/reports/jacoco")
}

jacocoTestReport {
    reports {
        xml.enabled = true
        html.enabled = true
    }
}

以下是我在web.xml中的jndi条目:

代码语言:javascript
复制
<resource-ref>
        <res-ref-name>jdbc/devcore</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

这是我在spring-config文件中的条目:-

代码语言:javascript
复制
<bean id="dataSource"
    class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/devcore" /> 
        <property name="lookupOnStartup" value="true"/>
        <property name="proxyInterface" value="javax.sql.DataSource"/>
    </bean> 

我还将数据库条目放在META-INF/context.xml文件和META-INF/jetty-env.xml文件中,正如其他相关文章中所建议的那样。所以谁能解释一下为什么我不能使用gretty来运行我的应用程序。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-13 03:17:19

您需要在gretty配置中激活命名:

代码语言:javascript
复制
gretty {
  enableNaming = true
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42190669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档