2017-02-12 4 views
0

Я делаю приложение mvc весны, используя gradle как инструмент построения, а Mysql - как источник данных jndi. Я могу запустить его, создав войну и развернув ее вручную. Но когда я пытаюсь запустить его с помощью gretty я получаю следующее исключениеjavax.naming.NoInitialContextException с mysql DataSource и gretty

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: -

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 
    } 
} 

Вот мой вход JNDI в web.xml: -

<resource-ref> 
     <res-ref-name>jdbc/devcore</res-ref-name> 
     <res-type>javax.sql.DataSource</res-type> 
     <res-auth>Container</res-auth> 
    </resource-ref> 

Это мой вход в весенне-файле конфигурации: -

<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 для запуска моего приложения.

ответ

1

Вам нужно активировать именования внутри конфигурации gretty:

gretty { 
    enableNaming = true 
}