사용자 도구

사이트 도구


kotlin:kotlin_spring

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
kotlin:kotlin_spring [2019/09/09 16:39]
kwon37xi
kotlin:kotlin_spring [2022/10/04 17:12] (현재)
kwon37xi
줄 1: 줄 1:
-====== Kotlin with SpringFramework ======+====== Kotlin with SpringFramework / JPA ======
   * [[:springframework|Spring Framework]]   * [[:springframework|Spring Framework]]
 +  * [[java:jpa|JPA]]
 +
 +===== all-open / kotling-spring compiler plugin =====
 +  * https://kotlinlang.org/docs/all-open-plugin.html
 +  * Spring 의 경우 ''all-open'' 말고, ''kotlin-spring'' 플러그인을 사용하는게 낫다.
 +  * Kotlin 이 기본 ''final'' 클래스를 만드는 것을 특정 AOP 애노테이션(''@Transactiona'', ''@Component'' 등이 붙어있으면 ''open''으로 컴파일 시점에 변경해줌.
 +  * [[:intellij_idea|IntelliJ IDEA]] **File -> Project Structure -> Project Settings -> Modules -> Kotlin -> Complier Plugins** 에서 적용된 컴파일러 플러그인 목록 확인가능.
 +
 +===== no-arg / kotlin-jpa plugin =====
 +  * JPA Entity나, 일부 프레임워크는 기본 생성자를 필수적으로 필요로 한다.
 +  * [[https://kotlinlang.org/docs/no-arg-plugin.html|No-arg compiler plugin | Kotlin]]
 +  * ''kotlin-jpa'' 플러그인은 ''no-arg'' 플러그인을 래핑해서 JPA Entity 애노테이션이 있는 클래스에 자동으로 기본 생성자를 생성해준다. 
 +
 +===== JPA 지연 로딩에 대해서 강제로 all open 지정 필요 =====
 +  * JPA lazy loading 을 하려면 Entity가 ''final''이면 안됨. ''kotlin-spring'' 플러그인을 적용해도 자동으로 Entity에 all-open 이 적용이 안되고 있음. 명시적 지정필요.
 +
 +<code kotlin>
 +// gradle
 +plugins {
 +    kotlin("plugin.spring") version ".."
 +    kotlin("plugin.jpa") version "..."
 +}
 +allOpen {
 +  annotation("javax.persistence.Entity")
 +  annotation("javax.persistence.MappedSuperclass")
 +}
 +</code>
 +
 +===== QueryDSL =====
 +  * [[https://kotlinlang.org/docs/kapt.html|Using kapt]] 로 적용 가능.
 +
 +===== @Transient 는 getter 사용 =====
 +  * JPA ''@Transient'' 에는 사용자 지정 getter 를 지정해야한다. 안그러면 초기화가 안된다.
 +
 +<code kotlin>
 +// @Transient 불필요.
 +val fixed: Boolean
 +    get() = startDate.until(endDate).years < 1
 +</code>
 +
 +
 +
 +===== Field 초기화는 lateinit =====
 +  * 필드 포기화가 필요할 경우에는 ''lateinit'' 을 사용하면 null safe 한 필드 주입이 가능하면서 지연 초기화가 가능하다.
 +  * ''var'' 로 선언해야 함
 +<code kotlin>
 +@Autowired
 +private lateinit var objectMapper: ObjectMapper
 +</code>
 +''@Entity'', ''@Embeddable'', ''@MappedSuperclass'' 등.
 +
 +===== Jackson =====
 +  * [[java:jackson|Java Jackson JSON Library]]
 +  * ''jacksonObjectMapper()'' 를 사용하면 Kotlin 모듈 자동 주입.
 +  * ''ObjectMapper()''로 생성할거라면 ''registerKotlinModule()'' 호출 필요.
 +
 +===== @ConfigurationProperties / @ConstructorBinding =====
 +  * Immutable data class 에 대해 [[springframework:springboot|SpringBoot]] 2.2 부터 생성자 바인딩 지원
 +
 +<code kotlin>
 +@ConfigurationProperties("...")
 +@ConstructorBinding
 +data class MyProperties(val url: String)
 +</code>
 +
 +===== 참조 =====
   * [[https://github.com/sdeleuze/spring-kotlin|Spring-Kotlin]]   * [[https://github.com/sdeleuze/spring-kotlin|Spring-Kotlin]]
   * [[https://github.com/MarioAriasC/KotlinPrimavera|Kotlin Primavera]]   * [[https://github.com/MarioAriasC/KotlinPrimavera|Kotlin Primavera]]
   * [[https://github.com/spring-projects/spring-fu|Spring fu]] Explicit configuration for Spring Boot using Kotlin and Kofu DSL   * [[https://github.com/spring-projects/spring-fu|Spring fu]] Explicit configuration for Spring Boot using Kotlin and Kofu DSL
 +  * [[https://12bme.tistory.com/578|[kotlin] 스프링 프레임워크 개발]] 
 +  * [[https://cheese10yun.github.io/spring-kotlin/|Kotlin으로 Spring 개발할 때 - Yun Blog | 기술 블로그]] 
 +  * [[https://github.com/tmdgusya/kotlin-spring-jsr-303-issue|tmdgusya/kotlin-spring-jsr-303-issue]]
kotlin/kotlin_spring.1568014766.txt.gz · 마지막으로 수정됨: 2019/09/09 16:39 저자 kwon37xi