运行spring batch war文件时出错

时间:2016-05-25 07:08:18

标签: maven tomcat spring-batch

我创建了一个弹簧批处理项目,并使用maven为

创建了一个war文件
 <modelVersion>4.0.0</modelVersion>
  <groupId>com.batch</groupId>
  <artifactId>dms-batch-app</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>dms-batch-app Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
        <jdk.version>1.8</jdk.version>
        <spring.version>4.0.6.RELEASE</spring.version>
        <spring.batch.version>3.0.7.RELEASE</spring.batch.version>
        <quartz.version>1.8.5</quartz.version>

  <build>
    <finalName>dms-batch-app</finalName>
    </build>

我能够生成war文件,但是当我在tomcat服务器上部署并启动tomcat应用程序时,它会抛出以下错误

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.jdbc.datasource.init.DataSourceInitializer#0': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.CannotReadScriptException: Cannot read SQL script from ServletContext resource [/jar:file:org/springframework/batch/core/schema-drop-mysql.sql]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/jar:file:org/springframework/batch/core/schema-drop-mysql.sql]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5068)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5584)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1260)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:2002)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.jdbc.datasource.init.CannotReadScriptException: Cannot read SQL script from ServletContext resource [/jar:file:org/springframework/batch/core/schema-drop-mysql.sql]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/jar:file:org/springframework/batch/core/schema-drop-mysql.sql]
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:441)
    at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:208)
    at org.springframework.jdbc.datasource.init.CompositeDatabasePopulator.populate(CompositeDatabasePopulator.java:61)
    at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:49)
    at org.springframework.jdbc.datasource.init.DataSourceInitializer.execute(DataSourceInitializer.java:108)
    at org.springframework.jdbc.datasource.init.DataSourceInitializer.afterPropertiesSet(DataSourceInitializer.java:93)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
    ... 25 more
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/jar:file:org/springframework/batch/core/schema-drop-mysql.sql]
    at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141)
    at org.springframework.core.io.support.EncodedResource.getReader(EncodedResource.java:132)
    at org.springframework.jdbc.datasource.init.ScriptUtils.readScript(ScriptUtils.java:278)
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:438)
    ... 32 more

我连接到xml上下文中的数据库,如下所示

<jdbc:initialize-database data-source="dataSource">
 <jdbc:script location="org/springframework/batch/core/schema-drop-mysql.sql" />
<jdbc:script location="org/springframework/batch/core/schema-mysql.sql" />
  </jdbc:initialize-database>

但是根据错误,战争无法从jar文件中读取。我也修改了同样的东西,并将ddl脚本保存在resources文件夹下,但战争再次从jar文件本身中选择ddl脚本。

请为此提供解决方案。

而且,将弹出批处理项目部署为jar文件或war文件会有什么更好的选择呢?

请帮助。

1 个答案:

答案 0 :(得分:1)

试试这个版本:

  1. <jdbc:script location="/org/springframework/batch/core/schema-drop-mysql.sql" /> <jdbc:script location="/org/springframework/batch/core/schema-mysql.sql" />
  2. <jdbc:script location="classpath:/org/springframework/batch/core/schema-drop-mysql.sql" /> <jdbc:script location="classpath:/org/springframework/batch/core/schema-mysql.sql" />
  3. <jdbc:script location="jar:file:org/springframework/batch/core/schema-drop-mysql.sql" /> <jdbc:script location="jar:file:org/springframework/batch/core/schema-mysql.sql" />