Skip to content

Allow to use MyBatisPagingItemReader without InitializingBean#afterPropertiesSet #323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MadJlzz opened this issue Oct 26, 2018 · 4 comments
Assignees
Labels
enhancement Improve a feature or add a new feature
Milestone

Comments

@MadJlzz
Copy link

MadJlzz commented Oct 26, 2018

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,

@kazuki43zoo
Copy link
Member

@MadJlzz

Thanks you for your suggestion.
It is designed to use with Spring DI container. Therefore, I propose to call afterPropertiesSet method if you want to use above as follow:

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

@MadJlzz
Copy link
Author

MadJlzz commented Oct 26, 2018

I was almost sure to get this kind of answer but let me explain why I disagree with this solution.

From the Spring documentation:

public interface InitializingBean

Interface to be implemented by beans that need to react once all their properties have been set by a BeanFactory: e.g. to perform custom initialization, or merely to check that all mandatory properties have been set.

So it should be use like said, to customize some object but not the initilization of the class itself. (my understanding)

I rather think of an other solution if possible.

@kazuki43zoo
Copy link
Member

kazuki43zoo commented Oct 26, 2018

@MadJlzz

Thanks for feedback!
I don't think the current implementation is wrong, but I'll welcome to improve it. I'll wait your PR!

Note:
In my opinion, I think that you should be call afterPropertiesSet when you use a class that implements the InitializingBean without DI container.

@kazuki43zoo kazuki43zoo self-assigned this Apr 3, 2019
@kazuki43zoo kazuki43zoo added the enhancement Improve a feature or add a new feature label Apr 3, 2019
@kazuki43zoo kazuki43zoo added this to the 2.0.1 milestone Apr 3, 2019
@kazuki43zoo kazuki43zoo changed the title Relax MyBatisPagingItemReader to allow constructor instanciation Allow to use MyBatisPagingItemReader without afterPropertiesSet Apr 3, 2019
@kazuki43zoo
Copy link
Member

close via #331

@kazuki43zoo kazuki43zoo changed the title Allow to use MyBatisPagingItemReader without afterPropertiesSet Allow to use MyBatisPagingItemReader without InitializingBean#afterPropertiesSet Apr 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improve a feature or add a new feature
Projects
None yet
Development

No branches or pull requests

2 participants