2017년 3월 22일 수요일

Spring properties 설정하기

1. beans에 스키마를 추가한다.

2개를 추가한다.

http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd

최종 xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util-4.0.xsd">

  <util:properties id="global" location="classpath:config/properties/global.properties" />  
    
</beans>

2. properties 파일에 설정을 추가한다.


dbPool.driverClassName=com.mysql.jdbc.Driver
dbPool.url=jdbc:mysql://주소:포트/데이터베이스명
dbPool.userName = 아이디
dbPool.password = 비밀번호

3. 사용하는 xml에 적용한다.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/jdbc  http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
     
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="#{global['jdbc.driverClassName']}"/>
        <property name="url" value="#{global['jdbc.url']}"/>
        <property name="username" value="#{global['jdbc.userName']}"/>
        <property name="password" value="#{global['jdbc.password']}"/>
    </bean>
</beans>

스프링에서@Value("#{global['jdbc.username']}") 으로도 사용가능하다.
Share:

0 개의 댓글:

댓글 쓰기