spring 找不到批量初始化数据库脚本

use*_*218 4 tomcat spring-batch

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

批处理启动正常,但在数据库初始化期间 db 脚本未运行。该脚本位于 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>
Run Code Online (Sandbox Code Playgroud)

它给了我以下例外 -

java.io.FileNotFoundException:无法在 org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141) 处打开 ServletContext 资源 [/org/springframework/batch/core/schema-drop-mysql.sql]在 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 更多

The*_*oul 6

请注意,您以两种不同的方式指定两个脚本的位置:一种以 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;
Run Code Online (Sandbox Code Playgroud)

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

@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;
Run Code Online (Sandbox Code Playgroud)

然后像这样使用这些值:

@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;
}
Run Code Online (Sandbox Code Playgroud)


Mic*_*lla 2

我认为这:

<jdbc:script location="jar:file:org/springframework/batch/core/schema-drop-mysql.sql" />
Run Code Online (Sandbox Code Playgroud)

应该是这样的:

<jdbc:script location="classpath:/org/springframework/batch/core/schema-drop-mysql.sql" />
Run Code Online (Sandbox Code Playgroud)