spring中的 classpath* 存在可移植性问题
classpath* 的可移植性问题,许多人都应该遇到过了。下面就是一个例子(使用的是spring4.1.5和mybatis3.2.8):
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath*:config/mybatis-config-master.xml" />
<property name="mapperLocations" value="classpath*:config/mappers/master/**/*.xml" />
</bean>
上面的配置,在启动时报错:
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/classpath*:config/mybatis-config-master.xml]
at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141)
at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:358)
at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:340)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
... 49 more
spring在构造sqlSessionFactory这个bean时报错,原因是找不到配置文件:
Could not open ServletContext resource [/classpath*:config/mybatis-config-master.xml]
spring查找的路径是:/classpath*:config/mybatis-config-master.xml
而这个路径根本不是我们想要的,根本就是找错了地方。
但是我们配置文件给出的路径是:classpath*:config/mybatis-config-master.xml
我们将配置文件中下面的配置稍作修改,去掉classpath后面的 *
<property name="configLocation" value="classpath*:config/mybatis-config-master.xml" />
改为:
<property name="configLocation" value="classpath:config/mybatis-config-master.xml" />
之后,启动正常,没有报错。
原因:
The wildcard classpath relies on the getResources()
method of the underlying classloader. As most application servers nowadays supply their own classloader implementation, the behavior might differ especially when dealing with jar files. A simple test to check if classpath*
works is to use the classloader to load a file from within a jar on the classpath: getClass().getClassLoader().getResources("<someFileInsideTheJar>")
. Try this test with files that have the same name but are placed inside two different locations. In case an inappropriate result is returned, check the application server documentation for settings that might affect the classloader behavior.
不同的类加载器 classLoader 在处理 classpath* 时存在可移植性问题。如果在使用 classpath* 时报错,那么就应该去掉 * 直接使用classpath: ,如果还是报错,那么可以去掉classpath直接使用路径,在stackoverflow上有一个例子:http://stackoverflow.com/questions/6035464/could-not-open-servletcontext-resource
classpath* 和 classpath的区别:
classpath* prefix specifies that all classpath resources that match the given name must be obtained (internally, this essentially happens via a ClassLoader.getResources(...)
call), and then merged to form the final application context definition.
1)classpath* 它会搜索所有的 classpath,找到所有符合条件的文件,包括jar文件中的配置文件。而classpath不会到jar文件中去寻找。
2)classpath* 存在可移植性问题,遇到问题时,应该使用classpath.
3)其实一般情况下我们根本没有必要去使用classpath*,直接使用classpath就好了。
spring中的 classpath* 存在可移植性问题的更多相关文章
- Spring配置中的"classpath:"与"classpath*:"的区别研究(转)
Spring配置中的"classpath:"与"classpath*:"的区别研究(转) 概念解释及使用场景: classpath是指WEB-INF文件夹下 ...
- Velocity初探小结--Velocity在spring中的配置和使用
最近正在做的项目前端使用了Velocity进行View层的数据渲染,之前没有接触过,草草过了一遍,就上手开始写,现在又回头细致的看了一遍,做个笔记. velocity是一种基于java的模板引擎技术, ...
- Spring中常见的bean创建异常
Spring中常见的bean创建异常 1. 概述 本次我们将讨论在spring中BeanFactory创建bean实例时经常遇到的异常 org.springframework.beans.fa ...
- Quartz 在 Spring 中如何动态配置时间--转
原文地址:http://www.iteye.com/topic/399980 在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度. 有关调度的实现我就第一就想到了Quartz这个开源 ...
- Spring中文文档
前一段时间翻译了Jetty的一部分文档,感觉对阅读英文没有大的提高(*^-^*),毕竟Jetty的受众面还是比较小的,而且翻译过程中发现Jetty的文档写的不是很好,所以呢翻译的兴趣慢慢就不大了,只能 ...
- [原创]java WEB学习笔记109:Spring学习---spring中事物管理
博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱好 ...
- Spring中的JDBCTemplate
src\dayday\JDBCTestTest package dayday;import com.sun.org.apache.xalan.internal.xsltc.compiler.Templ ...
- 由一个RABBITMQ监听器死循环引出的SPRING中BEAN和MAPPER接口的注入问题
1 @Slf4j 2 @RestController 3 @Component 4 public class VouchersReceiverController implements Message ...
- Spring中加载xml配置文件的六种方式
Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog 因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...
随机推荐
- 基于HTML5技术的电力3D监控应用(三)
继(一)和(二)之后不少,不少网友问我移动终端的使用问题,因为我们项目这次采用Android平板终端,所以我对这方面有点肤浅的研究,这篇分享些项目经验总结,希望对大家有所帮助. 电力3D项目去年底刚立 ...
- IE11之F12 Developer Tools--控制台工具(Console)
前面我们介绍了IE11的Developer Tools中的第一个工具--DOM Explorer,这篇文章介绍第二个工具--控制台(Console),使用控制台工具查看错误和其他信息.发送调试输出.检 ...
- 第一个sprint总结和读后感
总结:通过第一个sprint的冲刺,了解了sprint的整个流程,学会了在一个团队里该如何开展一个项目和分配任务.我们的队团在第一个sprint中没有达到我们预期的效果,我们也做出了反省,原因一是我们 ...
- MVC,如何在视图中声明方法,调用方法?
<div> <!--在视图中申明方法,此方法的类型已经固定为HelperResult--> @helper ShowHello(string s) { <div> ...
- 重构第28 天 重命名bool方法(Rename boolean method)
详解:本文中的”为布尔方法命名”是指如果一个方法带有大量的bool 参数时,可以根据bool 参数的数量,提取出若干个独立的方法来简化参数. 理解: 我们现在要说的重构并不是普通字面意义上的重构,它有 ...
- 重构第4天:降低方法(Push Down Method)
理解:降低方法,就是把基类中的某个方法,提出来放到继承类当中去. 详解: 上一节我们讲了方法的提公,是把多于一个继承类都要用到的方法,提出来放到基类中去,来提高代码的可维护性和重用性.那么这一节,我们 ...
- javaweb学习总结(三十二)——JDBC学习入门
一.JDBC相关概念介绍 1.1.数据库驱动 这里的驱动的概念和平时听到的那种驱动的概念是一样的,比如平时购买的声卡,网卡直接插到计算机上面是不能用的,必须要安装相应的驱动程序之后才能够使用声卡和网卡 ...
- IIS配置ASP.NET和服务器错误页
以下两种方法均为全站出错处理 方法一: 1.在Web.config配置文件中<system.web></system.web>中添加<customErrors mode= ...
- 静默安装、授权及卸载Microsoft SQL Server、NET Framework、Windows Installer 、ArcGIS License Manager、ArcGIS Engine(Silent install、uninstall and Authorization.. .through Setup Factory)基于Setup Factory
通过Setup Factory写的代码大概有1700行,所以就不整理了.思路如下: 静默安装都是通过去Microsoft 和Esri的官网找到静默安装的命令,然后File.Run(...)或者Shel ...
- 爱你.一万年>>数据库基础
嗨!最近学习了一项新的技能,学习了数据库的知识,在这里抛砖引玉,给大家说说,望赐教!!>> 01.首先给大家谈谈为什么要使用数据库? 数据库可以看成是一个永久保存数据的仓库. 可以存储大量 ...