전자정부 프레임워크에서 .properties
를 사용하는 방법을 알아보도록 한다.
우선 Spring boot
에서 매우 간편하게 배포 시에 매우 편하게 썼었기 때문에 Spring
에서도 찾아보았다.
우선 context-properties.xml
에 두 부분을 추가 해주도록한다
xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"
xmlns:util="http://www.springframework.org/schema/util"
<util:properties id="properties" location="classpath:../../../../globals.properties"/> // 내부일때
<util:properties id="properties" location="file:///C:/tomcat/webapps/globals.properties"/> // 외부일떄
나는 globals.properties
파일을 외부에 빼두고 사용할 것이기 때문에 절대 경로에서 외부경로로 나가게끔 설정하였다.
그럼 우리가 쓰는 프로젝트 바로 바깥부분에 있는 경로로 잡아서 파일의 내부 내용을 가져온다
util:properties의 id
이름으로 파일안에 있는 url을 가져와준다
context-datasource.xml
안에도 넣어서 쓸수있다.
파일에 이렇게 넣어두고 #{properties['db.ip']}
이런식으로 끌어온다
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver"></property>
<property name="url" value="jdbc:postgresql://#{properties['db.ip']}:#{properties['db.port']}/postgres"></property>
<property name="username" value=""></property>
<property name="password" value=""></property>
</bean>