전체 글
-
dto 코틀린으로 변환하기프로젝트/자프링 -> 코프링 마이그레이션 2022. 9. 26. 00:01
dto의 특징 dto는 양이 많다는 특징이 있습니다. 이때 dto 파일 우클릭을 통해 convert java file to kotlin file을 선택해줍니다. 이렇게 되면 변환이 끝나게 되는데 Null이 될 수 있는 값이 그대로 Null이 될 수 없는 Int로 변환될 수 있습니다. 또한 data 키워드를 앞에 붙여 dto클래스들의 toString, hashCode, equals 등을 정의해줄 수 있습니다. 또한 init부분을 취향에 따라 부생성자를 사용하거나 정적 팩토리 메서드로 만들 수 있습니다. class UserResponse(user: User) { val id: Long val name: String val age: Int? init { id = user.id!! name = user.name..
-
Repository와 Service 계층 Kotlin으로 변경하기프로젝트/자프링 -> 코프링 마이그레이션 2022. 9. 25. 00:01
기존 Java Repository interface BookRepository extends JpaRepository { Optional findByName(String bookName); } 변경된 Kotlin Repository interface BookRepository : JpaRepository { fun findByName(bookName: String): Optional } 여기서? 대신에 Optional을 사용한 이유는 Service 계층의 구현 변경 최소화를 위해 사용합니다. 추후에 Service계층을 변환하며 다시 Optional을 제거할 예정입니다. 변경 전 Java Service package com.group.libraryapp.service.user; import com.grou..
-
카카오톡의 서버사이드 코틀린 - 유용하세미나, 영상 요약정리 2022. 9. 24. 00:01
https://tv.kakao.com/channel/3150758/cliplink/391419295 카카오톡의 서버사이드 코틀린 - 유용하님의 영상을 요약한 내용입니다. 개요 개발 언어로 코틀린을 도입하여 카카오톡 서버 구조 개선을 하고자 했습니다. 코틀린/JVM 선택의 배경 성능/안정성, 개발/운영의 편의성 다양한 서버 환경에 검증된 자바의 생태계를 이용 생산성이 좋은 새로운 언어 적정 수준의 러닝 커브 개발 환경은 쓸 만 할까? API 하위 호환성이 잘 이루어지고 버전 업데이트 시 변화가 크지 않습니다. gradle, maven에 간단한 설정만 추가하면 바로 코틀린을 사용할 수 있습니다. 코틀린의 컴파일 과정 1. *.kt 파일 kotlinc 컴파일러에 의해 *.class 파일로 변환 2. 자바 파일..
-
11장 - DSL 만들기Kotlin/코틀린인액션요약 2022. 9. 23. 00:01
11장에서 다루는 내용 영역 특화 언어 만들기 수신 객체 지정 람다 사용 invoke 관례 사용 기존 코틀린 DSL 예제 API에서 DSL으로 궁극적인 목표는 코드의 가독성과 유지 보수성을 좋게 유지하고자 합니다. 코틀린이 지원하는 간결한 구문들 - 확장 함수 - 중위 호출 - 연산자 오버로딩 - get 메서드에 대한 관례 - 람다를 괄호 밖으로 빼내는 관례 - 수신 객체 지정 람다 코틀린 DSL도 온전히 컴파일 시점에 타입이 정해져서 모든 정적 타입 지정 언어의 장점을 얻을 수 있습니다. 영역 특화 언어라는 개념 우리에 가장 친숙한 DSL을 SQL과 정규식입니다. 이런 DSL을 사용하게 되면 범용 언어를 사용하는 경우보다 특정 영역에 대한 연산을 더 간결하게 기술할 수 있습니다. 하지만 범용 애플리케이..
-
10 장 - 애노테이션과 리플랙션Kotlin/코틀린인액션요약 2022. 9. 22. 00:01
10장에서 다루는 내용 애노테이션 적용과 정의 리플렉션을 사용해 실행 시점에 객체 내부 관찰 코틀린 실전 프로젝트 예제 어떤 함수를 호출하려면 그 함수가 정의된 클래스의 이름과 함수 이름, 파라미터 이름등을 알아야 했습니다. 하지만 애노테이션과 리플랙션을 이용하면 그런 제약을 벗어나서 미리 알지 못하는 임의의 클래스를 다룰 수 있습니다. 애너테이션 선언과 적용 코틀린에서는 자바와 같은 방법으로 애노테이션을 사용할 수 있습니다. 예를 들어 JUnit 프레임워크를 사용한다면 테스트 메서드 앞에 @Test 애노테이션을 붙이면 됩니다. 흥미로운 예제로는 @Deprecated 애노테이션입니다. 의미론적으로는 자바와 동일하지만 코틀린에서는 replaceWith 파라미터를 통해 옛버전을 대신할 수 있는 패턴을 제시합..
-
도메인 계층을 Kotlin으로 변경하기프로젝트/자프링 -> 코프링 마이그레이션 2022. 9. 21. 00:01
도메인 계층은 POJO에 JPA Entity라는 특징이 있습니다. 1. Book이라는 Entity를 JavaBook이라는 이름의 Entity로 변경합니다. Shift + F6을 사용하여 rename 하면 편리합니다. package com.group.libraryapp.domain.book; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import static javax.persistence.GenerationType.IDENTITY; @Entity public class JavaBook { @Id @Gener..
-
9장 -제네릭스Kotlin/코틀린인액션요약 2022. 9. 20. 00:01
9장에서 다루는 내용 제네릭 함수와 클래스 정의하는 방법 타입 소거와 실체화한 타입 파라미터 선언 지점과 사용 지점 변성 코틀린에서 제네릭 클래스와 함수를 선언하고 사용하는 기본 개념은 자바와 비슷합니다. 실제로 제네릭스가 쓰였던 일부 예제에서도 괴리감이 느껴지지 않았습니다. 제네릭 타입 파라미터 타입 파라미터를 받을 때 받는 타입을 정의할 수 있습니다. 만약 문자열을 담는 리스트를 표현할 때 List 으로 표현합니다. 코틀린 컴파일러는 타입 인자도 추론할 수 있습니다. val authors = listOf("Dmitry", "Svetlana") listOf에 전달된 두 값이 문자열이므로 컴파일러는 List임을 추론합니다. 제네릭 함수와 프로퍼티 fun List.slice(indices: IntRange..
-
8장 - 고차 함수 : 파라미터와 반환 값으로 람다 사용Kotlin/코틀린인액션요약 2022. 9. 19. 00:01
8장에서 다루는 내용 - 함수 타입 - 고차 함수와 코드를 구조화할 때 고차 함수를 사용하는 방법 - 인라인 함수 - 비로컬 return과 레이블 - 무명 함수 고차 함수 정의 고차 함수는 다른 함수를 인자로 받거나 함수를 반환하는 함수입니다. 코틀린에서는 람다나 함수 참조를 사용해 함수를 값으로 표현할 수 있습니다. 예를 들어 표준 라이브러리 함수인 filter는 술어 함수를 인자로 받으므로 고차 함수입니다. list.filter{x > 0 } 간단한 고차 함수 정의하기 fun twoAndThree (operation : (Int, Int) -> Int){ val result = operation(2,3) println("The result is ${result}") } twoAnddThree{a,b ..