البرمجة

تكوين الخصائص في Spring Boot باستخدام Kotlin

للأسف، قد تواجه بعض التحديات عند استخدام Kotlin مع spring-boot-configuration-processor لإنشاء ملف spring-configuration-metadata.json. على الرغم من أن Kotlin يولد الشيفرة المرئية بنفس الطريقة كما يفعل Java، إلا أن هناك بعض الاختلافات الدقيقة في كيفية تفاعل Kotlin مع المعالج.

لحل هذه المشكلة، يمكنك اتباع الخطوات التالية:

  1. تأكد من تضمين المعالج في ملف build.gradle الخاص بك:

    gradle
    dependencies { ... implementation 'org.springframework.boot:spring-boot-configuration-processor' }
  2. تأكد من تضمين المعلومات اللازمة في ملف build.gradle الخاص بك:

    gradle
    apply plugin: 'kotlin-kapt' dependencies { ... kapt 'org.springframework.boot:spring-boot-configuration-processor' }
  3. استخدام توصيف المكون (component description) في Kotlin:

    kotlin
    import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.stereotype.Component @Component @ConfigurationProperties("myprops") class MyProps { var hello: String? = null }

    عليك أيضًا التأكد من وجود META-INF/spring-configuration-metadata.json في مسار الإخراج (output path) الخاص بمشروعك بعد بناءه. يجب أن يحتوي هذا الملف على معلومات تكوين الخصائص التي تم تعريفها في كلاس Kotlin الخاص بك.

المزيد من المعلومات

بالتأكيد! لديك أيضًا الخيار لاستخدام @ConstructorBinding في Kotlin بدلاً من @ConfigurationProperties، وهو خيار أكثر تقدمًا يستخدم لربط القيم من الملفات المُعينة (properties files) بكائن Kotlin دون الحاجة إلى واجهة (interface).

لتحقيق ذلك، يجب عليك تعريف الكلاس كمصنف (data class) واستخدام التوصيف المعين (@ConstructorBinding) على الكلاس مع القيم المُعينة (@Value):

kotlin
import org.springframework.boot.context.properties.ConstructorBinding import org.springframework.beans.factory.annotation.Value @ConstructorBinding data class MyProps(@Value("\${myprops.hello}") val hello: String)

ثم يجب تكوين الملف application.properties أو application.yml لتحديد قيمة myprops.hello:

properties
myprops.hello=Hello, World!

أخيرًا، يجب عليك إعداد المشروع لدعم المعالجة المسبقة (kapt) في ملف build.gradle:

gradle
plugins { id 'org.springframework.boot' version '2.6.4' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'kotlin-jvm' version '1.6.10' id 'org.jetbrains.kotlin.kapt' version '1.6.10' } group = 'com.example' version = '0.0.1-SNAPSHOT' java.sourceCompatibility = '1.8' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-configuration-processor' implementation 'org.jetbrains.kotlin:kotlin-reflect' implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-web' testImplementation 'org.springframework.boot:spring-boot-test' testImplementation 'org.springframework.boot:spring-boot-test-autoconfigure' testImplementation 'org.springframework.boot:spring-boot-configuration-processor' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.jetbrains.kotlin:kotlin-test' testImplementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8' } test { useJUnitPlatform() }

بعد تنفيذ هذه الخطوات، يجب أن يتمكن المشروع من تحميل قيم الخصائص من الملفات المُعينة بنجاح واستخدامها في كائن Kotlin المحدد.

مقالات ذات صلة

زر الذهاب إلى الأعلى

هذا المحتوى محمي من النسخ لمشاركته يرجى استعمال أزرار المشاركة السريعة أو تسخ الرابط !!