사용자 도구

사이트 도구


java:classpathresource

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판 양쪽 다음 판
java:classpathresource [2019/11/22 13:26]
kwon37xi
java:classpathresource [2019/11/22 13:28]
kwon37xi
줄 12: 줄 12:
 ===== SpringFramework Resource 사용 ===== ===== SpringFramework Resource 사용 =====
   * [[:springframework|Spring Framework]]의 [[https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/ResourceLoader.html|ResourceLoader]] 를 사용하거나 [[https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/ClassPathResource.html|ClassPathResource]] 같은 [[https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/Resource.html|Resource]]의 구현체를 직접 사용해도 된다.   * [[:springframework|Spring Framework]]의 [[https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/ResourceLoader.html|ResourceLoader]] 를 사용하거나 [[https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/ClassPathResource.html|ClassPathResource]] 같은 [[https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/Resource.html|Resource]]의 구현체를 직접 사용해도 된다.
 +  * [[https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/ResourceUtils.html|ResourceUtils]]도 가능하다.
  
 <code java> <code java>
 Resource targetFileResource = new ClassPathResource("/package/to/myfile.txt"); Resource targetFileResource = new ClassPathResource("/package/to/myfile.txt");
 File targetFile = targetFileResource.getFile(); File targetFile = targetFileResource.getFile();
 +
 +// 혹은
 +File employeesFile = ResourceUtils.getFile("classpath:data/employees.dat");
 </code> </code>
 +
   * **classpath** 상의 리소스에 대해서 ''getFile()'' 은 매우 주의해야한다. ''jar'' 안에 묶여 있을 경우 ''getFile()''이 동작하지 않기 때문이다.   * **classpath** 상의 리소스에 대해서 ''getFile()'' 은 매우 주의해야한다. ''jar'' 안에 묶여 있을 경우 ''getFile()''이 동작하지 않기 때문이다.
   * 따라서 ''getFile()'' 은 Test 코드 등에서만 사용하고, 실제 운영에서는 ''InputStream'' 으로만 읽도록 한다.   * 따라서 ''getFile()'' 은 Test 코드 등에서만 사용하고, 실제 운영에서는 ''InputStream'' 으로만 읽도록 한다.
 +   
 +===== 참고 ===== 
 +  * [[https://www.baeldung.com/spring-classpath-file-access|Access a File from the Classpath using Spring | Baeldung]]
java/classpathresource.txt · 마지막으로 수정됨: 2019/11/22 13:32 저자 kwon37xi