public class DataSourceInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// 원하는 로직에 따라 데이터 소스 키를 선택합니다.
if (request.getRequestURL().toString().contains("/dev")) {
System.out.println("/dev");
DataSourceContextHolder.setDataSourceKey("devDataSource");
} else {
System.out.println("/prod");
DataSourceContextHolder.setDataSourceKey("prodDataSource");
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
DataSourceContextHolder.clearDataSourceKey();
}
}
댓글남기기