有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring配置问题:无法找到XML架构命名空间的Spring NamespaceHandler,有问题的资源:类路径

我被困在一个我无法解决的问题上。我有一个spring批处理应用程序,由Quartz调度器和Gradle提供支持。当地一切正常。但是当我使用Gradle构建jar并尝试在下面运行它时,会抛出异常

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)

我试图将xsd文件放在本地,但仍然出现了这个错误。下面是我的申请表。上下文文件

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

我正在使用gradle构建jar文件,并复制jar中的所有依赖项。下面是我的身材。格雷德尔档案

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

我的主课

    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);

    }

我只是不确定,为了运行可执行jar,我应该做哪些更改。我正在使用bat文件和-jar命令运行jar。请帮帮我,伙计们,没什么进展,我只是被卡住了


共 (2) 个答案

  1. # 1 楼答案

    我无意中发现了这篇文章。希望这对你有意义

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

    它说你应该有transformer配置

    <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>
    
  2. # 2 楼答案

    如前所述,你的URL是错误的。他们缺少一个/。正确的标题应为:

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

    上面的配置缺少contextspring-context-4.3.xsd之间的/