spring如何从jar中读取applicationContext.xml配置文件?


给外国朋友:

spring how to find and read applicationContext.xml from jar file?

Is very easy! Put jar file to web-inf/lib directory.  Don't put it to web-inf/classes directory. When you make jar file, check eclipse's "Add directory entries" option.  Because of The classPath include [src(classes)] path,but not include jar file where in  [src(classes)] path. 

很多人喜欢把非jsp部分的纯java代码打包为jar使用,但是如果把applicationContext.xml放在src目录中一起打包的话,很多人表示网站就无法正常运行了。

很多人费劲心机研究如何修改web.xml文件来处理:

 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 

但是,其实这么写并没有什么问题。说起来解决这个问题很简单,你是不是把生成的jar文件放在web-inf/classes目录下了,兄弟,把它放在web-inf/lib目录下试试,是不是一切都好了?

要注意打包时选上“Add directory entries”

 

====================================================

扩展信息:

如果需要用到 .jar 文件中的类或者资源, 必须把 jar 文件的文件名放入类路径, 而不是其(jar)所在的目录。

Java EE规范(Servlet规范)中,web app的web-inf/lib目录下的所有jar文件会自动加入classpath。

jar文件放在web-inf/classes目录下时,并不算在classpath中,因此jar中的applicationContext.xml找不到。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM