kotlin internal class
·
Tech/Spring
kotlin의 internal은 같은 모듈 내에서만 접근 가능한 가시성 제한자 Kotlin의 가시성 제한자public // 어디서나 접근 가능 (기본값)private // 같은 파일/클래스 내에서만protected // 같은 클래스와 서브클래스에서만internal // 같은 모듈 내에서만 1. 모듈 단위 캡슐화// Module A (core 모듈)internal class DatabaseHelper { internal fun connect() { }}internal val config = Config()// Module A 내 다른 파일에서val helper = DatabaseHelper() // ✅ OKhelper.connect() // ✅ OK// Module ..
gradle implementation vs api 차이
·
Tech/Gradle
implementation의존성을 내부적으로만 사용다른 모듈에 전이되지 않음 // Module Adependencies { implementation 'com.google.gson:gson:2.8.9'}// Module B가 Module A를 의존할 때dependencies { implementation project(':moduleA') // Module B는 Gson을 직접 사용할 수 없음 // Gson이 필요하면 명시적으로 추가해야 함} api의존성을 외부에 노출다른 모듈에 전이됨// Module Adependencies { api 'com.google.gson:gson:2.8.9'}// Module B가 Module A를 의존할 때dependencies { imp..
bootJar,jar 활성,비활성화 (구)/(신) 코드
·
Tech/Gradle
구tasks.getByName("bootJar") { enabled = true}tasks.getByName("jar") { enabled = false} 특징타입 미지정: DSL 안에서 enabled 같은 속성을 설정할 수 있지만 타입 안전하지 않음Eager 방식: 태스크를 즉시 조회 및 구성 -> 빌드 성능에 악영향을 줄 수 있음Gradle 5.1+부터 권장되는 스타일 신import org.springframework.boot.gradle.tasks.bundling.BootJartasks.named("bootJar") { enabled = true}tasks.named("jar") { enabled = false}특징타입 안전: BootJar 타입으로 지정되므로 intellij..
스프링 부트 3.2에서 docker-compose.yml와 연결하여 TestContainers를 설정하는 방법 (with Mysql)
·
Tech/Spring
개요로컬 혹은 CI 서버에서 실제 운영 환경과 같은 디비를 사용하여 통합 테스트를 하고 싶어 TestContainers 설정을 적용했다.세부 설정은 docker-compose.yml로 관리하는게 편하여 이를 import하는 방식으로 구현했다. 환경스프링 부트 3.2.6Gradle 8.8자바 17 설정gradle에 라이브러리 추가ext { testcontainersVersion = "1.19.0"}dependencies { testImplementation "org.springframework.boot:spring-boot-testcontainers" testImplementation 'org.testcontainers:mysql' testImplementation 'org.sprin..
SpringBoot 커스텀 프로퍼티에 대한 문서와 자동완성 ※ Spring Configuration Processor
·
Tech/Spring
외부설정 문서화 yml 혹은 properties 파일에 프로퍼티 설정시 SpringBoot에 이미 정의되어 있는 document랑 자동완성을 확인할 수 있다. 개발자가 커스텀한 값들에 대해서도 문서랑 자동완성ㅇ을 지원하는 방법에 대해 알아보자. Spring Configuration Processor란? application.properties, application.yml 파일 등에 넣는 커스텀 설정의 자동 완성, 도움말 등을 지원해주는 도구이다. 실습 환경 Spring Boot 3.2.0 Gradle 8.2 라이브러리 의존성 추가 dependencies { annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'..
Jpa save vs saveAndFlush vs saveAll
·
Tech/JPA
안녕하세요. 회사에서 배치를 돌려 약 10만건 정도의 데이터를 삽입해야 했습니다. 1번과 2번에서 데이터를 조회 하여 데이터를 합친 후 DB에 저장하는 단순한 프로세스 였습니다. DB에 저장시 save()를 사용했다가 반나절 이상이 걸렸습니다. save()를 사용하지 않고 saveAll()로 처리 하였더니 시간이 삼분의 일로 줄어 들었습니다. 이번 기회에 Jpa에서 데이터를 저장하는 방법인 save() / saveAndFlush() / saveAll()의 차이점 대해 정리하려고 합니다. ※ 사실 벌크 삽입은 Spring JDBC의 JdbcTemplate를 이용하면 훨씬 빠르게 Batch Insert를 수행할 수 있습니다. save @Transactional @Override public S save(S ..
Jpa로 데이터 수정 작업 후 수정된 데이터를 카프카에 전송하는 방법
·
Tech/JPA
API를 통해 Mysql DB에서 데이터를 등록 및 수정 작업을 마친 후, 카프카를 통해 최신화된 데이터를 다른 서버에 전송하는 로직이 있다. @Service @RequiredArgsConstructor public class Service { private final KafkaService kafkaService; private final Reader reader; private final Writer writer; @Transactional public void add(Request request) { // 데이터 저장 Entity entity = writer.save(request); // 카프카로 데이터 전송 kafkaService.send(savedEntity); } @Transactional ..
2개 이상의 DB를 사용하는 Spring Boot 환경에서 Spring Data Jpa 사용시 트랜잭션 관련 에러
·
Tech/Spring
한 프로젝트에서 2개 이상의 DB를 연결해서 사용하기 위한 다중 DataSource 설정을 아래와 같이 했다. @Configuration @EnableTransactionManagement(proxyTargetClass = true) @EnableJpaRepositories( entityManagerFactoryRef = "aJpaEntityManagerFactory", basePackages = {"com.soap.repository"} ) public class ADbConfig { @Bean(name = "aDataSource") @ConfigurationProperties(prefix = "spring.a.datasource") public DataSource aDataSource() { retu..
요청으로 들어온 language 값에 따른 GlobalExceptioner에서 다국어 처리 (i18n, yml) + spring validaiton 다국어처리
·
Tech/Spring
기록용 직접 정의한 헤더값의 language값에 따른 예외처리 메시지 다중화 applicaiton.yml spring: messages: basename: i18n/exception encoding: UTF-8 resources/i18n/exception_eng.yml ※ 다른 포맷 사용시 그에 따른 값으로 변경 # exception_eng.yml unKnown: code: "-9999" msg: "An unknown error has occurred. SadPepe :(" userNotFound: code: "-1000" msg: "This member not exist. SadPepe :(" resources/i18n/exception_kor.yml ※ 다른 포맷 사용시 그에 따른 값으로 변경 # e..
Validation 클래스 단위 제약과 조건부 검사 설정
·
Tech/Spring
POST 컨트롤러 호출시 @RequestBody로 요청받는 Request의 dto는 다음과 같습니다. public class Request { private String name; private ColorType colrType; private String redApple; private String greenApple; private String redOrange; private String greenOrange; //getter, setter ... public enum ColorType { RED, GREEN } colorType의 값이 RED면 redApple과 redOrange의 값은 필수, GREEN면 greenApple과 greenOrange의 값에 대한 필수값 체크를 하고 싶었습니다. 우선..