【问题标题】:spring can't find batch initialization db scriptspring找不到批量初始化db脚本
【发布时间】:2016-05-18 14:56:58
【问题描述】:

我已经通过 tomcat 中的 war 文件部署了一个 spring 批处理。 我在服务器启动时使用 ContextListener 运行批处理。

批处理启动正常,但在数据库初始化期间数据库脚本未运行。 该脚本位于 WEB-INF/lib 文件夹中的 jar 文件中。 这是来自配置 xml 的代码部分 -

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

它给了我以下异常-

java.io.FileNotFoundException: 无法打开 ServletContext 资源 [/org/springframework/batch/core/schema-drop-mysql.sql] 在 org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141) 在 org.springframework.core.io.support.EncodedResource.getReader(EncodedResource.java:132) 在 org.springframework.jdbc.datasource.init.ScriptUtils.readScript(ScriptUtils.java:278) 在 org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:438) ... 32 更多

【问题讨论】:

  • 缺少代码部分 -

标签: tomcat spring-batch


【解决方案1】:

请注意,您以两种不同的方式指定两个脚本的位置:一种以jar:file:org/springframework/... 开头,另一种直接:org/springframework/...

也许当您进行其他人建议的更改时,您只对其中一个位置进行了更改?

但无论如何,我面临着同样的问题。添加classpath: 作为前缀为我修复了它。我正在使用 Java 配置来注入脚本位置。早些时候它是(“不工作的情况”):

@Value("org/springframework/batch/core/schema-drop-postgresql.sql")
private Resource dropRepositoryTables;

@Value("org/springframework/batch/core/schema-postgresql.sql")
private Resource dataRepositorySchema;

但是在更改为以下内容时,它起作用了:

@Value("classpath:org/springframework/batch/core/schema-drop-postgresql.sql")
private Resource dropRepositoryTables;

@Value("classpath:org/springframework/batch/core/schema-postgresql.sql")
private Resource dataRepositorySchema;

然后像这样使用这些值:

@Bean
public DataSourceInitializer dataSourceInitializer(DataSource dataSource) throws MalformedURLException {

    ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
    databasePopulator.addScript(dropRepositoryTables);
    databasePopulator.addScript(dataRepositorySchema);
    databasePopulator.setIgnoreFailedDrops(false);

    DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDataSource(dataSource);
    initializer.setDatabasePopulator(databasePopulator);

    return initializer;
}

【讨论】:

    【解决方案2】:

    我认为是这样的:

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

    应该是这样的:

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

    【讨论】:

      【解决方案3】:

      试试这几行。在我的情况下,这些工作正常:

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

      【讨论】:

      • 不适合我。您是否将其部署为对 tomcat 的战争?你是如何运行你的批处理的?通过 servlet 或上下文侦听器?
      【解决方案4】:

      这对我有用

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

      我使用 maven 作为构建工具并部署在 tomcat 上。

      【讨论】:

        猜你喜欢
        • 2016-05-27
        • 2020-02-18
        • 2012-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-22
        • 1970-01-01
        相关资源
        最近更新 更多