[java] 스프링 클래스 패스 접두사 차이

문서화 여기 가 말한다

이 특수 접두사는 주어진 이름과 일치하는 모든 클래스 경로 리소스를 가져와야하며 (내부적으로 이는 기본적으로 ClassLoader.getResources (…) 호출을 통해 발생 함) 병합 된 다음 최종 응용 프로그램 컨텍스트 정의를 형성하도록 지정합니다.

누군가 이것을 설명 할 수 있습니까?

별표 classpath*:conf/appContext.xmlclasspath:conf/appContext.xml없는 것과 반대로 사용하는 것의 차이점은 무엇입니까?



답변

간단한 정의

이는 classpath*:conf/appContext.xml단순히 클래스 경로의 모든 jar에있는 폴더 아래의 모든 appContext.xml 파일이conf 하나의 큰 애플리케이션 컨텍스트로 선택되어 결합 됨을 의미합니다 .

반대로, 하나의 파일 만classpath:conf/appContext.xml 로드 합니다 . 클래스 패스에서 처음 발견 된 파일 입니다.


답변

classpath*:...와일드 카드 구문을 사용하여 여러 빈 정의 파일에서 응용 프로그램 컨텍스트를 구축 할 때 주로 구문 유용합니다.

예를 들어,을 사용하여 컨텍스트를 구성 classpath*:appContext.xml하면 클래스 경로 appContext.xml에서 호출 된 모든 자원에 대해 클래스 경로가 스캔되고 모든 자원 의 Bean 정의가 단일 컨텍스트로 병합됩니다.

대조적으로, 클래스 경로에서 classpath:conf/appContext.xml하나의 파일 만 호출 appContext.xml됩니다. 둘 이상이 있으면 다른 것들은 무시됩니다.


답변

클래스 경로 * : 그것은을 의미 자원의 목록모든 부하 등의 파일이 클래스 경로에 제시 비어있을 수 목록 및 경우에 그러한 파일이 존재하지 않는 클래스 패스에 다음 응용 프로그램 예외를 throw하지 않습니다는 (단지 오류를 무시합니다).

클래스 경로 : 그것은을 의미 특정 자원첫 번째로드 파일을 클래스 패스에 발견하고 그러한 파일을 클래스 패스에 존재하지 않는 경우는 예외가 발생합니다

java.io.FileNotFoundException: class path resource [conf/appContext.xml] cannot be opened because it does not exist


답변

Spring의 소스 코드 :

public Resource[] getResources(String locationPattern) throws IOException {
   Assert.notNull(locationPattern, "Location pattern must not be null");
   //CLASSPATH_ALL_URL_PREFIX="classpath*:"
   if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) {
      // a class path resource (multiple resources for same name possible)
      if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) {
         // a class path resource pattern
         return findPathMatchingResources(locationPattern);
      }
      else {
         // all class path resources with the given name
         return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));
      }
   }
   else {
      // Only look for a pattern after a prefix here
      // (to not get fooled by a pattern symbol in a strange prefix).
      int prefixEnd = locationPattern.indexOf(":") + 1;
      if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) {
         // a file pattern
         return findPathMatchingResources(locationPattern);
      }
      else {
         // a single resource with the given name
         return new Resource[] {getResourceLoader().getResource(locationPattern)};
      }
   }
}  


답변