스프링

    [Spring] 공통 필드 하나로 묶기! || BaseEntity, JpaAuditing

    들어가며 Member, Order, Board 등 다양한 엔티티를 생성하여 사용하다보면, 반복되는 공통 속성이 존재하게 됩니다. 예를 들면, 생성일, 수정일, 식별자 등이 있으며, 보통 하나로 묶어 BaseEntity를 생성하여 각 엔티티가 상속받을 수 있도록 구현하며 사용하곤 합니다. 본 포스팅에는 공통 속성을 하나로 묶어 BaseEntity라는 부모 클래스를 생성하는 내용을 소개합니다. 📌 공통 속성public class Member { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; private String password; private LocalDateTime crea..

    'authorizeRequests()' is deprecated 해결 || Spring Security Configuration

    'authorizeRequests()' is deprecated 해결 || Spring Security Configuration

    기존에 사용해오던 방식대로 SecurityConfig를 만들어서 403 Error를 해결하고자 하였는데, authorizeRequests()가 deprecated 되었다고 한다. WebSecurityConfigurerAdapter도 deprecate되어 빈을 등록하는 방식으로 바꿔주었었는데, 이것도 바꿔보자!아래와 같이 변경하여 사용할 수 있다.http .authorizeRequests() .requestMatchers("/admin/**").hasRole("ADMIN") .requestMatchers("/**").hasRole("USER") .and().formLogin(); return http.build();antMatchers를 requestMatchers로 변경. authorizeRequest는..

    [Spring] 의존성 주입(DI, Dependency Injection) (생성자 주입을 사용해야 하는 이유)

    [Spring] 의존성 주입(DI, Dependency Injection) (생성자 주입을 사용해야 하는 이유)

    📌 DI(Dependency Injection)DI(Dependency Injection)이란, 객체를 직접 생성하는 것이 아니라 외부에서 생성 후 주입시켜주는 방식이다. 즉, 의존 관계를 외부에서 결정하고 주입하는 것을 의미한다. interface Book { // 더 다양한 Book을 의존받을 수 있도록 인터페이스로 추상화 ... } class ScienceBook implements Book { ... } class EnglishBook implements Book { ... } public class Library { private Book book; public Library() { this.book = new ScienceBoo..

    [Spring] JPA N + 1 문제 발생 원인 및 해결 방안

    [Spring] JPA N + 1 문제 발생 원인 및 해결 방안

    📌 JPA N + 1 문제란?조회된 데이터 개수만큼, 연관 관계의 조회 쿼리가 추가로 발생하는 문제를 의미한다. EX) 카테고리와 게시글@Entity @NoArgsConstructor @Setter @Getter public class Board { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; private String content; @ManyToOne private Category category; public Board(String title, String content, Category category) { this.title = title; this.content = co..