spring 配置文件中的 classpath*

0,
There is special support for retrieving multiple class path resources with the same name, via the "classpath*" prefix. For example, "classpath*:/beans.xml" will find all beans.xml files in the class path, be it in "classes" directories or in JAR files. This is particularly useful for auto-detecting config files.

1,
Hmm..After reading PathMatchingResourcePatternResolver, it looks like classpath* prefix should work with wildcards in mutliple JARs. My test showed it worked for contexts in WEB/classes, but not with a mix in WEB-INF/classes, and some in JARs in WEB-INF/lib. Maybe a bug?

Just updating this post...
OK - just to clarify after feedback from Juergen. classpath*: and wildcards do work, but not from the root directory within JARs. So for application contexts in the classpath at:

/com/company/applicationContext.xml
/com/company/applicationContext-ds.xml

If they're in WEB-INF/classes the following will work:
classpath*:**/applicationContext*.xml

If they're in a JAR in WEB-INF/lib the following will work:
classpath*:/com/**/applicationContext*.xml

2,
When you use 'classpath:' for an Ant style wildcard search, Spring uses a single classpath directory for the search. The documentation is vague, but it seems the directory returned will be the first one provided by ClassLoader.getResources(""). In our case, it returned the '/target/test-classes' directory that contains applicationTest.xml and our test classes, instead of '/target/classes' which contains application.xml and all the *.hbm.xml files.

Using the 'classpath*:' prefix fixes the problem. It indicates that the resource loader should look in all directories on the classpath, so making this change solved our problem. Apparently Spring maintains both prefixes because limitations in the Classloader (at the specification level) make it difficult to search for resources in the classpath root when performing wildcard searches across all classpath directories. This suggest it might be good practice to always create a directory to contain resources you might otherwise want to put in the classpath root and always use the 'claspath*:' prefix.
發佈了23 篇原創文章 · 獲贊 9 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章