초오오오오오짜개발자의낙서장
Spring N+1 일차 본문
spring 특징
IoC/DI
AOP
- 공통적으로 적용될 모듈을 만든 후 적용하고자 하는 부분의 코드 밖에서 삽입하는 방법
- 사용 분야 - 메소드의 성능 테스트, 트랜잭션 처리, 예외 반환, 로깅인증 권한 처리등,
- 관점 지향 프로그래밍
@Aspect 어노테이션 사용법 익히기
ControllerAspect
발전된 프로그래밍 방식이다. 라고 알면 된다
일일히 외워야 된다.
controller 에 메소드가 2개가 있다.면? 실행될때 로그를 남기고 싶다 @Around 사용
예외처리할때 스스로 구현하는것
around 로 method 실행정 시간 측정 method 실행후 시간 측정
single ton 으로 전역변수 사용 불가.
@Slf4j
@Aspect
@Component
public class ControllerAspect {
@Around("execution(* com.example.basic.controller.*.*(..))")
public Object onAroundHandler(ProceedingJoinPoint joinPoint)
throws Throwable {
log.debug("@Before run"); // Before
Object result = null;
try {
result = joinPoint.proceed(); // 실제 메서드 실행
if (result != null) { // AfterReturning
log.debug(result.toString());
}
log.debug("@AfterReturning run");
return result;
} finally {
log.debug("@After run"); // After
}
}
}
after returning after throwing 시험에나 나올법하다.
Json 결과를 원하면 @ResponseBody 사용
[] 는 옵션으로 사용한다.
request 헤드 안에 있는 정보를 조회해서 파악이 가능하다
물론 조작도 가능하다.
Interceptor
controller 에 들어오는 요청과 응답을 가로채는 기능
Filter가 먼저 동작하고 2번째로 Interceptor가 동작한다.
앞쪽에서 처리하면 뒤에서 부담감이 줄어든다.
서블릿 필터를 직접 제어하고 있는게 아니라 서블릿을 직접 조작을 못한다 현재 상태로는.
startup.bat을 직접 실행해서 제어하는게 직접 제어다.
interceptor는 method 로 제어를 한다.
prehandle 을 훨씬 많이 쓴다.
@Component
@Slf4j
public class SignInCheckInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(
HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
log.debug("preHandle");
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
if (user == null) {
response.sendRedirect("/login");
return false;
}
return true;
}
handlerInterceptor 상속을 받는다.
기본 형식 작성후 어떤 검사를 할건지 체크한다.
Filter - FIlter 그 자체 / filterCOnfig 2가지가 필요하다.
config - bean 등록 + url 등록
interceptor
1) interceptor
2) Interceptor COnfig
url 명시
File Upload
multipart - 파일이 자동으로 이 변수로 들어오게 된다. m Request
@Controller
public class UploadController {
@GetMapping("/upload1")
public String upload1() {
return "upload1";
}
@PostMapping("/upload1")
@ResponseBody
public String upload1Post(MultipartHttpServletRequest mRequest) {
String result = "";
MultipartFile mFile = mRequest.getFile("file");
String oName = mFile.getOriginalFilename();
result += oName + "<br>" + mFile.getSize();
return result;
}
}
<form method="post" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="submit" value="업로드">
</form>
action 이 빠졋을때 이 파일이 불러진 주소가 action 이 된다.
업로드 할때만 enctype multipart/form-data
파일명이 중복되면 기존 파일이 삭제가 된다.
'Programing Language > SPRING' 카테고리의 다른 글
코드리뷰 키워드 공부 1일차 (1) | 2025.07.14 |
---|---|
JPA란? (2) | 2025.06.04 |
SPRING 프로젝트 세팅하기 (1) | 2025.06.01 |
InteliJ - Project 디렉토리 표시 단축키 (0) | 2025.05.02 |