Created
January 16, 2013 16:38
-
-
Save ageldama/4548664 to your computer and use it in GitHub Desktop.
Spring Simple DataSource Example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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-2.5.xsd"> | |
| <!-- datasource에 접근할 수 있는 빈 객체 설정 --> | |
| <!-- org.apache.commons.dbcp.BasicDataSource 클래스는 DB Connection Pool을 만들어줌. ㅎㅎ --> | |
| <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource" | |
| destroy-method="close"> <!-- 서버가 내려갈때 close라는 메서드를 부른다. --> | |
| <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> | |
| <property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL1" /> | |
| <property name="username" value="SCOTT" /> | |
| <property name="password" value="TIGER" /> | |
| </bean> | |
| <!-- 니가 사용할 예제 DAO --> | |
| <bean id="myDao" class="jhyun.dao.MyDao"> | |
| <!-- 위에 선언한 ds을 주입하라고 지정. --> | |
| <property name="dataSource" ref="ds"/> | |
| </bean> | |
| </beans> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 요게 애플리케이션 시작하면 일어날 코드. | |
| public static void main(String[] args) { | |
| // 일단 applicationContext.xml 로딩해서 ApplicationContext 객체 얻어냄. | |
| ApplicationContext appCtx = new ClasspathApplicationContext("classpath*:applicationContext.xml"); | |
| // myDao 객체 달라고 요청. | |
| MyDao myDao = (MyDao) appCtx.getBean("myDao"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MyDao { | |
| public DataSource dataSource; | |
| // TODO: 뭐 여기에 dataSource사용해서 SQL 쿼리할 메서드들 잔뜩... | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good post