본문 바로가기

목록이 없습니다.

[Spring] 스프링 @Value annotation(어노테이션)

Framework/Spring
    반응형

    Spring FrameworkSpring Framework



    스프링에서 텍스트 파일로 변수값들을 선언하고 싶을 때 (보통 이런 변수값들을 프로퍼티라고 함) 다음의 과정을 거친다.


    1. 프로퍼티 파일을 생성



    • resources/config/config.properties 파일 생성
    • 파일의 내용


      img.path=/home/upload/images/

       



    2. application-servlet.xml 파일에 프로퍼티 파일을 선언


    • beans 선언문에 다음과 같이 util 네임스페이스를 추가한다.
     
    <beans xmlns:util="http://www.springframework.org/schema/util">
    
    </beans>
    
    
    • 프로퍼티 파일을 선언
     
     <util:properties id="config" location="classpath:/config/config.properties"/>


    3. controller에서 선언한 프로퍼티 사용


    • @Value를 사용하면 프로퍼티 값을 가져올 수 있다.

     
    @Value("#{config['img.path']}")
    String imgPath;
    


    참고 : https://dukeom.wordpress.com/

    반응형