Skip to content

Commit

Permalink
1장: 스프링의 IoC - 오브젝트 팩토리를 이용한 스프링 IoC
Browse files Browse the repository at this point in the history
ApplicationContext 를 통해, annotation으로 설정하기

어플리케이션컨텍스트는 빈의 생성, 관계설정 등의 제어를 총괄한다.
  • Loading branch information
kimdahyeee committed Mar 9, 2020
1 parent 04c2216 commit fe87562
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
6 changes: 6 additions & 0 deletions build.gradle
Expand Up @@ -20,6 +20,12 @@ dependencies {

/* oracle jdbc driver */
compile files('libs/ojdbc6-11.2.0.3.jar')
compile group: 'cglib', name: 'cglib', version: '2.2.2'
compile group: 'commons-logging', name: 'commons-logging', version: '1.2'
compile group: 'org.springframework', name: 'spring-beans', version: '5.2.1.RELEASE'
compile group: 'org.springframework', name: 'spring-context', version: '5.2.2.RELEASE'
compile group: 'org.springframework', name: 'spring-expression', version: '5.2.4.RELEASE'
compile group: 'org.springframework', name: 'spring-core', version: '5.2.1.RELEASE'

testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.6.0")
testRuntimeOnly("org.junit.platform:junit-platform-engine:1.6.0")
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/toby/tobyspring/MainTest.java
@@ -1,16 +1,17 @@
package com.toby.tobyspring;

import com.toby.tobyspring.user.dao.ConnectionMaker;
import com.toby.tobyspring.user.dao.DUserConnectionMaker;
import com.toby.tobyspring.user.dao.DaoFactory;
import com.toby.tobyspring.user.dao.UserDao;
import com.toby.tobyspring.user.domain.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.sql.SQLException;

public class MainTest {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
UserDao userDao = new DaoFactory().userDao();
ApplicationContext context = new AnnotationConfigApplicationContext(DaoFactory.class);
UserDao userDao = context.getBean("userDao", UserDao.class);

User user = new User();
user.setId("dahyekim");
Expand Down
Expand Up @@ -8,7 +8,7 @@ public class DUserConnectionMaker implements ConnectionMaker {
@Override
public Connection makeNewConnection() throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "dahye", "dahye");
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "dahye", "test");
return connection;
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/toby/tobyspring/user/dao/DaoFactory.java
@@ -1,10 +1,16 @@
package com.toby.tobyspring.user.dao;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DaoFactory {
@Bean
public UserDao userDao() {
return new UserDao(connectionMaker());
}

@Bean
public ConnectionMaker connectionMaker() {
return new DUserConnectionMaker();
}
Expand Down

0 comments on commit fe87562

Please sign in to comment.