SlideShare a Scribd company logo
1 of 27
SPRING
TROUBLE SHOOTING
백기선
http://whiteship.me
whiteship2000@gmail.com
Agenda
• 쉬운 문제
• 약간 어려운 문제
• 조금 더 어려운 문제
• 마무리
“순순히 다음 문제들을 해결하면 유혈사태는 일어나지 않을 것입니다.”
- 백간디
쉬운 문제
@Autowired
@Repository
public class 옥수수{
}
@Service
public class 간디{
@Autowired 옥수수 옥수수;
}
잘 되겠죠?
public interface 옥수수{
}
@Repository
public class 찰옥수수 implements 옥수수 {
}
@Repository
public class 팝콘옥수수 implements 옥수수 {
}
@Service
public class 간디{
@Autowired 옥수수 찰옥수수;
}
간디는 대체
어떤 옥수수를
팔겠다는거지…
@Autowired 동작원리
• 해당 타입의 빈이 없을 때 BAD!
• 해당 타입의 빈이 1개일 때 GOOD!
• 해당 타입의 빈이 여러개 일 때
• 해당 멤버 변수랑 같은 이름의 빈이 있을 때 GOOD!
• 해당 멤버 변수랑 같은 이름의 빈이 없을 때 BAD!
• Whiteship’s 권장사항
• 같은 타입의 빈이 여러개 일땐 명시적으로 이름을 설정해 주세요.
• @Repository(“찰옥수수”), @Service(“찰옥수수 간디”)
• 같은 타입의 빈이 여러개 일땐 빈을 주입 받을 곳에서 @Autowired
대신 @Resource를 사용하세요.
• @Resource(“찰옥수수”)
@Repository
public class 찰옥수수 implements 옥수수 {
}
@Repository
public class 팝콘옥수수 implements 옥수수 {
}
@Service
public class 간디{
@Autowired 옥수수 찰옥수수;
}
근데 간디가
팝콘을 팔던데?
컴포넌트 스캔과 <bean /> 혼용하기
<context:component-scan default-package=“easy”/>
<bean id=“gandhi” class=“easy.Gandhi”>
<property name=“maize” ref=“popconMaize”/>
</bean>
@Service
public class 간디{
@Autowired 옥수수 찰옥수수;
}
XML과 애노테이션 동작원리
• 컴포넌트 스캔으로 DI 한 다음 XML에서 DI 한 정보로
덮어씁니다.
• Whiteship’s 권장사항
• XML로 등록할 빈은 @Component를 사용하지 맙시다.
• DI 정보를 굳이 XML로 덮어써야 한다면 반드시 주석을 달아둡시다.
약간 어려운 문제
@Transactional
@Service
@Transactional
public class 간디 {
public 옥수수 거래(다이아 몬드) throws 유열사태 {
…
}
}
간디 클래스의 모든 public 메서드에
트랜잭션 처리 됨.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class 간디 테스트 {
@Autowired 간디 간디;
…
}
인터페이스를 만들어 볼까…
@Service
@Transactional
public class 간디 implements 간디서비스 {
public 옥수수 거래(다이아 몬드) throws 유열사태 {
…
}
}
public interface 간디서비스 {
public 옥수수 거래(다이아 몬드) throws 유열사태;
}
엥.. 갑자기
간디테스트에서
에러가 나네요?
간디 빈 구조
간디 간디 프록시
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class 간디 테스트 {
@Autowired 간디 간디;
…
}
간디 인터페이스 도입시 빈 구조
간디 서비스
간디 간디 프록시
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class 간디 테스트 {
@Autowired 간디 간디;
…
}
스프링 AOP 동작 원리
• <tx:annotation-driven /> 기본 설정 동작 원리
• 인터페이스가 있으면 JDK 프록시를 사용한다.
• 인터페이스가 없으면 CGLib 프록시를 사용한다.
• Whiteship’s 권장사항
• 인터페이스를 만들었다면 인터페이스를 사용할 것.
• 클래스기반으로 코딩하고 있다면 클래스 기반 프록시를 명시적으로
선언할 것.
• <tx:annotation-driven proxy-target-class=“true” />
애플리케이션 컨텍스트 상속 구조
ApplicationContext
ApplicationContext
public AC createAC(AC parent) {
CAC cac = new ACAC();
cac.setParent(parent);
return cac;
}
ConfigurableApplicationContext
ApplicationContext 상속구조 동작원리
• 부모 App 컨텍스트는 자식 App 컨텍스트에 있는 빈에
접근할 수 없다.
• 자식 App 컨텍스트는 부모 App 컨텍스트에 있는 빈에
접근할 수 있다.
• 빈을 달라고 요청하면…
• 자기 자신이 가지고 있는 빈을 찾아 준다.
• 자기 자신한테 없으면 부모 애플리케이션 컨텍스트에서 찾아 준다.
스프링 웹 애플리케이션
ContextLoaderListener
DispatcherServlet
WebApplicationContext
WebApplicationContext
만든다
만든다
상속 받는다
DispatcherServlet WebApplicationContext
DispatcherServlet WebApplicationContext
DispatcherServlet WebApplicationContext
발생 가능한 문제
ContextLoaderListener
DispatcherServlet
WebApplicationContext
WebApplicationContext
만든다
만든다
상속 받는다
DispatcherServlet WebApplicationContext
DispatcherServlet WebApplicationContext
DispatcherServlet WebApplicationContext
@Transactional이 안돼요!
AOP 설정 분명히 했는데 안돼요!
빈을 못 찾는다고 에러나요!
빈을 두번씩 만들고 있네;; 흠..
스프링 웹 애플리케이션 설정
• 부모 WAC
• 웹과 관련이 없는 빈 등록
• 예) Service, Dao
• 하위 WAC
• 웹과 관련이 있는 빈 등록
• 예) Controller
• Whiteship’s 권장사항
• 웹과 관련이 있는 것과 그렇치 않은 것을 패키지 레벨에서 잘 구분
할 것
• 패키지 레벨로 구분하기 힘들다면 @Component 애노테이션을
확장해서 사용할 것
• <context:component-scan>의 하위 엘리먼트인 <exclude>와
<include> 활용 할 것.
조금 더 어려운 문제
DispatcherServlet 매핑
Web.xml
<servlet>
<servlet-name>mygroups</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mygroups</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
도대체 /app/* 은 머야;;
/app/* -> /
http://toby.epril.com/?p=1107
- 스프링 3.0.4에 추가된 <mvc:default-servlet-handler/> 소개
해봤더니
기본 @MVC가
동작하질 않아;;
DispatcherServlet 기본전략
• HandlerMapping
• BeanNameUrlHandlerMapping
• DefaultAnnotationHandlerMapping
• HandlerAdapter
• HttpRequestHandlerAdapter
• SimpleControllerHandlerAdapter
• AnnotationMethodHandlerAdapter
• HandlerExceptionResolver
• AnnotationMethodHandlerExceptionResolver
• ResponseStatusExceptionResolver
• DefaultHandlerExceptionResolver
• ViewResolver
• InternalResourceViewResolver
• LocaleResovler
• AcceptHeaderLocalerResolver
• ThemeResolver
• FixedThemeResolver
• RequestToViewNameTranslator
• DefaultRequestToViewNameTranslator
HandlerAdapter ViewResolver
View
DispatcherSevlet HandlerMaping
Handler
getHandler
handle
ModelAndView
Controller
Service
Repository
ModelAndView
View
resolveViewName
render
DispatcherServlet 동작원리
• 해당 전략 타입의 빈이 하나라도 등록되면 해당 전략의
기본 전략 빈들은 하나도 등록되지 않는다.
• 예) mygroups-servlet.xml에 BeanNameUrlHandleMapping을
등록하면.. DS의 기본 전략 중 하나인
DefaultAnnotationHandlerMapping은 자동으로 등록되지 않는다.
• Whiteship’s 권장사항
• 빈으로 등록할 전략에 대해서는 기본 전략에 의지하지 말 것.
• HandlerMapping, HandlerAdapter, ViewResolver,
HandlerExceptionResovler
• DispatcherServlet의 동작원리를 팀 내에 공유할 것.
<mvc:default-servlet-handler/>
• HttpRequestHandlerAdapter
• DefaultServletHttpRequestHandler
• SimplUrlHandlerMapping
결론
문제가 발생했을 때 발생한 에러 스택은 매우 중요한 정보입니다.
스프링 설정 파일과 web.xml에서 실마리를 찾을 수 있습니다.
간혹 스프링 내부 동작 원리를 이해해야만 해결할 수 있는 문제도 있습니다.
마침 여러분은 매우 좋은 책을 가지고 계십니다.

More Related Content

What's hot

Meteor 0.3.6 Preview
Meteor 0.3.6 PreviewMeteor 0.3.6 Preview
Meteor 0.3.6 PreviewJuntai Park
 
jQuery angular, React.js 로 댓글달아보기 공부했던 기록
jQuery angular, React.js 로 댓글달아보기 공부했던 기록jQuery angular, React.js 로 댓글달아보기 공부했던 기록
jQuery angular, React.js 로 댓글달아보기 공부했던 기록라한사 아
 
Web Application Testing Patterns
Web Application Testing PatternsWeb Application Testing Patterns
Web Application Testing PatternsJune Kim
 
[D2 campus]착 하면 척! chak 서비스 개발기
[D2 campus]착 하면 척! chak 서비스 개발기[D2 campus]착 하면 척! chak 서비스 개발기
[D2 campus]착 하면 척! chak 서비스 개발기NAVER D2
 
웹-프론트엔드 프레임워크를 고르기 위한 팁
웹-프론트엔드 프레임워크를 고르기 위한 팁웹-프론트엔드 프레임워크를 고르기 위한 팁
웹-프론트엔드 프레임워크를 고르기 위한 팁WebFrameworks
 

What's hot (9)

Meteor 0.3.6 Preview
Meteor 0.3.6 PreviewMeteor 0.3.6 Preview
Meteor 0.3.6 Preview
 
jQuery angular, React.js 로 댓글달아보기 공부했던 기록
jQuery angular, React.js 로 댓글달아보기 공부했던 기록jQuery angular, React.js 로 댓글달아보기 공부했던 기록
jQuery angular, React.js 로 댓글달아보기 공부했던 기록
 
jQuery 구조와 기능
jQuery 구조와 기능jQuery 구조와 기능
jQuery 구조와 기능
 
Web Application Testing Patterns
Web Application Testing PatternsWeb Application Testing Patterns
Web Application Testing Patterns
 
[D2 campus]착 하면 척! chak 서비스 개발기
[D2 campus]착 하면 척! chak 서비스 개발기[D2 campus]착 하면 척! chak 서비스 개발기
[D2 campus]착 하면 척! chak 서비스 개발기
 
Presentation1
Presentation1Presentation1
Presentation1
 
웹-프론트엔드 프레임워크를 고르기 위한 팁
웹-프론트엔드 프레임워크를 고르기 위한 팁웹-프론트엔드 프레임워크를 고르기 위한 팁
웹-프론트엔드 프레임워크를 고르기 위한 팁
 
Gulp 입문
Gulp 입문 Gulp 입문
Gulp 입문
 
WebAssembly 101
WebAssembly 101WebAssembly 101
WebAssembly 101
 

Viewers also liked

Servlet design pattern
Servlet design patternServlet design pattern
Servlet design patternSukjin Yun
 
Mongo Web Apps: OSCON 2011
Mongo Web Apps: OSCON 2011Mongo Web Apps: OSCON 2011
Mongo Web Apps: OSCON 2011rogerbodamer
 
Jsp convert to Servlet
Jsp convert to ServletJsp convert to Servlet
Jsp convert to ServletJU Chae
 
Servlet Architecture
Servlet ArchitectureServlet Architecture
Servlet ArchitectureJU Chae
 
Spring framework 3.2 > 4.0 — themes and trends
Spring framework 3.2 > 4.0 — themes and trendsSpring framework 3.2 > 4.0 — themes and trends
Spring framework 3.2 > 4.0 — themes and trendsArawn Park
 
[오픈소스컨설팅]Day #3 MySQL Monitoring, Trouble Shooting
[오픈소스컨설팅]Day #3 MySQL Monitoring, Trouble Shooting[오픈소스컨설팅]Day #3 MySQL Monitoring, Trouble Shooting
[오픈소스컨설팅]Day #3 MySQL Monitoring, Trouble ShootingJi-Woong Choi
 
실용주의 디자인패턴 2 인터페이스로 프로그래밍하기
실용주의 디자인패턴   2 인터페이스로 프로그래밍하기실용주의 디자인패턴   2 인터페이스로 프로그래밍하기
실용주의 디자인패턴 2 인터페이스로 프로그래밍하기Cosmos Shin
 

Viewers also liked (7)

Servlet design pattern
Servlet design patternServlet design pattern
Servlet design pattern
 
Mongo Web Apps: OSCON 2011
Mongo Web Apps: OSCON 2011Mongo Web Apps: OSCON 2011
Mongo Web Apps: OSCON 2011
 
Jsp convert to Servlet
Jsp convert to ServletJsp convert to Servlet
Jsp convert to Servlet
 
Servlet Architecture
Servlet ArchitectureServlet Architecture
Servlet Architecture
 
Spring framework 3.2 > 4.0 — themes and trends
Spring framework 3.2 > 4.0 — themes and trendsSpring framework 3.2 > 4.0 — themes and trends
Spring framework 3.2 > 4.0 — themes and trends
 
[오픈소스컨설팅]Day #3 MySQL Monitoring, Trouble Shooting
[오픈소스컨설팅]Day #3 MySQL Monitoring, Trouble Shooting[오픈소스컨설팅]Day #3 MySQL Monitoring, Trouble Shooting
[오픈소스컨설팅]Day #3 MySQL Monitoring, Trouble Shooting
 
실용주의 디자인패턴 2 인터페이스로 프로그래밍하기
실용주의 디자인패턴   2 인터페이스로 프로그래밍하기실용주의 디자인패턴   2 인터페이스로 프로그래밍하기
실용주의 디자인패턴 2 인터페이스로 프로그래밍하기
 

Similar to 스프링 트러블슈팅

Elastic beanstalk - 판교 초급자 모임 - 안병학
Elastic beanstalk - 판교 초급자 모임 - 안병학Elastic beanstalk - 판교 초급자 모임 - 안병학
Elastic beanstalk - 판교 초급자 모임 - 안병학Byeong-hak An
 
Spring boot 공작소(1-4장)
Spring boot 공작소(1-4장)Spring boot 공작소(1-4장)
Spring boot 공작소(1-4장)Choonghyun Yang
 
Working Effectively With Legacy Code - xp2005
Working Effectively With Legacy Code - xp2005Working Effectively With Legacy Code - xp2005
Working Effectively With Legacy Code - xp2005Ryan Park
 
Scala, Spring-Boot, JPA의 불편하면서도 즐거운 동거
Scala, Spring-Boot, JPA의 불편하면서도 즐거운 동거Scala, Spring-Boot, JPA의 불편하면서도 즐거운 동거
Scala, Spring-Boot, JPA의 불편하면서도 즐거운 동거Javajigi Jaesung
 
[H3 2012] 내컴에선 잘되던데? - vagrant로 서버와 동일한 개발환경 꾸미기
[H3 2012] 내컴에선 잘되던데? - vagrant로 서버와 동일한 개발환경 꾸미기[H3 2012] 내컴에선 잘되던데? - vagrant로 서버와 동일한 개발환경 꾸미기
[H3 2012] 내컴에선 잘되던데? - vagrant로 서버와 동일한 개발환경 꾸미기KTH, 케이티하이텔
 
내컴에선 잘되던데? Vagrant로 서버와 동일한 개발환경 꾸미기
내컴에선 잘되던데? Vagrant로 서버와 동일한 개발환경 꾸미기내컴에선 잘되던데? Vagrant로 서버와 동일한 개발환경 꾸미기
내컴에선 잘되던데? Vagrant로 서버와 동일한 개발환경 꾸미기소리 강
 
vert.x 를 활용한 분산서버 개발하기
vert.x 를 활용한 분산서버 개발하기vert.x 를 활용한 분산서버 개발하기
vert.x 를 활용한 분산서버 개발하기John Kim
 
안드로이드 빌드: 설탕없는 세계
안드로이드 빌드: 설탕없는 세계안드로이드 빌드: 설탕없는 세계
안드로이드 빌드: 설탕없는 세계Leonardo YongUk Kim
 
Things Happend between JDBC and MySQL
Things Happend between JDBC and MySQLThings Happend between JDBC and MySQL
Things Happend between JDBC and MySQLDataya Nolja
 
자바가 디비와 사귀기 까지 벌어지는 일들
자바가 디비와 사귀기 까지 벌어지는 일들자바가 디비와 사귀기 까지 벌어지는 일들
자바가 디비와 사귀기 까지 벌어지는 일들cho hyun jong
 
20170813 django api server unit test and remote debugging
20170813 django api server unit test and remote debugging20170813 django api server unit test and remote debugging
20170813 django api server unit test and remote debuggingJongwon Han
 
JQuery를 이용하여 웹 위젯 작성하기_(주)시스포유아이앤씨
JQuery를 이용하여 웹 위젯 작성하기_(주)시스포유아이앤씨JQuery를 이용하여 웹 위젯 작성하기_(주)시스포유아이앤씨
JQuery를 이용하여 웹 위젯 작성하기_(주)시스포유아이앤씨sys4u
 
자바 웹 개발 시작하기 (5주차 : 스프링 프래임워크)
자바 웹 개발 시작하기 (5주차 : 스프링 프래임워크)자바 웹 개발 시작하기 (5주차 : 스프링 프래임워크)
자바 웹 개발 시작하기 (5주차 : 스프링 프래임워크)DK Lee
 
[웹기반시스템 3조]e govframe 중간고사 제출 정리
[웹기반시스템 3조]e govframe 중간고사 제출 정리[웹기반시스템 3조]e govframe 중간고사 제출 정리
[웹기반시스템 3조]e govframe 중간고사 제출 정리구 봉
 
Sonarqube 20160509
Sonarqube 20160509Sonarqube 20160509
Sonarqube 20160509영석 조
 
구글앱엔진+스프링+스프링datajpa+메이븐
구글앱엔진+스프링+스프링datajpa+메이븐구글앱엔진+스프링+스프링datajpa+메이븐
구글앱엔진+스프링+스프링datajpa+메이븐라한사 아
 

Similar to 스프링 트러블슈팅 (20)

4-1. javascript
4-1. javascript4-1. javascript
4-1. javascript
 
Spring Boot 1
Spring Boot 1Spring Boot 1
Spring Boot 1
 
Elastic beanstalk - 판교 초급자 모임 - 안병학
Elastic beanstalk - 판교 초급자 모임 - 안병학Elastic beanstalk - 판교 초급자 모임 - 안병학
Elastic beanstalk - 판교 초급자 모임 - 안병학
 
Spring boot 공작소(1-4장)
Spring boot 공작소(1-4장)Spring boot 공작소(1-4장)
Spring boot 공작소(1-4장)
 
Working Effectively With Legacy Code - xp2005
Working Effectively With Legacy Code - xp2005Working Effectively With Legacy Code - xp2005
Working Effectively With Legacy Code - xp2005
 
Scala, Spring-Boot, JPA의 불편하면서도 즐거운 동거
Scala, Spring-Boot, JPA의 불편하면서도 즐거운 동거Scala, Spring-Boot, JPA의 불편하면서도 즐거운 동거
Scala, Spring-Boot, JPA의 불편하면서도 즐거운 동거
 
[H3 2012] 내컴에선 잘되던데? - vagrant로 서버와 동일한 개발환경 꾸미기
[H3 2012] 내컴에선 잘되던데? - vagrant로 서버와 동일한 개발환경 꾸미기[H3 2012] 내컴에선 잘되던데? - vagrant로 서버와 동일한 개발환경 꾸미기
[H3 2012] 내컴에선 잘되던데? - vagrant로 서버와 동일한 개발환경 꾸미기
 
내컴에선 잘되던데? Vagrant로 서버와 동일한 개발환경 꾸미기
내컴에선 잘되던데? Vagrant로 서버와 동일한 개발환경 꾸미기내컴에선 잘되던데? Vagrant로 서버와 동일한 개발환경 꾸미기
내컴에선 잘되던데? Vagrant로 서버와 동일한 개발환경 꾸미기
 
vert.x 를 활용한 분산서버 개발하기
vert.x 를 활용한 분산서버 개발하기vert.x 를 활용한 분산서버 개발하기
vert.x 를 활용한 분산서버 개발하기
 
안드로이드 빌드: 설탕없는 세계
안드로이드 빌드: 설탕없는 세계안드로이드 빌드: 설탕없는 세계
안드로이드 빌드: 설탕없는 세계
 
Things Happend between JDBC and MySQL
Things Happend between JDBC and MySQLThings Happend between JDBC and MySQL
Things Happend between JDBC and MySQL
 
자바가 디비와 사귀기 까지 벌어지는 일들
자바가 디비와 사귀기 까지 벌어지는 일들자바가 디비와 사귀기 까지 벌어지는 일들
자바가 디비와 사귀기 까지 벌어지는 일들
 
20170813 django api server unit test and remote debugging
20170813 django api server unit test and remote debugging20170813 django api server unit test and remote debugging
20170813 django api server unit test and remote debugging
 
JQuery를 이용하여 웹 위젯 작성하기_(주)시스포유아이앤씨
JQuery를 이용하여 웹 위젯 작성하기_(주)시스포유아이앤씨JQuery를 이용하여 웹 위젯 작성하기_(주)시스포유아이앤씨
JQuery를 이용하여 웹 위젯 작성하기_(주)시스포유아이앤씨
 
5.Spring IoC&DI(DI와 관련된 어노테이션)
5.Spring IoC&DI(DI와 관련된 어노테이션)5.Spring IoC&DI(DI와 관련된 어노테이션)
5.Spring IoC&DI(DI와 관련된 어노테이션)
 
자바 웹 개발 시작하기 (5주차 : 스프링 프래임워크)
자바 웹 개발 시작하기 (5주차 : 스프링 프래임워크)자바 웹 개발 시작하기 (5주차 : 스프링 프래임워크)
자바 웹 개발 시작하기 (5주차 : 스프링 프래임워크)
 
[웹기반시스템 3조]e govframe 중간고사 제출 정리
[웹기반시스템 3조]e govframe 중간고사 제출 정리[웹기반시스템 3조]e govframe 중간고사 제출 정리
[웹기반시스템 3조]e govframe 중간고사 제출 정리
 
Node.js 기본
Node.js 기본Node.js 기본
Node.js 기본
 
Sonarqube 20160509
Sonarqube 20160509Sonarqube 20160509
Sonarqube 20160509
 
구글앱엔진+스프링+스프링datajpa+메이븐
구글앱엔진+스프링+스프링datajpa+메이븐구글앱엔진+스프링+스프링datajpa+메이븐
구글앱엔진+스프링+스프링datajpa+메이븐
 

스프링 트러블슈팅

  • 2. Agenda • 쉬운 문제 • 약간 어려운 문제 • 조금 더 어려운 문제 • 마무리 “순순히 다음 문제들을 해결하면 유혈사태는 일어나지 않을 것입니다.” - 백간디
  • 4. @Autowired @Repository public class 옥수수{ } @Service public class 간디{ @Autowired 옥수수 옥수수; } 잘 되겠죠?
  • 5. public interface 옥수수{ } @Repository public class 찰옥수수 implements 옥수수 { } @Repository public class 팝콘옥수수 implements 옥수수 { } @Service public class 간디{ @Autowired 옥수수 찰옥수수; } 간디는 대체 어떤 옥수수를 팔겠다는거지…
  • 6. @Autowired 동작원리 • 해당 타입의 빈이 없을 때 BAD! • 해당 타입의 빈이 1개일 때 GOOD! • 해당 타입의 빈이 여러개 일 때 • 해당 멤버 변수랑 같은 이름의 빈이 있을 때 GOOD! • 해당 멤버 변수랑 같은 이름의 빈이 없을 때 BAD! • Whiteship’s 권장사항 • 같은 타입의 빈이 여러개 일땐 명시적으로 이름을 설정해 주세요. • @Repository(“찰옥수수”), @Service(“찰옥수수 간디”) • 같은 타입의 빈이 여러개 일땐 빈을 주입 받을 곳에서 @Autowired 대신 @Resource를 사용하세요. • @Resource(“찰옥수수”)
  • 7. @Repository public class 찰옥수수 implements 옥수수 { } @Repository public class 팝콘옥수수 implements 옥수수 { } @Service public class 간디{ @Autowired 옥수수 찰옥수수; } 근데 간디가 팝콘을 팔던데?
  • 8. 컴포넌트 스캔과 <bean /> 혼용하기 <context:component-scan default-package=“easy”/> <bean id=“gandhi” class=“easy.Gandhi”> <property name=“maize” ref=“popconMaize”/> </bean> @Service public class 간디{ @Autowired 옥수수 찰옥수수; }
  • 9. XML과 애노테이션 동작원리 • 컴포넌트 스캔으로 DI 한 다음 XML에서 DI 한 정보로 덮어씁니다. • Whiteship’s 권장사항 • XML로 등록할 빈은 @Component를 사용하지 맙시다. • DI 정보를 굳이 XML로 덮어써야 한다면 반드시 주석을 달아둡시다.
  • 11. @Transactional @Service @Transactional public class 간디 { public 옥수수 거래(다이아 몬드) throws 유열사태 { … } } 간디 클래스의 모든 public 메서드에 트랜잭션 처리 됨. @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class 간디 테스트 { @Autowired 간디 간디; … }
  • 12. 인터페이스를 만들어 볼까… @Service @Transactional public class 간디 implements 간디서비스 { public 옥수수 거래(다이아 몬드) throws 유열사태 { … } } public interface 간디서비스 { public 옥수수 거래(다이아 몬드) throws 유열사태; } 엥.. 갑자기 간디테스트에서 에러가 나네요?
  • 13. 간디 빈 구조 간디 간디 프록시 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class 간디 테스트 { @Autowired 간디 간디; … }
  • 14. 간디 인터페이스 도입시 빈 구조 간디 서비스 간디 간디 프록시 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class 간디 테스트 { @Autowired 간디 간디; … }
  • 15. 스프링 AOP 동작 원리 • <tx:annotation-driven /> 기본 설정 동작 원리 • 인터페이스가 있으면 JDK 프록시를 사용한다. • 인터페이스가 없으면 CGLib 프록시를 사용한다. • Whiteship’s 권장사항 • 인터페이스를 만들었다면 인터페이스를 사용할 것. • 클래스기반으로 코딩하고 있다면 클래스 기반 프록시를 명시적으로 선언할 것. • <tx:annotation-driven proxy-target-class=“true” />
  • 16. 애플리케이션 컨텍스트 상속 구조 ApplicationContext ApplicationContext public AC createAC(AC parent) { CAC cac = new ACAC(); cac.setParent(parent); return cac; } ConfigurableApplicationContext
  • 17. ApplicationContext 상속구조 동작원리 • 부모 App 컨텍스트는 자식 App 컨텍스트에 있는 빈에 접근할 수 없다. • 자식 App 컨텍스트는 부모 App 컨텍스트에 있는 빈에 접근할 수 있다. • 빈을 달라고 요청하면… • 자기 자신이 가지고 있는 빈을 찾아 준다. • 자기 자신한테 없으면 부모 애플리케이션 컨텍스트에서 찾아 준다.
  • 18. 스프링 웹 애플리케이션 ContextLoaderListener DispatcherServlet WebApplicationContext WebApplicationContext 만든다 만든다 상속 받는다 DispatcherServlet WebApplicationContext DispatcherServlet WebApplicationContext DispatcherServlet WebApplicationContext
  • 19. 발생 가능한 문제 ContextLoaderListener DispatcherServlet WebApplicationContext WebApplicationContext 만든다 만든다 상속 받는다 DispatcherServlet WebApplicationContext DispatcherServlet WebApplicationContext DispatcherServlet WebApplicationContext @Transactional이 안돼요! AOP 설정 분명히 했는데 안돼요! 빈을 못 찾는다고 에러나요! 빈을 두번씩 만들고 있네;; 흠..
  • 20. 스프링 웹 애플리케이션 설정 • 부모 WAC • 웹과 관련이 없는 빈 등록 • 예) Service, Dao • 하위 WAC • 웹과 관련이 있는 빈 등록 • 예) Controller • Whiteship’s 권장사항 • 웹과 관련이 있는 것과 그렇치 않은 것을 패키지 레벨에서 잘 구분 할 것 • 패키지 레벨로 구분하기 힘들다면 @Component 애노테이션을 확장해서 사용할 것 • <context:component-scan>의 하위 엘리먼트인 <exclude>와 <include> 활용 할 것.
  • 23. /app/* -> / http://toby.epril.com/?p=1107 - 스프링 3.0.4에 추가된 <mvc:default-servlet-handler/> 소개 해봤더니 기본 @MVC가 동작하질 않아;;
  • 24. DispatcherServlet 기본전략 • HandlerMapping • BeanNameUrlHandlerMapping • DefaultAnnotationHandlerMapping • HandlerAdapter • HttpRequestHandlerAdapter • SimpleControllerHandlerAdapter • AnnotationMethodHandlerAdapter • HandlerExceptionResolver • AnnotationMethodHandlerExceptionResolver • ResponseStatusExceptionResolver • DefaultHandlerExceptionResolver • ViewResolver • InternalResourceViewResolver • LocaleResovler • AcceptHeaderLocalerResolver • ThemeResolver • FixedThemeResolver • RequestToViewNameTranslator • DefaultRequestToViewNameTranslator HandlerAdapter ViewResolver View DispatcherSevlet HandlerMaping Handler getHandler handle ModelAndView Controller Service Repository ModelAndView View resolveViewName render
  • 25. DispatcherServlet 동작원리 • 해당 전략 타입의 빈이 하나라도 등록되면 해당 전략의 기본 전략 빈들은 하나도 등록되지 않는다. • 예) mygroups-servlet.xml에 BeanNameUrlHandleMapping을 등록하면.. DS의 기본 전략 중 하나인 DefaultAnnotationHandlerMapping은 자동으로 등록되지 않는다. • Whiteship’s 권장사항 • 빈으로 등록할 전략에 대해서는 기본 전략에 의지하지 말 것. • HandlerMapping, HandlerAdapter, ViewResolver, HandlerExceptionResovler • DispatcherServlet의 동작원리를 팀 내에 공유할 것.
  • 27. 결론 문제가 발생했을 때 발생한 에러 스택은 매우 중요한 정보입니다. 스프링 설정 파일과 web.xml에서 실마리를 찾을 수 있습니다. 간혹 스프링 내부 동작 원리를 이해해야만 해결할 수 있는 문제도 있습니다. 마침 여러분은 매우 좋은 책을 가지고 계십니다.