spring加载资源文件中classpath*与classpath的区别

我们在做Spring和Mbatis集成的时候,需要在配置中配置mapperLocations.
我需要加载多个包下的mapper.xml文件.
比如加载cn/wolfcode/base/mapper文件夹和cn/wolfcode/bussiness/mapper文件夹下所有的mapper.xml文件.
一开始文件配置如下:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <property name="configLocation" value="classpath:mybatis-config.xml" />  
        <property name="typeAliasesPackage" value="cn.wolfcode.base.domain;cn.wolfcode.business.domain" />  
        <property name="mapperLocations" value="classpath:cn/wolfcode/*/mapper/*Mapper.xml;" />  
</bean> 

结果一直报cn/wolfcode/base/mapper下的mapper文件的某个方法找不到.
错误:Invalid bound statement (not found)

修改成这样就可以找到了.

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <property name="configLocation" value="classpath:mybatis-config.xml" />  
        <property name="typeAliasesPackage" value="cn.wolfcode.base.domain;cn.wolfcode.business.domain" />  
        <property name="mapperLocations" value="classpath*:cn/wolfcode/*/mapper/*Mapper.xml;" />  
</bean> 

原因是classpath:和classpath*:在spring加载资源的时候是不同的.

classpath:只能加载找到的匹配的第一个资源目录下文件.(上面只匹配了cn/wolfcode/bussiness/mapper下的mapper文件,而cn/wolfcode/base/mapper就被忽略了)

classpath*:能加载多个路径下的资源目录下文件.(cn/wolfcode/bussiness/mapper和cn/wolfcode/base/mapper中的mapper.xml文件都被加载进来了.)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容