Spring Framework

Spring Framework/Spring

Spring h2 인메모리 DatabaseCleaner 적용 방법 - 테스트 데이터 초기화를 통한 독립적인 테스트 환경 구축

h2 인메모리 환경 Database Cleaner 오작동문제상황기존 DatabaseCleaner 코드@Componentpublic class DatabaseCleaner { @Autowired private DataSource dataSource; @Autowired private JdbcTemplate jdbcTemplate; private List tables; @PostConstruct public void init() { this.tables = jdbcTemplate.query("show tables", (rs, rowNum) -> rs.getString(1)).stream().toList(); } public void clean() throws SQLExcepti..

Spring Framework/Spring

Spring ServletContainer, Dispatcher Servlet 동작 위치 이해하기 - Filter, Interceptor 특징 및 처리 순서

Servlet이란Servlet은 클라이언트의 요청을 처리하고, 그 결과를 응답하는 Java의 웹 프로그래밍 기술이다.Servlet Container는 웹 서버에 오는 요청을 가장 먼저 받습니다.Servlet Filter를 통해 전달받은 요청을 필터링할 수 있습니다. Spring Security의 Filter도 Servlet Filter에 속하며, 해당 위치에서 인증/인가를 처리합니다.Servlet 특징클라이언트의 요청에 대해 동적으로 작동한다.Java Thread를 이용하여 동작한다.Request, Response 객체를 통해 개발자가 직접 HTTP 요청을 Parsing 하지 않고 편리하게 사용할 수 있게 해 준다.Servlet 통신 방식클라이언트 요청이 Servlet Container로 Request를..

Spring Framework/Spring boot

스프링부트 @SpringBootTest, @ContextConfiguration, @WebMvcTest, @AutoConfigureMockMvc 주요 테스트 어노테이션 정리

스프링 부트를 기반으로 테스트 코드 작성 시 자주 등장하는 테스트 어노테이션의 사용 상황과 기능에 대해 정리하고자 합니다. @SpringBootTest 어노테이션Annotation that can be specified on a test class that runs Spring Boot Based tests. @SpringBootTest 어노테이션은 스프링 부트 기반 테스트 시에 사용하는 어노테이션이다. 다음과 같은 기능과 특징을 가지고 있습니다. 1. Uses SpringBootContextLoader as the default ContextLoader when no specific @ContextConfiguration(loader=...) is defined. 💬 특정 @ContextConfigu..

Spring Framework/Spring

Spring @PostConstruct, @Value 통한 빈 의존성 설정 - @PreDestroy bean destruction

@PostConstructSpring calls the methods annotated with @PostConstruct only once, just after the initialization of bean properties. 스프링은 @PostConstruct가 붙은 메서드를 빈 속성 초기화 이후에 단 한 번 호출합니다. The method annotated with @PostConstruct can have any access level, but it can’t be static. @PostConstruct가 붙은 메서드는 static으로 선언될 수 없습니다.초기화 작업을 하므로, 정적 메서드로 선언될 수 없다.@Componentpublic class DbInit { @Autowired ..