ssm错误汇总:Error creating bean with name 'sqlSessionFactory' defined in class path resource

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 "&lt;sqlmapper&gt;" 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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值