열렬히.뛰기

코드분석 (1)

Projects > 코드분석 (1)

스프링 코드분석

전체 디렉토리

최상단srcmain 이고, main 내부는 다음과 같이 구성.

bash
.
├── generated
├── java
│   └── com
│       └── project
│           └── simsim_server
│               ├── config
│               ├── controller
│               ├── domain
│               ├── dto
│               ├── exception
│               ├── filter
│               ├── repository
│               └── service
├── resources
│   └── static
│       └── html
└── sql

패키지 (1)

  • resources : 애플리케이션 설정 파일 및 정적 리소스
  • sql : 데이터베이스 초기화를 위한 SQL 스크립트 파일

패키지 (2)

java.com.project.simsim_server 내부의 패키지를 분석해보자.

이 앱은 크게 4가지의 도메인이 있다: AI, 다이어리, 리포트, 사용자.

- config : 보안 설정, 인코딩, Redis, 스케줄링, Swagger 설정을 관리
- controller : 각 도메인의 HTTP 요청을 처리.
- domain : 데이터베이스의 테이블과 매핑되는 부분 (DB <-> 백엔드)
- dto : 클라이언트와 서버 간에 데이터를 주고받기 위한 부분. (프론트 <-> 백엔드)
- exception : 예외처리
- filter : JWT 인증
- repository : 데이터베이스와 상호작용하는 리포지토리 인터페이스
- service : 비즈니스 로직을 처리

이를 데이터베이스 → 백엔드 → 프론트로 가는 단계별로 구성해보면 다음과 같다.

<연결 과정>
데이터베이스
- domain (DB의 테이블을 클래스로 변환)
- repository (DB와의 연결통로)
- service (로직 처리)
	- filter (로그인 시 필요)
- dto (클래스를 프론트로 보내기 위한 처리)
- controller (프론트와의 연결통로)
프론트

<이외>
- config
- exception

main문

java
package com.project.simsim_server;

import jakarta.annotation.PostConstruct;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.TimeZone;

// Spring에서 스케줄링을 활성화하는 역할
@EnableScheduling
// PA의 Auditing 기능을 활성화 (엔티티에 자동으로 생성일, 수정일 등을 기록)
@EnableJpaAuditing
@SpringBootApplication
// JPA 리포지토리를 활성화
@EnableJpaRepositories(basePackages = {"com.project.simsim_server.repository"})
// 엔티티 클래스를 스캔하여 JPA에서 사용
@EntityScan(basePackages = {"com.project.simsim_server.domain"})
public class SimsimServerApplication {

	// Spring에서 해당 메서드가 빈(Bean) 초기화 직후 호출되도록 설정
	// 애플리케이션이 실행될 때 이 메서드가 자동으로 호출
	@PostConstruct
	void started(){
		TimeZone.setDefault(TimeZone.getTimeZone("Asia/Seoul"));
	}
	
	public static void main(String[] args) {
		// Spring Boot 애플리케이션을 실행시키는 명령
		SpringApplication.run(SimsimServerApplication.class, args);
	}
}

코드분석의 다음 내용

영역 : AI, 다이어리, 사용자, 리포트.

각 영역을 다음과 같은 순서로 각 도메인을 분석.

데이터베이스
- domain (DB의 테이블을 클래스로 변환)
- repository (DB와의 연결통로)
- service (로직 처리)
	- filter (로그인 시 필요)
- dto (클래스를 프론트로 보내기 위한 처리)
- controller (프론트와의 연결통로)
프론트