> Hello World !!!

     

@syaku

자바 스프링 자원 읽기 혹은 경로 알기 : JAVA Spring Resources path , rootPath

자바 스프링 자원 읽기 혹은 경로 알기 : JAVA Spring Resources path , rootPath

웹서비스 폴더 구조

/build                     <--- 배포경로
    /resources              <--- (1) 웹서비스에서 사용되는 자원 img, js, css 등등
    /WEB-INF
        /classes
        /views
/src
    /main
        /java
        /resources
        /webapp
            /resources       <--- (1) 웹서비스에서 사용되는 자원 img, js, css 등등
            /WEB-INF
                /views        <--- 뷰 템플릿 jsp or ftl or html 등등
            /META-INF
    /test
        /java
        /resources

웹루트 절대경로 얻기

@Autowired private ServletContext servletContext;

logger.debug(servletContext.getRealPath("/"));

결과

test ===> /뭐뭐뭐/머뭐뭐/src/main/webapp

was ===> /뭐뭐뭐/머뭐뭐/build

만약 테스트에서 자바영역의 자원을 찾을라면 아래의 라이브러리를 사용해야 한다.

자원(다양한 타입의 파일들)을 기준으로 경로 찾기

@Autowired private ResourceLoader resourceLoader;

logger.debug(resourceLoader.getResource("classpath:config.properties").getURI().getPath());
logger.debug(resourceLoader.getResource("resources/style.css").getURI().getPath());

첫번째는 /src/main/java/src/main/resources 하위에 있는 자원을 찾는 다. was 인 경우 build/WEB-INF/classes 경로에서 찾는 다.(jar 포함)

classpath*: 은 모든 대상에서 관련된 모두를 찾는 다. 그리고 와일드 카드를 사용할 수 있다.

두번째는 /src/main/webapp 하위에 있는 자원을 찾는 다. was 인 경우 /build 경로에서 찾는 다.

어노테이션을 이용하지 않고 자원 찾기

/src/main/java/src/main/resources 하위에 있는 자원을 찾는 다. was 인 경우 build/WEB-INF/classes 경로에서 찾는 다.(jar 포함)

logger.debug(new ClassPathResource("config.properties").getURI().getPath());

어노테이션을 이용하지 않고 자원을 와일드카드로 찾기

와일드 카드를 사용할 수 있다. ** , * , ?

private final PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();

// 한개
logger.debug(pathMatchingResourcePatternResolver.getResource("절대경로 혹은 classpath:이하경로").getFile().getAbsolutePath());

// 여러개
Resource[] resource = pathMatchingResourcePatternResolver.getResources("절대경로 혹은 classpath:이하경로");




posted syaku blog

Syaku Blog by Seok Kyun. Choi. 최석균.

http://syaku.tistory.com