-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
의존성 분리 : HashMapSqlRegistry, JaxbXmlSqlReader 구현체 추가
- Loading branch information
Showing
6 changed files
with
134 additions
and
87 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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); | ||
} | ||
} | ||
} |
This file contains 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
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
src/main/java/springbook/chapter07/sqlService/HashMapSqlRegistry.java
This file contains 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
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; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/springbook/chapter07/sqlService/JaxbXmlSqlReader.java
This file contains 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
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); | ||
} | ||
} | ||
} |