티스토리 뷰

반응형


1) configuration.xml 파일 생성 및 작성 (경로 :/프로젝트명/src/main/webapp/WEB-INF/config/configuration.xml)

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd" >

<properties>


<comment>테스트</comment>

<entry key="BLOG_NAME">hiworld</entry>

        <entry key="MY_NAME">deerfrd</entry>


</properties>



2) configuration.xml 파일을 context-common.xml 파일이라던지, action-servlet.xml 파일같은 곳 한곳에 하단의 bold체로 기입된 내용을 추가


<?xml version="1.0" encoding="UTF-8"?>

<beans 

    xmlns:util="http://www.springframework.org/schema/util"

    xsi:schemaLocation="http://www.springframework.org/schema/util 

  http://www.springframework.org/schema/util/spring-util-3.0.xsd">

                         

        <!-- configuration.xml 등록 -->

<util:properties id="config" location="classpath:../config/configuration.xml">

</util:properties>

    

</beans>

 



이렇게 설정된 xml 파일의 BLOG_NAME, MY_NAME 변수값을 다른 java 파일에서 불러오고자 한다.



3) java 파일에서 configuration.xml 에 설정된 변수값 가져와서 사용하기

//configuration.xml 에서 해당 값 불러옴

@Value("#{config['BLOG_NAME']}") String blogName; 

@Value("#{config['MY_NAME']}") String myName; 


위에 저렇게 두개 선언해준 뒤에, 실제로 쓰일 부분에

System.out.println("안녕하세요."+ blogName + "블로그에 오신것을 환영합니다. 제 이름은" + myName + "입니다.");

결과 >> 안녕하세요. hiworld블로그에 오신것을 환영합니다. 제 이름은deerfrd입니다.




반응형
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함
반응형