My class extends from MapperFactoryBean, which extends from SqlSessionDaoSupport. All 3 classes are instantiated by Spring through their Autowiring. The problem is that SqlSessionDaoSupport contains these two methods:
@Autowired(required = false)
public final void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
if (!this.externalSqlSession) {
this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);
}
}
@Autowired(required = false)
public final void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSession = sqlSessionTemplate;
this.externalSqlSession = true;
}
These methods take a SqlSessionFactory and a SqlSessionTemplate, both of which are tied to a particular database configuration. If you have more than 1 database configured in your application, the Autowiring fails because it can't figure out which SqlSessionFactory and SqlSessionTemplate definition to use. Since these methods don't take qualifiers and they're final, there is no way to get around this.
I removed the finals, compiled a new spring-mybatis jar, and override these methods in my own classes which do take qualifiers to identify which database configuration to use. This works for me, but I don't think there's a fix out there in the wild.