⇒ 테이블 이름이나 필드 이름, sql을 변경할 때 xml 파일만 바꾸면 되기에 변경에 유연하게 대처할 수 있다.
<bean id="userDao" class="...">
<property name="datasource" ref="datasource"/>
<property name="sqlAdd" value="insert into users(id, name) values(?, ?)"/>
...
</bean>
<bean id="userDao" class="...">
<property name="datasource" ref="datasource"/>
<property name="sqlMap">
<map>
<entry key="add" value="insert into users(id, name) values(?,?)"/>
<entry key="get" value="select * from users"/>
</map>
</property>
...
</bean>
스프링 설정파일에 DI 설정정보와 sql이 섞여있는 것은 보기에도 않좋고 sql 리뷰나 튜닝에도 좋지 않다.
스프링 설정파일에서 생성된 오브젝트는 애플리케이션을 다시 시작하기 전에는 변경이 어렵다
⇒ 운영중인 애플리케이션에서 빈번하게 참조되는 맵 정보(sqlMap)을 수정할 경우, 동시성 문제가 발생할 수 있기 때문
public interface SqlService{
String getSql(String key) throws SqlRethrievalFailureException;
}
public class SqlRethrievalFailureException extends RuntimeException{
public SqlRethrievalFailureException(String message){
super(message);
}
public SqlRethrievalFailureException(String message, Throwable cause){
super(message, cause);
}
}