Skip to content

Commit

Permalink
중복된 메소드 BaseSqlService에게 위임
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalma committed Nov 2, 2022
1 parent 6121c67 commit d5ca2a5
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/main/java/springbook/chapter07/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import springbook.chapter06.factoryBean.MessageFactoryBean;
import springbook.chapter07.sqlService.HashMapSqlRegistry;
import springbook.chapter07.sqlService.JaxbXmlSqlReader;
import springbook.chapter07.sqlService.OxmSqlService;
import springbook.chapter07.sqlService.SqlReader;
import springbook.chapter07.sqlService.SqlRegistry;

Expand Down Expand Up @@ -91,9 +92,8 @@ public UserDaoJdbc userDao(){

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

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/springbook/chapter07/TestDBConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import springbook.chapter06.DummyMailSender;
import springbook.chapter07.sqlService.HashMapSqlRegistry;
import springbook.chapter07.sqlService.JaxbXmlSqlReader;
import springbook.chapter07.sqlService.OxmSqlService;
import springbook.chapter07.sqlService.SqlReader;
import springbook.chapter07.sqlService.SqlRegistry;

Expand Down Expand Up @@ -96,10 +97,9 @@ public UserDaoJdbc userDao(){

@Bean
public SqlService sqlService() {
// BaseSqlService sqlService = new BaseSqlService();
// sqlService.setSqlReader(jaxbXmlSqlReader());
// sqlService.setSqlRegistry(hashMapSqlRegistry());
return new DefaultSqlService();
OxmSqlService sqlService = new OxmSqlService();
sqlService.setUnmarshaller(unmarshaller());
return sqlService;
}

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

import org.springframework.oxm.Unmarshaller;
import springbook.chapter07.BaseSqlService;
import springbook.chapter07.SqlRetrievalFailureException;
import springbook.chapter07.SqlService;
import springbook.chapter07.jaxb.SqlType;
import springbook.chapter07.jaxb.Sqlmap;

import javax.annotation.PostConstruct;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;

public class OxmSqlService implements SqlService {
private final BaseSqlService baseSqlService = new BaseSqlService();
private final SqlRegistry sqlRegistry = new HashMapSqlRegistry();

// OxmSqlService와 OxmSqlReader는 강합게 결합되어 있다
private final OxmSqlReader oxmSqlReader = new OxmSqlReader();

private static class OxmSqlReader implements SqlReader {
private final static String DEFAULT_SQLMAP_FILE = "sqlmap.xml";
private Unmarshaller unmarshaller;
private String sqlmapFile = DEFAULT_SQLMAP_FILE;

public void setUnmarshaller(Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
}

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

@Override
public void read(SqlRegistry sqlRegistry) {
try {
Source source = new StreamSource(this.getClass().getResourceAsStream("/" + sqlmapFile));
Sqlmap sqlmap = (Sqlmap) unmarshaller.unmarshal(source);

for (SqlType sql : sqlmap.getSql()) {
sqlRegistry.registerSql(sql.getKey(), sql.getValue());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

@PostConstruct
public void loadSql() {
this.baseSqlService.setSqlReader(this.oxmSqlReader);
this.baseSqlService.setSqlRegistry(this.sqlRegistry);

this.baseSqlService.loadSql();
}

@Override
public String getSql(String key) throws SqlRetrievalFailureException {
return this.baseSqlService.getSql(key);
}

public void setUnmarshaller(Unmarshaller unmarshaller) {
this.oxmSqlReader.setUnmarshaller(unmarshaller);
}

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

0 comments on commit d5ca2a5

Please sign in to comment.