emacarron / mybatis

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

[mybatis-spring] MapperScanner causes property placeholder problems #143

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What version of the MyBatis are you using?
mybatis-spring-RC1

Please describe the problem.

I have a dataSource defined in Spring that looks like the following. Note that 
the values are set using property placeholders.

 <bean id="dataSource" class="org.apache.ibatis.datasource.pooled.PooledDataSource" destroy-method="forceCloseAll">
    <property name="driver" value="${jdbc.driver}" />
    <property name="url" value="${jdbc.url}" />
    ...
  </bean>

This works fine until I try and use MapperScanner at which point I get errors 
about 'cannot load class ${jdbc.driver}'. In other words, Spring is not doing 
property substitution any more.

It seems the underlying issue is that because MapperScanner is a bean post 
processor, it gets loaded very early in Spring startup. As a result, its 
properties also get loaded, including the SqlSesisonFactory and its DataSource. 
This happens _before_ any property resolution method is in place which is why I 
am seeing errors.

The easiest work around is to remove the SqlSessionFactory property from 
MapperScanner so that it can get loaded later in the container startup process. 
For everything else to work, this requires that the sqlSessionFactory property 
is set to @Autowired in MapperFactoryBean. Because autowiring happens after the 
property placeholder stuff is in place, it also fixes my error.

I think overall, this is a net benefit because now we don't have to set 
sqlSessionFactory as a property for anything in the usual case. Spring's 
autowiring takes care of this. This patch makes note of that in 
sample/context.xml.

One other change this patch adds is an addToConfig value in MapperScanner that 
will set the given value in all the MapperFactoryBeans.

Original issue reported on code.google.com by hpresnall@gmail.com on 14 Oct 2010 at 11:27

Attachments:

GoogleCodeExporter commented 9 years ago
Now that I have commit access, I can submit this patch if the team agrees on 
the change.

Original comment by hpresnall@gmail.com on 15 Oct 2010 at 3:40

GoogleCodeExporter commented 9 years ago
Committed as REV 2717

Original comment by hpresnall@gmail.com on 16 Oct 2010 at 1:32

GoogleCodeExporter commented 9 years ago

Original comment by eduardo.macarron on 1 Jan 2011 at 4:42