Spring 프레임 워 크 (3) -- Spring 설정 데이터 원본

흔히 볼 수 있 는 데이터 원본 (링크 풀) 은 DBCP, C3P 0, Druid 등 이 있 습 니 다.
C3P 0 데이터 원본 (C3P 0 연결 풀) 의 경우 스프링 이 '사용자 정의 대상 이 아 닌' 에 대한 설정 을 보 여 줍 니 다.
Spring 에서 C3P 0 링크 풀 을 설정 하 는 단계:
1. 데이터 원본 의 좌표 와 데이터 베 이 스 를 가 져 오 는 구동 좌표
 1 
 2 
 3     c3p0
 4     c3p0
 5     0.9.1.2
 6 
 7 
 8 
 9 
10     mysql
11     mysql-connector-java
12     5.1.39
13 

2. 데이터 원본 의 기본 링크 데이터 설정
C3P 0 링크 풀 을 수 동 으로 만 들 고 Spring 설정 C3P 0 링크 풀 을 사용 합 니 다.
Spring 설정 사용: 핵심 설정 파일 에 다음 과 같이 설정 합 니 다.
1 "dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
2     "driverClass" value="com.mysql.jdbc.Driver"/>
3     "jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
4     "user" value="root"/>
5     "password" value="root"/>
6 

최적화 설정: value 후의 값 을 새로운 프로필 jdbc. properties 로 추출 합 니 다.
1 jdbc.driver=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql://localhost:3306/test
3 jdbc.username=root
4 jdbc.password=root

그리고 핵심 프로필 의 내용 을 수정 합 니 다. 핵심 프로필 에 프로필 을 도입 합 니 다.


       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd">
      
    
    
    
    class="com.mchange.v2.c3p0.ComboPooledDataSource">
    
    
    
    




수 동: 테스트 클래스 에서 작성 하여 실행 합 니 다. 일반적으로 이 방법 을 사용 하지 않 습 니 다.
 1 @Test
 2 public void testC3P0() throws Exception {
 3     //     
 4     ComboPooledDataSource dataSource = new ComboPooledDataSource();
 5     //         
 6     dataSource.setDriverClass("com.mysql.jdbc.Driver");
 7     dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
 8     dataSource.setUser("root");
 9     dataSource.setPassword("root");
10     //      
11     Connection connection = dataSource.getConnection();
12     System.out.println(connection);
13 }

3. 데이터 원본 대상 을 만 들 고 데이터 원본 을 사용 하여 연결 자원 을 가 져 오고 연결 자원 을 반환 합 니 다.
테스트 클래스 에서 작성:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource) applicationContext.getBean("dataSource");
Connection connection = dataSource.getConnection();
System.out.println(connection);

데이터 원본 을 성공 적 으로 만 들 었 는 지 실험 할 수 있 습 니 다.

좋은 웹페이지 즐겨찾기