---
title: "Spring中使用classpath*加载配置文件，jar包中的配置文件不加载问题"
author: "lxsgoodluck"
date: "2009-11-18T18:14:07Z"
tags: []
slug: "spring-classpath-jar"
draft: false
lastmod: "2009-11-20T15:50:19Z"
id: 519844
url: "https://webwork.iteye.com/blog/519844"
---

jar中配置文件放在“/”(根目录)下时，通过classpath\*加载配置文件，jar包中的配置文件不会加载，

这是因为Spring使用classpath加载配置文件时需要借助JDK的ClassLoader.getResources([String]() name)方法，而该方法有一个局限：当传入的参数为空字符串时，即我们本意是想从根目录获取文件，这时JDK只会返回存在于文件系统中的资源，而在jar包中的资源并不会被返回。

解决方法是将配置文件放在根的下一级目录内，例如/conf/application-context.xml，web.xml中配置为classpath\*:conf/\*\*/\*application-context.xml。