티스토리 뷰

소스코딩을 하면서 

JDBC연결에 필요한 username, password, url 같은 접속 정보나 

로컬호스트와 서버의 파일 저장 경로같은 설정을 기존에는 MessageSource기능을 이용해서 사용하였는데..

이 방법의 단점은 자바의 controller단에서 request객체를 이용하여 서버의 url을 가져온 다음에 조건문으로 

localhost나 127.0.0.1, 192.168.x.x 같은 호스트가 포함되어 있으면 로컬이라하고 아니면 서버로 구분한 문자열을 service단으로 넘겨서

이문자열을 플래그 값으로 해서 실서버 환경인지 로컬작업 환경인지를 구분하는 조금 복잡한 구조를 가지고 있었는데..


어떻게 다른 방법이 없나 하다가 profile이라는 기능을 발견하게 되었다.



사용방법은 의외로 간단하면서 유용하다.


- 첫째로 profile을 사용하기 위해서 web.xml에 다음 엘리먼트를 집어넣는다.

<context-param>
		<param-name>spring.profiles.active</param-name>
		<param-value>local</param-value>
	</context-param>

param-value요소에 local이라고 넣었는데 개발자가 구분할 프로필이름을 정해서 넣으면된다.



-두번째로는 profile-config.xml을 정의한다.



내용을 보면

<beans profile="local">

<beans profile="server">

가 있는데 이 profile값들이 앞서 web.xml에 정의한 

<param-value>요소의 값에 따라 일치하는 프로필의 <bean>요소의 인스턴스가 생성될 것이다.

class속성의 값이 CustomProfile인데 이것도 개발자 마음대로 필요한 정보를 변수로 만들어서 get set클래스를 만들면 된다.




- CustomProfile.java

public class CustomProfile { private String filePath; private String imagePath; private String resourcePath; private String siteKey; private String secretKey; public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } public String getImagePath() { return imagePath; } public void setImagePath(String imagePath) { this.imagePath = imagePath; } public String getResourcePath() { return resourcePath; } public void setResourcePath(String resourcePath) { this.resourcePath = resourcePath; } public String getSiteKey() { return siteKey; } public void setSiteKey(String siteKey) { this.siteKey = siteKey; } public String getSecretKey() { return secretKey; } public void setSecretKey(String secretKey) { this.secretKey = secretKey; } }

일반적인 get set클래스이다.




- 마지막으로 설정파일을 web.xml의 contextConfigLocation에 추가한다.

<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring/*-config.xml     </param-value> </context-param>

profile-config.xml 대신해서

와일드카드를 써서 등록했다. 이렇게 등록하면 이 경로에 있는 -config.xml로 끝나는 설정파일들이 전부 설정파라미터로 적용될 것이다.

Comments
최근에 올라온 글
최근에 달린 댓글
TAG
more
Total
Today
Yesterday