Skip to content

Commit

Permalink
의존성 분리 : HashMapSqlRegistry, JaxbXmlSqlReader 구현체 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalma committed Nov 1, 2022
1 parent 7e67d94 commit 6fbe092
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 87 deletions.
23 changes: 21 additions & 2 deletions src/main/java/springbook/chapter07/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
import org.springframework.transaction.interceptor.TransactionInterceptor;
import springbook.chapter06.DummyMailSender;
import springbook.chapter06.factoryBean.MessageFactoryBean;
import springbook.chapter07.sqlService.HashMapSqlRegistry;
import springbook.chapter07.sqlService.JaxbXmlSqlReader;
import springbook.chapter07.sqlService.SqlReader;
import springbook.chapter07.sqlService.SqlRegistry;

import javax.sql.DataSource;
import java.util.Properties;
Expand Down Expand Up @@ -84,8 +88,23 @@ public UserDaoJdbc userDao(){
}

@Bean
public SimpleSqlService sqlService() {
return new SimpleSqlService();
public SqlService sqlService() {
BaseSqlService sqlService = new BaseSqlService();
sqlService.setSqlReader(jaxbXmlSqlReader());
sqlService.setSqlRegistry(hashMapSqlRegistry());
return sqlService;
}

@Bean
public SqlRegistry hashMapSqlRegistry() {
return new HashMapSqlRegistry();
}

@Bean
public SqlReader jaxbXmlSqlReader() {
JaxbXmlSqlReader jaxbXmlSqlReader = new JaxbXmlSqlReader();
jaxbXmlSqlReader.setSqlmapFile("sqlmap.xml");
return jaxbXmlSqlReader;
}

@Bean
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/springbook/chapter07/BaseSqlService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package springbook.chapter07;

import springbook.chapter07.sqlService.SqlNotFoundException;
import springbook.chapter07.sqlService.SqlReader;
import springbook.chapter07.sqlService.SqlRegistry;

import javax.annotation.PostConstruct;

public class BaseSqlService implements SqlService {

private SqlReader sqlReader;
private SqlRegistry sqlRegistry;

public void setSqlReader(SqlReader sqlReader) {
this.sqlReader = sqlReader;
}

public void setSqlRegistry(SqlRegistry sqlRegistry) {
this.sqlRegistry = sqlRegistry;
}

@PostConstruct
public void loadSql() {
this.sqlReader.read(this.sqlRegistry);
}

@Override
public String getSql(String key) throws SqlRetrievalFailureException {
try {
return this.sqlRegistry.findSql(key);
} catch (SqlNotFoundException e) {
throw new SqlRetrievalFailureException(e);
}
}
}
23 changes: 19 additions & 4 deletions src/main/java/springbook/chapter07/TestDBConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.interceptor.TransactionInterceptor;
import springbook.chapter06.DummyMailSender;
import springbook.chapter07.sqlService.HashMapSqlRegistry;
import springbook.chapter07.sqlService.JaxbXmlSqlReader;
import springbook.chapter07.sqlService.SqlReader;
import springbook.chapter07.sqlService.SqlRegistry;

import javax.sql.DataSource;
import java.util.Properties;
Expand Down Expand Up @@ -90,13 +94,24 @@ public UserDaoJdbc userDao(){

@Bean
public SqlService sqlService() {
XmlSqlService sqlService = new XmlSqlService();
sqlService.setSqlmapFile("sqlmap.xml");
sqlService.setSqlReader(sqlService);
sqlService.setSqlRegistry(sqlService);
BaseSqlService sqlService = new BaseSqlService();
sqlService.setSqlReader(jaxbXmlSqlReader());
sqlService.setSqlRegistry(hashMapSqlRegistry());
return sqlService;
}

@Bean
public SqlRegistry hashMapSqlRegistry() {
return new HashMapSqlRegistry();
}

@Bean
public SqlReader jaxbXmlSqlReader() {
JaxbXmlSqlReader jaxbXmlSqlReader = new JaxbXmlSqlReader();
jaxbXmlSqlReader.setSqlmapFile("sqlmap.xml");
return jaxbXmlSqlReader;
}

@Bean
public DataSource dataSource(){
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
Expand Down
81 changes: 0 additions & 81 deletions src/main/java/springbook/chapter07/XmlSqlService.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package springbook.chapter07.sqlService;

import java.util.HashMap;
import java.util.Map;

public class HashMapSqlRegistry implements SqlRegistry {

private Map<String , String> sqlMap = new HashMap<>();

@Override
public void registerSql(String key, String sql) {
sqlMap.put(key, sql);
}

@Override
public String findSql(String key) throws SqlNotFoundException {
String sql = sqlMap.get(key);
if (sql == null) {
throw new SqlNotFoundException(key + "에 대한 SQL을 찾을 수 없습니다.");
}
return sql;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package springbook.chapter07.sqlService;

import springbook.chapter07.jaxb.SqlType;
import springbook.chapter07.jaxb.Sqlmap;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.InputStream;

public class JaxbXmlSqlReader implements SqlReader {

private String sqlmapFile;

public void setSqlmapFile(String sqlmapFile) {
this.sqlmapFile = sqlmapFile;
}

@Override
public void read(SqlRegistry sqlRegistry) {
String contextPath = Sqlmap.class.getPackage().getName();
try {
JAXBContext jaxbContext = JAXBContext.newInstance(contextPath);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
InputStream is = ClassLoader.class.getResourceAsStream("/" + sqlmapFile);
Sqlmap sqlmap = (Sqlmap) unmarshaller.unmarshal(is);

for (SqlType sql : sqlmap.getSql()) {
sqlRegistry.registerSql(sql.getKey(), sql.getValue());
}
} catch (JAXBException e) {
// JAXBException은 복구 불가능한 예외이기 때문에 RuntimeException으로 포장해서 던진다
throw new RuntimeException(e);
}
}
}

0 comments on commit 6fbe092

Please sign in to comment.