ssm整合时碰到的一个问题,在springcontext上下文创建SqlSessionFactoryBean(org.mybatis.spring.SqlSessionFactoryBean)时,就会报错,网页显示404错误,控制台错误输出摘抄如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [springapp-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.core.io.Resource[]' for property 'mapperLocations'; nested exception is java.lang.IllegalArgumentException: Could not resolve resource location pattern [com/kuaisuji/mapper/**/*.xml]: ServletContext resource [/com/kuaisuji/mapper/] cannot be resolved to URL because it does not exist
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
仔细看第一行很容易就会发现时创建sqlSessionFactory时出现的错误
Error creating bean with name 'sqlSessionFactory' defined in class path resource [springapp-context.xml]
再看后面
Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.core.io.Resource[]' for property 'mapperLocations'; nested exception is java.lang.IllegalArgumentException: Could not resolve resource location pattern [com/kuaisuji/mapper/**/*.xml]: ServletContext resource [/com/kuaisuji/mapper/] cannot be resolved to URL because it does not exist
是字段‘mapperLocations’出的幺蛾子……
下面是我SqlSessionFactoryBean的配置:
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.kuaisuji.po"></property>
<!-- !!!!预警!!!就是下面这个配置出了问题,这样配是错的!!!!!! -->
<property name="mapperLocations"
value="com/kuaisuji/mapper/**/*.xml"></property>
</bean>
诡异的时这个配置测试时没有问题,可以正常运行……
去查下源码摘取如下:
/**
* Set locations of MyBatis mapper files that are going to be merged into the {@code SqlSessionFactory} configuration
* at runtime.
*
* This is an alternative to specifying "<sqlmapper>" entries in an MyBatis config file. This property being
* based on Spring's resource abstraction also allows for specifying resource patterns here: e.g.
* "classpath*:sqlmap/*-mapper.xml".
*
* @param mapperLocations
* location of MyBatis mapper files
*/
public void setMapperLocations(Resource... mapperLocations) {
this.mapperLocations = mapperLocations;
}
人家的格式是这样的:"classpath*:sqlmap/*-mapper.xml".而我的是这样的:sqlmap/*-mapper.xml;少了个“classpath*:"……
其实写的时候就注意到了这个……但是一般情况下(读取spring.xml)之类的带不带这个都能读取到所以就疏忽了……事实证明单元测试时也没得问题……但是千里之堤毁于蚁穴啊……一次错误就搞的你半死不活的……错不可怕……可怕的是你以为那个错是对的……
最后贴上规范的配置……:
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.kuaisuji.po"></property>
<property name="mapperLocations"
value="classpath*:com/kuaisuji/mapper/**/*.xml"></property>
</bean>