관리 메뉴

nalaolla

BasicDataSource 적용하기 본문

SPRING

BasicDataSource 적용하기

날아올라↗↗ 2016. 3. 14. 17:18
728x90
반응형

BasicDataSource사용하기


BasicDataSource는 Connection Pool역활을 한다.


pom.xml에 다음과 같이 dependency추가

<!-- BasicDataSource -->

<dependency>

<groupId>commons-dbcp</groupId>

<artifactId>commons-dbcp</artifactId>

<version>1.2.2</version>

</dependency>



root-context에 빈 등록


  <!--0. oracle properties -->

  <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<value>classpath:jdbc/oracle.properties</value>

</property>

</bean>


  <!-- DAOImple constructor에 사용되는 인자값 설정 -->

  <bean id="dao" class="test.com.spring03.TestDAOimpl">

<constructor-arg>

<ref bean="dataSource"></ref>

</constructor-arg>

</bean>


<!--1. pom.xml commons-dbcp.jar -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName" value="${oracle.DRIVER_NAME}" />

<property name="url" value="${oracle.URL}" />

<property name="username" value="${oracle.USER_ID}" />

<property name="password" value="${oracle.USER_PW}" />

</bean>



src/main/resources/ --> jdbc/oracle.properties 등록


oracle.DRIVER_NAME=oracle.jdbc.OracleDriver

oracle.URL=jdbc:oracle:thin:@localhost:1521:xe

oracle.USER_ID=test

oracle.USER_PW=hi123456




DAOimpl

  

  DAO java에서 해당 연결객체를 사용할 수 있다.

@Autowired

BasicDataSource bds;

        System.out.println(bds.getDriverClassName());

System.out.println(bds.getUrl());

System.out.println(bds.getUsername());

System.out.println(bds.getPassword());



  1. @Autowired
  2. BasicDataSource bds;
  3.    
  4.     public TestDAOimpl(BasicDataSource dbs) {
  5.         this.DRIVER_NAME = dbs.getDriverClassName();
  6.         this.URL = dbs.getUrl();
  7.         this.USER_ID = dbs.getUsername();
  8.         this.USER_PW = dbs.getPassword();
  9.        
  10.         try {
  11.             Class.forName(DRIVER_NAME);
  12.             System.out.println("Driver successed");
  13.         } catch (ClassNotFoundException e) {
  14.             System.out.println("Driver failed");
  15.             e.printStackTrace();
  16.         }
  17.     }

      

728x90
반응형

'SPRING' 카테고리의 다른 글

log4j 사용 및 적용방법  (0) 2016.03.17
mybatis사용하기  (0) 2016.03.16
JSON 파싱 dependency 추가  (0) 2016.03.08
Spring 한글처리  (0) 2016.03.08
오라클 드라이브 dependency추가  (0) 2016.03.08