Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch01] Use XML as bean configurations.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 authored and Dongho Sim committed Jul 29, 2017
1 parent 1815285 commit 87cb981
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/main/java/ch01/springbook/user/dao/UserDao.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
package ch01.springbook.user.dao;

import ch01.springbook.user.domain.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.sql.*;
import ch01.springbook.user.domain.User;

public class UserDao {

private ConnectionMaker connectionMaker;

public UserDao() {

}

public UserDao(ConnectionMaker connectionMaker) {
this.connectionMaker = connectionMaker;
}

public void setConnectionMaker(ConnectionMaker connectionMaker) {
this.connectionMaker = connectionMaker;
}

public void add(User user) throws ClassNotFoundException, SQLException {
Connection c = connectionMaker.makeNewConnection();

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ch01/springbook/user/dao/UserDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.sql.SQLException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

import ch01.springbook.user.domain.User;

Expand All @@ -12,7 +12,7 @@ public class UserDaoTest {
public static void main(String[] args) throws ClassNotFoundException, SQLException {

ApplicationContext applicationContext =
new AnnotationConfigApplicationContext(DaoFactory.class);
new GenericXmlApplicationContext("applicationContext.xml");
UserDao dao = applicationContext.getBean("userDao", UserDao.class);

User user = new User();
Expand Down
15 changes: 15 additions & 0 deletions src/main/resources/applicationContext.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
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-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<bean id="connectionMaker" class="ch01.springbook.user.dao.SimpleConnectionMaker" />

<bean id="userDao" class="ch01.springbook.user.dao.UserDao">
<property name="connectionMaker" ref="connectionMaker" />
</bean>

</beans>

0 comments on commit 87cb981

Please sign in to comment.