Skip to content

Commit

Permalink
3장: 컨텍스트와 DI - 수동 DI 적용
Browse files Browse the repository at this point in the history
이 전의 코드는 userDao 에 직접 DI를 적용하는 방법으로도 변경할 수 있다.

1. 이전의 코드 : 스프링의 DI 이용 방법
  장점: 의존관계가 설정파일에 명확하게 드러나고, 싱글톤으로 관리가 가능하다.
  단점: DI 원칙에 부합하지 않는 구체적인 클래스의 관계가 설정에 직접 노출된다.
2. 현재 코드 : 수동 DI 적용
  장점: userDao와 JdbcContext의 관계가 외부에 드러나지 않는다. 해당 전략을 감출 수 있다.
  단점: 싱글톤으로 만들 수 없고, DI 작업을 위한 부가적인 코드가 필요하다.

두 방법 중 어느 것이 더 낫다고 말할 수 없다.
  • Loading branch information
kimdahyeee committed Mar 14, 2020
1 parent d04ad2c commit e188d79
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 13 deletions.
7 changes: 3 additions & 4 deletions src/main/java/com/toby/tobyspring/user/dao/UserDao.java
Expand Up @@ -14,11 +14,10 @@ public class UserDao {
private JdbcContext jdbcContext;

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
this.jdbcContext = new JdbcContext();
this.jdbcContext.setDataSource(dataSource);

public void setJdbcContext(JdbcContext jdbcContext) {
this.jdbcContext = jdbcContext;
this.dataSource = dataSource;
}

public void add(final User user) throws SQLException {
Expand Down
5 changes: 0 additions & 5 deletions src/main/resources/applicationContext.xml
Expand Up @@ -3,10 +3,6 @@
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.xsd">

<bean id="jdbcContext" class="com.toby.tobyspring.user.dao.JdbcContext">
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
Expand All @@ -16,6 +12,5 @@

<bean id="userDao" class="com.toby.tobyspring.user.dao.UserDao">
<property name="dataSource" ref="dataSource"/>
<property name="jdbcContext" ref="jdbcContext"/>
</bean>
</beans>
4 changes: 0 additions & 4 deletions src/test/java/com/toby/tobyspring/user/dao/UserDaoTest.java
Expand Up @@ -30,10 +30,6 @@ public void setup() {
DataSource dataSource = new SingleConnectionDataSource("jdbc:oracle:thin:@localhost:1521:orcl", "dahye", "test", true);
userDao.setDataSource(dataSource);

JdbcContext jdbcContext = new JdbcContext();
jdbcContext.setDataSource(dataSource);
userDao.setJdbcContext(jdbcContext);

user1 = new User("dahyekim", "김다혜", "dahye");
user2 = new User("toby", "토비", "toby");
user3 = new User("whiteship", "백기선", "white");
Expand Down

0 comments on commit e188d79

Please sign in to comment.