Skip to content

Allow to use MyBatisPagingItemReader without InitializingBean#afterPropertiesSet #323

Closed
@MadJlzz

Description

@MadJlzz

Hi, I am currently using MyBatisPagingItemReader along with spring batch in order to read sequentially data from my database.

The thing is that with the current implementation of MyBatisPagingItemReader, I am constraint to create as many bean as I have requests to do. I am actually not happy with that and I'd like to have some system to create an instance of MyBatisPagingItemReader and configure myself like so:

@Autowired
public AccountReader(final SqlSessionFactory sqlSessionFactory) {
  pagingItemReader = new MyBatisPagingItemReader<>();
  pagingItemReader.setSqlSessionFactory(sqlSessionFactory);
  pagingItemReader.setQueryId(QUERY_ID);
  pagingItemReader.setPageSize(10);
}

This is a dummy example but this is what I am trying to achieve.

Now this is actually not possible because of:

/**
 * Check mandatory properties.
 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
 */
@Override
public void afterPropertiesSet() throws Exception {
  super.afterPropertiesSet();
  notNull(sqlSessionFactory, "A SqlSessionFactory is required.");
  sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory, ExecutorType.BATCH); // <-- You're fault
  notNull(queryId, "A queryId is required.");
}

Reference here.

In fact, by instanciating with the constructor and not declaring it as a Bean, Spring wont go through afterPropertiesSet and results.addAll(sqlSessionTemplate.selectList(queryId, parameters)); will throw a NullPointerException

Reference here

What do you think of this ? I could actually do a PR if you want me to try implement this.

Cheers,

Metadata

Metadata

Assignees

Labels

enhancementImprove a feature or add a new feature

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions