티스토리 뷰
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 파일에서 불러오고자 한다.
//configuration.xml 에서 해당 값 불러옴 @Value("#{config['BLOG_NAME']}") String blogName; @Value("#{config['MY_NAME']}") String myName; 위에 저렇게 두개 선언해준 뒤에, 실제로 쓰일 부분에 System.out.println("안녕하세요."+ blogName + "블로그에 오신것을 환영합니다. 제 이름은" + myName + "입니다."); |
결과 >> 안녕하세요. hiworld블로그에 오신것을 환영합니다. 제 이름은deerfrd입니다.
'Spring' 카테고리의 다른 글
스프링 웹 프로젝트 복사해서 프로젝트명 바꿔서 재활용하기 (0) | 2017.11.14 |
---|---|
redirect 경로에 관하여 (0) | 2017.04.05 |
spring MVC session interceptor 예외처리(Spring3.2 & 3.1이하 ) (0) | 2017.02.20 |
Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/codehaus/jackson/map/ObjectMapper (0) | 2017.02.12 |
스프링 초기설정(sts + maven) (0) | 2017.02.08 |