Michael Lanyon's Blog Notes and thoughts from LanyonM

MyBatis Spring Java Config Contribution

Comments

MyBatis Spring

As you may have guessed from a previous post, I prefer Java Config. At times in the past I’ve been forced to go with what’s currently supported instead of spending the time necessary to add the feature I’d like to have. With the Spring Grabbag project, I have that time.

The current mybatis-spring project (v1.1.1) only has support for XML-defined config. Some searching lead me to java/xml config hybrids, like the one described here, but that seemed too much like the compromise I’d been forced to make in the past. Some more targeted searching lead me to this issue on the MyBatis issue tracker, which in turn lead me to a more technically robust ticket on SpringSource’s Jira instance.

At this point I knew:

  • there was no code yet-written to satisfy my predicament
  • that the SpringSource folks expected the MyBatis-Spring code to follow the established @Enable pattern
  • that Chris Beams had very graciously written code that reproduced the situation and committed it to the spring-framework-issues repo

Now all I had to do was learn the Spring bean lifecycle and write the solution. While the documentation is great, it’s also a bit dense. I chose to read up on EnableWebMvc, ComponentScanAnnotationParser, AspectJAutoProxyRegistrar, and BatchConfigurationSelector and more specifically how configurers, adapters and registrars are used in the context of Spring annotations. Given that the MapperScanner is registering new beans to the application context, adding a registrar made most sense. I had been in contact with Eduardo Macarron, the maintainer of the MyBatis-Spring project, and he altered the code as necessary to be accepted into the code base.

It feels good to be able to @MapperScan my DataConfig and have it work like you’d hope and expect. It feels great to meaningfully contribute back to a project I’ve relied on so much in the past.