Spring Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace, Offending resource: class path

情到浓时终转凉″ 提交于 2020-06-23 05:12:17

问题


I am stuck at an issue which I am unable to resolve. I have spring batch application powered with Quartz scheduler and Gradle. Everything is working fine locally. But when I'm building the jar using Gradle and trying to run it below exceptions are thrown.

Jun 09, 2020 5:14:01 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@15551852: startup date [Tue Jun 09 17:14:01 IST 2020]; root of context hierarchy
Jun 09, 2020 5:14:01 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/context]
Offending resource: class path resource [applicationContext.xml]

        at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)
        at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:118)

I tried to place the xsd file locally, but still getting this error. Below is my application.context file.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:batch="http://www.springframework.org/schema/batch"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    spring-beans-4.3.xsd
    http://www.springframework.org/schema/context
    spring-context-4.3.xsd
    http://www.springframework.org/schema/tool
    spring-tool.xsd
    http://www.springframework.org/schema/batch
    spring-batch-3.0.xsd
    http://www.springframework.org/schema/tx 
    spring-tx.xsd">

    <context:component-scan base-package="org.*.*" />

    <bean id="transactionManager"
        class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

    <bean id="jobRepository"
        class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
        <property name="transactionManager" ref="traJnsactionManager" />
    </bean>

    <bean id="jobLauncher"
        class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository" />
    </bean>

    <batch:job id="compareVersion">
      <batch:step id="step1">
        <batch:tasklet ref = "versionComparisonTasklet" />
      </batch:step>
    </batch:job> 

I am using gradle to build the jar file and copying all the dependencies within the Jar. Below is my build.gradle file.

group "org.*.*"
apply plugin: 'java'

sourceCompatibility = 1.8
version = '1.0'
jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart',
        'Implementation-Version': version
    }
    baseName = 'BatchJob'
}

dependencies {
    compile 'com.oracle:ojdbc6:11.2.0.4.0'
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
    compile group: 'org.springframework', name: 'spring-core', version: '4.3.12.RELEASE'
    compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.12.RELEASE'
    compile group: 'org.springframework', name: 'spring-orm', version: '4.3.12.RELEASE'
    compile group: 'org.springframework', name: 'spring-context', version: '4.3.12.RELEASE'
    compile group: 'org.springframework', name: 'spring-context-support', version: '4.3.12.RELEASE'
    compile group: 'org.springframework.batch', name: 'spring-batch-core', version: '3.0.8.RELEASE'
    compile group: 'org.springframework', name: 'spring-oxm', version: '4.3.12.RELEASE'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.9.1'
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.9.1'
    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.12.Final'
    compile group: 'org.hibernate.validator', name: 'hibernate-validator', version: '6.0.3.Final'
    compile group: 'org.hibernate', name: 'hibernate-c3p0', version: '5.2.12.Final'
    compile group: 'javax.mail', name: 'mail', version: '1.4.4'
    testCompile group: 'junit', name: 'junit', version: '4.+'
    testCompile group: 'org.springframework', name: 'spring-test', version: '4.3.12.RELEASE'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.2'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.9.2'
    compile group: 'org.thymeleaf', name: 'thymeleaf-spring4', version: '3.0.9.RELEASE'
    compile group: 'org.thymeleaf', name: 'thymeleaf', version: '3.0.9.RELEASE'
    compile group: 'aopalliance', name: 'aopalliance', version: '1.0'
    compile group: 'cglib', name: 'cglib', version: '2.2'
    compile group: 'asm', name: 'asm', version: '3.1'
    compile group: 'com.sun.xml.bind', name: 'jaxb-impl', version: '2.2.11'
    compile group: 'com.sun.xml.bind', name: 'jaxb-core', version: '2.2.11'
    compile group: 'commons-logging', name: 'commons-logging', version: '1.2'
    compile group: 'com.sun.xml.wss', name: 'xws-security', version: '3.0'
    compile group: 'org.apache.ws.security', name: 'wss4j', version: '1.6.19'
    compile group: 'com.thoughtworks.xstream', name: 'xstream', version: '1.3.1'
    compile group: 'joda-time', name: 'joda-time', version: '2.9.9'

    xjc 'com.sun.xml.bind:jaxb-impl:2.1.12'
    xjc 'com.sun.xml.bind:jaxb-xjc:2.1.12'
    xjc 'javax.xml.bind:jaxb-api:2.2.2'
}



buildDistributionZip {

    into('dist/config/dpw/cdi-org-structure-ws-v1/wsdl') {
        from ("${projectDir}/misc/resources/schemas/cdi-org-structure-ws-v1/wsdl") {
        }
    }
}

test { systemProperties 'property': 'value' }

uploadArchives {
    repositories { flatDir { dirs 'repos'
        } }
}

task copyToJar(type: Jar) {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart',
        'Implementation-Version': version
    }
    baseName = project.name + '-dependencies'
    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
    with jar
}



jar {
manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart',
        'Implementation-Version': version,
        'Main-Class': 'org..MyJobScheduler'
  }
    baseName = 'batchjob'

  from {
    configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
  }
}


task runBatchJob(type: JavaExec) {
    main = 'org.springframework.batch.core.launch.support.CommandLineJobRunner'
    classpath = sourceSets.test.runtimeClasspath
    args = ["applicationContext.xml", "versionComparisonTasklet"]
}

My Main Class

    public static void main(String[] args) throws SchedulerException {  
    JobDetail j=JobBuilder.newJob(MyJobBuilder.class).build();  
    Trigger t=TriggerBuilder.newTrigger().withIdentity("CroneTrigger").withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInHours(72).repeatForever()).build();    
    Scheduler s=StdSchedulerFactory.getDefaultScheduler();
    s.start();
    s.scheduleJob(j,t);

    }

I am just not sure, what changes I am supposed to make to run the executable jar. I am running the jar using bat file with -jar command. Please help me guys, unable to make any progress, I am just stuck.


回答1:


As posted, your URLs are wrong. They are missing a /. The correct header should be:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:batch="http://www.springframework.org/schema/batch"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/tool
    http://www.springframework.org/schema/tool/spring-tool.xsd
    http://www.springframework.org/schema/batch
    http://www.springframework.org/schema/batch/spring-batch-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

Your configuration above was missing the / between context and spring-context-4.3.xsd.




回答2:


I bumped on this article. Hope this make some sense for you.

https://robert-reiz.com/2011/11/14/832/

It says you should have transformer configuration

<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
  <resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
  <resource>META-INF/spring.schemas</resource>
</transformer>


来源:https://stackoverflow.com/questions/62282842/spring-configuration-problem-unable-to-locate-spring-namespacehandler-for-xml-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!