Java LocalDate 在 Spring bean 中不工作
Java LocalDate is not working within Spring bean
我正在 Spring 批处理项目中工作。我有一个从 SQL 服务器数据库读取数据的 bean。在读取查询中,我正在设置日期以从 table 中提取信息。问题是我使用 String.format()
方法使用 LocalDate
设置查询日期参数。我必须在当前日期添加一天。 bean 正在初始化,没有任何问题,首先 运行 查询是 运行 根据要求但在连续 运行 期间 LocalDate
仅生成当前日期而不添加 1 天.
@Bean
public JdbcCursorItemReader<SchoolOrders> getOrdersInAdvance(){
JdbcCursorItemReader<SchoolOrders> jdbcCursorReader = new JdbcCursorItemReader();
String query = String.format("SELECT ORDER_NUMBER,STORE_NUMBER,PAYMENTINFO FROM ORDERS WHERE ORDER_DATE=%1$s",new Object[]{LocalDate.now(ZoneID.of("Asia/Kolkata")).plusDays(1)});
jdbcCursorReader .setDataSource(dataSource);
jdbcCursorReader .setSql(query);
jdbcCursorReader .setVerifyCurosrPosition(true);
jdbcCursorReader .setRowMapper(new BeanPropertyMapper<SchoolOrders>(SchoolOrders.class));
returns jdbcCursorReader;
}
我知道 LocalDate
是 immutable 但日期不会在对象级别持久化并立即生成。
谁能帮我解决这个问题!提前致谢。
使用 @Bean
指定 JdbcCursorItemReader<SchoolOrders>
结果是 singleton-scoped bean (default)。因此,为了配置 Spring Bean,方法 getOrdersInAdvance()
仅完整 运行 一次,所有后续调用都将简单地 return 缓存的 bean,除非 - 如您所愿观察到 - 应用程序已重新启动。
如果 Spring Bean 中有一个 date/time 敏感项需要在每次执行 Job
或 Step
时刷新,您可以注释bean方法分别用@JobScope
or @StepScope
,
根据我对你的情况的了解,我建议你先尝试将 JdbcCursorItemReader
配置为 job-scoped。
@Bean
@JobScope
public JdbcCursorItemReader<SchoolOrders> getOrdersInAdvance(){
JdbcCursorItemReader<SchoolOrders> jdbcCursorReader = new JdbcCursorItemReader();
String query = String.format("SELECT ORDER_NUMBER,STORE_NUMBER,PAYMENTINFO FROM ORDERS WHERE ORDER_DATE=%1$s",new Object[]{LocalDate.now(ZoneID.of("Asia/Kolkata")).plusDays(1)});
jdbcCursorReader .setDataSource(dataSource);
jdbcCursorReader .setSql(query);
jdbcCursorReader .setVerifyCurosrPosition(true);
jdbcCursorReader .setRowMapper(new BeanPropertyMapper<SchoolOrders>(SchoolOrders.class));
return jdbcCursorReader;
}
我还建议您阅读 late binding of job and step attributes 的 Spring 批处理文档。
我正在 Spring 批处理项目中工作。我有一个从 SQL 服务器数据库读取数据的 bean。在读取查询中,我正在设置日期以从 table 中提取信息。问题是我使用 String.format()
方法使用 LocalDate
设置查询日期参数。我必须在当前日期添加一天。 bean 正在初始化,没有任何问题,首先 运行 查询是 运行 根据要求但在连续 运行 期间 LocalDate
仅生成当前日期而不添加 1 天.
@Bean
public JdbcCursorItemReader<SchoolOrders> getOrdersInAdvance(){
JdbcCursorItemReader<SchoolOrders> jdbcCursorReader = new JdbcCursorItemReader();
String query = String.format("SELECT ORDER_NUMBER,STORE_NUMBER,PAYMENTINFO FROM ORDERS WHERE ORDER_DATE=%1$s",new Object[]{LocalDate.now(ZoneID.of("Asia/Kolkata")).plusDays(1)});
jdbcCursorReader .setDataSource(dataSource);
jdbcCursorReader .setSql(query);
jdbcCursorReader .setVerifyCurosrPosition(true);
jdbcCursorReader .setRowMapper(new BeanPropertyMapper<SchoolOrders>(SchoolOrders.class));
returns jdbcCursorReader;
}
我知道 LocalDate
是 immutable 但日期不会在对象级别持久化并立即生成。
谁能帮我解决这个问题!提前致谢。
使用 @Bean
指定 JdbcCursorItemReader<SchoolOrders>
结果是 singleton-scoped bean (default)。因此,为了配置 Spring Bean,方法 getOrdersInAdvance()
仅完整 运行 一次,所有后续调用都将简单地 return 缓存的 bean,除非 - 如您所愿观察到 - 应用程序已重新启动。
如果 Spring Bean 中有一个 date/time 敏感项需要在每次执行 Job
或 Step
时刷新,您可以注释bean方法分别用@JobScope
or @StepScope
,
根据我对你的情况的了解,我建议你先尝试将 JdbcCursorItemReader
配置为 job-scoped。
@Bean
@JobScope
public JdbcCursorItemReader<SchoolOrders> getOrdersInAdvance(){
JdbcCursorItemReader<SchoolOrders> jdbcCursorReader = new JdbcCursorItemReader();
String query = String.format("SELECT ORDER_NUMBER,STORE_NUMBER,PAYMENTINFO FROM ORDERS WHERE ORDER_DATE=%1$s",new Object[]{LocalDate.now(ZoneID.of("Asia/Kolkata")).plusDays(1)});
jdbcCursorReader .setDataSource(dataSource);
jdbcCursorReader .setSql(query);
jdbcCursorReader .setVerifyCurosrPosition(true);
jdbcCursorReader .setRowMapper(new BeanPropertyMapper<SchoolOrders>(SchoolOrders.class));
return jdbcCursorReader;
}
我还建议您阅读 late binding of job and step attributes 的 Spring 批处理文档。