emacarron / mybatis

Automatically exported from code.google.com/p/mybatis
0 stars 0 forks source link

Better solution to issue 143: Use a reference to SqlSessionFactory in MapperScanner #162

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What version of the MyBatis are you using?
myibatis-3.0.2
myibatis-spring-1.0.0-RC1
package org.mybatis.spring.annotation from trunk, r2940

Please describe the problem.  Unit tests are best!
Only one SqlSessionFactoryBean can be used with MapperScannerPostProcessor 
beans.

What is the expected output? What do you see instead?
I want to have several SqlSessionFactoryBean beans (I have two different 
databases), and still use MapperScannerPostProcessor to autodetect their 
mappers.

Please provide any additional information below.

Since r2717 (issue 143), the mapper scanner does not have a sqlSessionFactory 
property, and instead MapperFactoryBean has @Autowired. The problem wat that 
the SqlSessionBean was created *before* PropertyPlaceholder is processed, so 
${var} don't work in DataSource definition. My solution is, not to pass the 
SqlSessionBean, but it's name, and create the MapperFactoryBean definition with 
BeanDefinitionBuilder.setPropertyReference instead of 
BeanDefinition.setPropertyValue. So no @Autowired is necessary in 
MapperFactoryBean (I suggest to remove it). I have attached 
MapperScannerPostProcessor.class r2940 modified (sorry, no patch).

Original issue reported on code.google.com by boccaro...@gmail.com on 28 Oct 2010 at 5:39

Attachments:

GoogleCodeExporter commented 9 years ago
Hi boccaromel thanks for the idea and the code. We already discussed and tested 
that possibility (using a addPropertyValue also works). But we thought that it 
would be unusual and probably everyone that uses the scanner would also use 
autowiring.  

BTW why would you remove @autowire from MapperFactoryBean? It should be 
harmless.

Original comment by eduardo.macarron on 28 Oct 2010 at 6:59

GoogleCodeExporter commented 9 years ago
Recarding the first point, boccaromel. I do agree that we should provide a way 
to use MapperScanner when usign more than one datasource. 

Please have a look at trunk and send me your opinions.

Original comment by eduardo.macarron on 29 Oct 2010 at 8:21

GoogleCodeExporter commented 9 years ago
Can you create two MapperScannerPostProcessors in Spring, each using a 
different SqlSessionFactory/DataSource and pointing to a different classpath 
location? I haven't tried this, but if it works, that seems to be the easiest 
solution.

Original comment by hpresnall@gmail.com on 29 Oct 2010 at 11:35

GoogleCodeExporter commented 9 years ago
Yes Hunter, it works fine. I have tested it with 2 ds, 2 sessionFactories, 2 
scanners and a JtaTransactionManager and works. 

Original comment by eduardo.macarron on 30 Oct 2010 at 6:06

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago

Original comment by eduardo.macarron on 5 Apr 2012 at 6:31