@BeanSqlSessionFactoryBean sqlSessionFactoryBeanOne() {SqlSessionFactoryBean ss = new SqlSessionFactoryBean();ss.setDataSource(dataSourceOne());ss.setTypeAliasesPackage("net.learntechnology.empmaint.domain");return ss;}@BeanMapperScannerConfigurer hsqlDBmapperScannerConfigurer() {MapperScannerConfigurer sc = new MapperScannerConfigurer();sc.setBasePackage("net.learntechnology.empmaint.persistence");sc.setAnnotationClass(OurHsqlDB.class);//******* can't do ://sc.setSqlSessionFactoryBean(sqlSessionFactoryBeanOne());
A factory bean is a trick to set up factories instead of beans when
using XML. In the case of MyBatis we need to build SqlSesionFactories
so instead of using a SqlSessionFactoryBuilder we use an Spring's
SqlSessionFactoryBean.
A factory bean returns and object when getObject() is called. So you should try:
public SqlSessionFactory sqlSessionFactoryBeanOne() {
SqlSessionFactoryBean ss = new SqlSessionFactoryBean();
ss.setDataSource(dataSourceOne());
ss.setTypeAliasesPackage("net.learntechnology.empmaint.domain");
ss.getObject();
}
hope this helps!
2012/2/24 Rick R <ric...@gmail.com>:
2012/2/24 Eduardo Macarron <eduardo....@gmail.com>:
Classpath scanners are special beans. For example, I may be wrong, but
Spring Data JPA uses a similar scanner that is a
JpaRepositoryNameSpaceHandler. That means that it is an XML namespace
processor. Will that work with @Configuration? Not sure but probably
not :)
There is an small change in Spring 1.1.0 suggested by the Spring team.
I am fairly sure it will behave the same but I may give it a try (it
needs MB 3.1.0 still in snapshot).
So if you can, I would try how this works with XML.
2012/2/25 Rick R <ric...@gmail.com>:
@ Rick what are you trying to achieve by creating multiple data
sources, is it multi tenancy ?
If the number of databases is known XML should work fine.
2012/2/25 Rick R <ric...@gmail.com>:
That is exactly the issue that caused our change, but now I recall
that change was made for 1.0.2 so you are using the right version. No
need to go 1.1.0.
If the number of databases is known XML should work fine.
A while ago, I tried the Java config, but had to switch to Java + XML
hybrid configuration after I found of SPR-8269.
I will show you my config, but I am not sure if this is helpful
because I don't use Spring MVC or multiple data sources.
Anyway, my XML config defines the @Configuration bean and the
MapperScannerConfigurer with its dependencies.
<context:annotation-config />
<context:property-placeholder />
<aop:aspectj-autoproxy />
<tx:annotation-driven />
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:MyBatisConfig.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mycompany.mapper" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
... other DBCP properties
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean class="mycompany.AppConfig" />
And the other configurations are done with Java config (not much in my case).
@Configuration
@ComponentScan("mycompany.service")
public class AppConfig {
@Autowired
private Environment env;
@Bean
public SomeBean beanName() {
...
In my web.xml, I have ContextLoaderListener as a listener and
contextInitializerClasses as a context-param which prepares my
Environment.
And that's all, I think (it's been a while since I edited these config files).
Hope this helps,
Iwao
2012/2/28 Rick R <ric...@gmail.com>:
Hi Rick!
I think you need to call explicitly to afterPropertiesSet()
MapperFactoryBean<EmployeeMapper> bean = new
@Bean
public EmployeeMapper employeeMapper() throws Exception {
MapperFactoryBean<EmployeeMapper>();
bean.setMapperInterface(EmployeeMapper.class);
bean.setSqlSessionFactory(sqlSessionFactoryBeanOne());
bean.afterPropertiesSet();
return bean.getObject();
Regarding the use of java config with the scanner, honestly we did not
know if it works or not yet :) I usually do not use the java config
but hope I can have look at this. How do the out-of-the box component
scan work in with java config? is that supported?
<bean class="mycompany.AppConfig" />
And the other configurations are done with Java config (not much in my case).
They use the ComponentScanAnnotationParser for @ComponentScan
and the ContextNamespaceHandler/ComponentScanBeanDefinitionParser for
XML config.
Maybe we should go that strategy and build our own mybatis
namespace... and maybe an @MyBatisMapperScan annotation.
2012/2/27 Rick R <ric...@gmail.com>:
web.xml...
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-config.xml</param-value>
</context-param>
application-config.xml...
<beans ....><import resource="classpath:/services-config.xml"/> <!-- MyBatis config for persistence jar --><bean class="net.learntechnology.empmaint.web.config.WebMvcConfig"/> <!-- Java web config --></beans>
Have a look at:
http://www.mybatis.org/spring/mappers.html
2012/3/1 Rick R <ric...@gmail.com>:
Hi Rick, the scanner runs before the PropertyPlaceHolderConfigurer.
There is a workaround for this in 1.1.0. Instead of setting the
SqlSessionFactory reference you can set the name using
SqlSesionFactoryName. That makes PropertyPlaceHolderConfigurer work
again.
Have a look at:
http://www.mybatis.org/spring/mappers.html
Oh I see try to use 1.1.0 and use sqlSessionFactoryBeanName in lieu of
sqlSessionFactory for setting the MapperScannerConfigurer
I'll see if I could get 1.1.0 working. thanks.
--
Rick R
Just to keep this thread for reference when people are searching...
Using mybatis 3.1 snapshot and mybatis-spring 1.1.0 snapshot, I was
able to at use an xml config for mybatis AND a Java config for my web
app, so thanks for recommending that.
[ Side question(s), I had to download both snapshots and install them
manually. Is there a public snapshots repo for both projects? I
couldn't seem to find a valid one. Also, how close are both to being
officially released? Not trying to be too picky, but since 3.1 and
1.1.0 sound like nice releases it would be great to squeeze in the
ability to use mybatis-spring with a Java config if possible into the
release. Not sure how much work that involves though. ]
2012/3/5 Rick R <ric...@gmail.com>: