JAVA/Spring Framework

알바 관리 메이븐 프로젝트에 Spring 적용하기

Tech Signal 2020. 10. 12. 19:22
  1. pom.xml 수정
  2. web.xml
    1. contextParam
    2. filter, Listener 추가
    3. frontController -> appServlet으로 대체
  3. WEB-INF 내에 spring폴더 만들기 (spring의 웹 리소스들 만들기)
  4. spring-appServlet 폴더에 Spring configuration xml 파일 만들기( 파일명: servlet-context.xml)
    1. namespaces 추가 (c, p, context, beans, mvc 등)
    2. namespace를 mvc를 기본설정으로 하고 beans 에서 beans:beans로 바꾼다 (optional)
    3. context:component scan 추가 후 controller, controllerAdvice 만 include
    4. <annotation-driven /> (mvc:anno하거나)
    5. <InternalResourceViewResolver> 추가하고 prefix, suffix 지정 ( for .jsp files)
    6. <default-servlet-handler/>, localeResolver를 위한 인터셉터, messageSource 등 추가
      1. localeResolver 를 추가함으로써 알바 관리에서 한글인 컬럼이름들을 영어로 변경 가능
  5. root-context.xml에서는 controller, controllerAdvice만 빼고 모든 @component 추가
  6. VO에서 PartWrapper를 포함하는 요소들 다 MultipartFile 로 수정, 파일명도 기존의 메서드 지우고 UUID에서 randomUUID 생성해 넣기
  7. Dao 인터페이스에서 더이상 SqlSession 사용하지 않으니 시그니처 수정
    1. (대신 xml에서 sqlSessionTemplate 사용해 트랜잭션 관리함)
  8. daoImpl 다 삭제
  9. Service 구현체
    1. @service 붙이고 인터페이스엔 안붙임
    2. 생성자 삭제
    3. dao에서 더이상 sqlSession 안쓰니 try with resource 문 안에서 아예 세션 만들고 session.commit()할 필요도 없음. 지우기
  10. Controller 단 수정
    1. @Controller 붙이기
    2. RequestParameter -> RequestParam으로 고치기 
    3. HTTPServletRequest-> Model 로 바꾸기
      1. Model 객체로 바꾸는 이유: 
    4. ProdRetrieveController: 알바생 목록 보여주는 메서드에서 json 마샬링하는 부분 따로 메서드 분리
      1. @RequestMapping에서 produces 속성으로 mediaType이 jSON일 경우에 매핑 설정
    5. ProdInsertController
      1. validation 필요하니까 @Validated(InsertGroup.Class) 적용
      2. BindingResults 타입의 errors 받아오기
      3. 요청이 Multipart 타입일때 사진 저장하던 부분은 
    6. CodeRetrieveController 
      1. 의 경우 갖고 있는 메서드 2개 다 마샬링을 해야함
      2. resp.sendError

ServletContext 가 필요한 부분은 private WebApplicationContext context.getServletContext()로 하기