Skip to content

Commit

Permalink
XmlSqlService 자기 참조 빈
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalma committed Nov 1, 2022
1 parent f20dfcc commit 7e67d94
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public SqlRetrievalFailureException(String message) {
public SqlRetrievalFailureException(String message, Throwable cause) {
super(message, cause);
}

public SqlRetrievalFailureException(Throwable cause) {
super(cause);
}
}
2 changes: 2 additions & 0 deletions src/main/java/springbook/chapter07/TestDBConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public UserDaoJdbc userDao(){
public SqlService sqlService() {
XmlSqlService sqlService = new XmlSqlService();
sqlService.setSqlmapFile("sqlmap.xml");
sqlService.setSqlReader(sqlService);
sqlService.setSqlRegistry(sqlService);
return sqlService;
}

Expand Down
40 changes: 36 additions & 4 deletions src/main/java/springbook/chapter07/XmlSqlService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import springbook.chapter07.jaxb.SqlType;
import springbook.chapter07.jaxb.Sqlmap;
import springbook.chapter07.sqlService.SqlNotFoundException;
import springbook.chapter07.sqlService.SqlReader;
import springbook.chapter07.sqlService.SqlRegistry;

import javax.annotation.PostConstruct;
import javax.xml.bind.JAXBContext;
Expand All @@ -11,17 +14,32 @@
import java.util.HashMap;
import java.util.Map;

public class XmlSqlService implements SqlService {
public class XmlSqlService implements SqlService, SqlRegistry, SqlReader {

private final Map<String , String> sqlMap = new HashMap<>();
private SqlReader sqlReader;
private SqlRegistry sqlRegistry;
private String sqlmapFile;

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

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

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

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

@Override
public void read(SqlRegistry sqlRegistry) {
String contextPath = Sqlmap.class.getPackage().getName();
try {
JAXBContext jaxbContext = JAXBContext.newInstance(contextPath);
Expand All @@ -30,7 +48,7 @@ private void loadSql() {
Sqlmap sqlmap = (Sqlmap) unmarshaller.unmarshal(is);

for (SqlType sql : sqlmap.getSql()) {
this.sqlMap.put(sql.getKey(), sql.getValue());
sqlRegistry.registerSql(sql.getKey(), sql.getValue());
}
} catch (JAXBException e) {
// JAXBException은 복구 불가능한 예외이기 때문에 RuntimeException으로 포장해서 던진다
Expand All @@ -40,9 +58,23 @@ private void loadSql() {

@Override
public String getSql(String key) throws SqlRetrievalFailureException {
try {
return this.sqlRegistry.findSql(key);
} catch (SqlNotFoundException e) {
throw new SqlRetrievalFailureException(e);
}
}

@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 SqlRetrievalFailureException(key + "를 이용해서 SQL을 찾을 수 없습니다.");
throw new SqlNotFoundException(key + "에 대한 SQL을 찾을 수 없습니다.");
}
return sql;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package springbook.chapter07.sqlService;

public class SqlNotFoundException extends RuntimeException {
public SqlNotFoundException(String message) {
super(message);
}
}
6 changes: 6 additions & 0 deletions src/main/java/springbook/chapter07/sqlService/SqlReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package springbook.chapter07.sqlService;

public interface SqlReader {

void read(SqlRegistry sqlRegistry);
}
20 changes: 20 additions & 0 deletions src/main/java/springbook/chapter07/sqlService/SqlRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package springbook.chapter07.sqlService;

public interface SqlRegistry {
/**
* SQL을 키와 함께 등록한다.
*
* @param key
* @param sql
*/
void registerSql(String key, String sql);

/**
* 키에 해당하는 SQL을 조회한다.
*
* @param key
* @return 찾은 SQL
* @throws SqlNotFoundException
*/
String findSql(String key) throws SqlNotFoundException;
}

0 comments on commit 7e67d94

Please sign in to comment.