TIL

1_1.Spring Framework 환경 구성

꿀승 2025. 1. 9. 10:23
728x90
반응형
SMALL

학습 내용

  1. Spring Framework 특징 및 구조
  2. WAS
  3. Spring Boot 프로젝트 생성
  4. 디렉토리 구조
  5. 주요 라이브러리 설정

학습 정리

1. Spring Framework 특징 및 구조

  • 특징
    1. 의존성 주입 (Dependency Injection) : 객체 간의 의존 관계를 외부(스프링 컨테이너)에서 주입하여 결합도를 낮춤
    2. AOP (Aspect Oriented Programming) : 공통 관심사 (로깅,트랜잭션)를 분리하여 코드 재사용성을 높임 #추후에 해당 부분에 대해 다룸
    3. 강력한 생태계
      • 구조
    4. Controller : 클라이언트 요청 처리
    5. Service : 비즈니스 로직을 구현
    6. Repository : 데이터베이스와 상호 작용

2. WAS

  • WAS(Web Application Serv)는 HTTP 요청과 응답을 관리하고 비즈니스 로직을 실행하는 서버
  • SpringBoot (Spring Web)은 기본적으로 내장 WAS로 Tomcat 제공 #밑에서 undertow 다른 WAS로 변경 예정

3. Spring Boot 프로젝트 생성

  • https://start.spring.io/ 접속
  • 프로젝트 설정 및 Dependencies 추가 후 GENERATE
  • 다운로드 된 zip 파일 해제 후 IntelliJ에서 해당 경로의 build.gradle open

4. 디렉토리 구조

  • 기본구조

    
     src/
     |-- main/
     |     |-- java/
     |     |   |-- com.example.demo/
     |     |       |-- config/       # 설정 클래스
     |     |       |-- controller/   # 웹 컨트롤러
     |     |       |-- dto/          # 데이터 전송 객체
     |     |       |-- entity/       # JPA 엔티티 클래스
     |     |       |-- service/      # 서비스 클래스
     |     |       |-- repository/   # 리포지토리 인터페이스
     |     |       |-- util/         # 유틸리티 클래스
     |     |-- resources/
     |         |-- db/migration/     # Flyway 마이그레이션 파일
     |         |-- application.yml   # 설정 파일
     |         |-- static/           # 정적 리소스
  • 도메인 중심 구조

    
     src/
     |-- main/
     |     |-- java/
     |     |   |-- com.example.demo/
     |     |       |-- domain/
     |     |       |   |-- member/          # Member 도메인 관련 코드
     |     |       |       |-- controller/  # Member 관련 컨트롤러
     |     |       |       |-- dto/         # Member 관련 DTO
     |     |       |       |-- service/     # Member 관련 서비스
     |     |       |       |-- entity/      # Member 관련 엔티티 클래스
     |     |       |       |-- repository/  # Member 관련 리포지토리
     |     |       |-- config/       # 설정 클래스
     |     |       |-- util/         # 유틸리티 클래스
     |     |-- resources/
     |         |-- db/migration/     # Flyway 마이그레이션 파일
     |         |-- application.yml   # 설정 파일
     |         |-- static/           # 정적 리소스

    앞으로 프로젝트는 도메인 중심 구조를 기반으로 진행됨.

5. 주요라이브러리 설정

  • build.gralde

  • application.yml

  • undertow
    참고자료 : https://seung1214.tistory.com/21

  • MapStruct
    참고자료 : https://seung1214.tistory.com/23

  • Flyway
    참고자료 : https://seung1214.tistory.com/22

  • Swagger

    1. 의존성 추가

      implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'
    2. Swagger Configuration 클래스

    
    import io.swagger.v3.oas.annotations.OpenAPIDefinition;
    import io.swagger.v3.oas.annotations.info.Info;
    import io.swagger.v3.oas.models.Components;
    import io.swagger.v3.oas.models.OpenAPI;
    import io.swagger.v3.oas.models.security.SecurityRequirement;
    import io.swagger.v3.oas.models.security.SecurityScheme;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @OpenAPIDefinition(
        info = @Info(
            title = "Example API Docs",
            description = "Description",
            version = "v1"
        )
    )
    @Configuration
    public class SwaggerConfig {
    
      private static final String BEARER_TOKEN_PREFIX = "Bearer";
    
      @Bean
      public OpenAPI openAPI() {
        String securityJwtName = "JWT";
        SecurityRequirement securityRequirement = new SecurityRequirement().addList(securityJwtName);
        Components components = new Components()
            .addSecuritySchemes(securityJwtName, new SecurityScheme()
                .name(securityJwtName)
                .type(SecurityScheme.Type.HTTP)
                .scheme(BEARER_TOKEN_PREFIX)
                .bearerFormat(securityJwtName));
    
        return new OpenAPI()
            .addSecurityItem(securityRequirement)
            .components(components);
      }
    
    }

    스프링버전, Swagger 버전에 따라 다르니 참고

728x90
반응형
LIST