From 18665865d2bfd8efddab1cb783c0310c82fcbcfc Mon Sep 17 00:00:00 2001 From: RollW <73376341+Roll-W@users.noreply.github.com> Date: Wed, 16 Oct 2024 00:02:47 +0800 Subject: [PATCH 1/3] Update Kotlin code. --- .../modules/ROOT/pages/core/aot.adoc | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/framework-docs/modules/ROOT/pages/core/aot.adoc b/framework-docs/modules/ROOT/pages/core/aot.adoc index 560c61b4dfa9..80c10e41cb41 100644 --- a/framework-docs/modules/ROOT/pages/core/aot.adoc +++ b/framework-docs/modules/ROOT/pages/core/aot.adoc @@ -290,6 +290,19 @@ Java:: } ---- + +Kotlin:: ++ +[source,kotlin,indent=0,subs="verbatim,quotes"] +---- + @Configuration(proxyBeanMethods = false) + class UserConfiguration { + + @Bean + fun myInterface(): MyInterface = MyImplementation() + + } +---- ====== In the example above, the declared type for the `myInterface` bean is `MyInterface`. @@ -314,6 +327,19 @@ Java:: } ---- + +Kotlin:: ++ +[source,kotlin,indent=0,subs="verbatim,quotes"] +---- + @Configuration(proxyBeanMethods = false) + class UserConfiguration { + + @Bean + fun myInterface() = MyImplementation() + + } +---- ====== If you are registering bean definitions programmatically, consider using `RootBeanBefinition` as it allows to specify a `ResolvableType` that handles generics. @@ -371,6 +397,15 @@ Java:: // ... } ---- + +Kotlin:: ++ +[source,kotlin,indent=0,subs="verbatim,quotes"] +---- + class ClientFactoryBean : FactoryBean { + // ... + } +---- ====== A concrete client declaration should provide a resolved generic for the client, as shown in the following example: @@ -391,6 +426,19 @@ Java:: } ---- + +Kotlin:: ++ +[source,kotlin,indent=0,subs="verbatim,quotes"] +---- + @Configuration(proxyBeanMethods = false) + class UserConfiguration { + + @Bean + ClientFactoryBean myClient() = ClientFactoryBean(...) + + } +---- ====== If the `FactoryBean` bean definition is registered programmatically, make sure to follow these steps: @@ -412,6 +460,16 @@ Java:: // ... registry.registerBeanDefinition("myClient", beanDefinition); ---- + +Kotlin:: ++ +[source,kotlin,indent=0,subs="verbatim,quotes"] +---- + val beanDefinition = RootBeanDefinition(ClientFactoryBean::class.java) + beanDefinition.setTargetType(ResolvableType.forClassWithGenerics(ClientFactoryBean::class.java, MyClient::class.java)); + // ... + registry.registerBeanDefinition("myClient", beanDefinition) +---- ====== [[aot.bestpractices.jpa]] @@ -433,6 +491,19 @@ Java:: return factoryBean; } ---- + +Kotlin:: ++ +[source,kotlin,indent=0,subs="verbatim,quotes"] +---- + @Bean + fun customDBEntityManagerFactory(dataSource: DataSource): LocalContainerEntityManagerFactoryBean { + val factoryBean = LocalContainerEntityManagerFactoryBean(); + factoryBean.dataSource = dataSource + factoryBean.packagesToScan= "com.example.app" + return factoryBean + } +---- ====== To make sure the scanning occurs ahead of time, a `PersistenceManagedTypes` bean must be declared and used by the @@ -458,6 +529,25 @@ Java:: return factoryBean; } ---- + +Kotlin:: ++ +[source,kotlin,indent=0,subs="verbatim,quotes"] +---- + @Bean + fun persistenceManagedTypes(resourceLoader: ResourceLoader): PersistenceManagedTypes { + return PersistenceManagedTypesScanner(resourceLoader) + .scan("com.example.app") + } + + @Bean + fun customDBEntityManagerFactory(dataSource: DataSource, managedTypes: PersistenceManagedTypes): LocalContainerEntityManagerFactoryBean { + val factoryBean = LocalContainerEntityManagerFactoryBean(); + factoryBean.dataSource = dataSource + factoryBean.managedTypes = managedTypes + return factoryBean; + } +---- ====== [[aot.hints]] @@ -479,6 +569,13 @@ Java:: ---- runtimeHints.resources().registerPattern("config/app.properties"); ---- + +Kotlin:: ++ +[source,kotlin,indent=0,subs="verbatim,quotes"] +---- + runtimeHints.resources().registerPattern("config/app.properties") +---- ====== A number of contracts are handled automatically during AOT processing. From f9cff6b90bc281b38f9ad0575a2b8e7620b78adc Mon Sep 17 00:00:00 2001 From: RollW <73376341+Roll-W@users.noreply.github.com> Date: Wed, 16 Oct 2024 23:35:36 +0800 Subject: [PATCH 2/3] Update Kotlin code. --- framework-docs/modules/ROOT/pages/core/aot.adoc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/framework-docs/modules/ROOT/pages/core/aot.adoc b/framework-docs/modules/ROOT/pages/core/aot.adoc index 80c10e41cb41..ef8799484799 100644 --- a/framework-docs/modules/ROOT/pages/core/aot.adoc +++ b/framework-docs/modules/ROOT/pages/core/aot.adoc @@ -498,7 +498,7 @@ Kotlin:: ---- @Bean fun customDBEntityManagerFactory(dataSource: DataSource): LocalContainerEntityManagerFactoryBean { - val factoryBean = LocalContainerEntityManagerFactoryBean(); + val factoryBean = LocalContainerEntityManagerFactoryBean() factoryBean.dataSource = dataSource factoryBean.packagesToScan= "com.example.app" return factoryBean @@ -542,10 +542,10 @@ Kotlin:: @Bean fun customDBEntityManagerFactory(dataSource: DataSource, managedTypes: PersistenceManagedTypes): LocalContainerEntityManagerFactoryBean { - val factoryBean = LocalContainerEntityManagerFactoryBean(); + val factoryBean = LocalContainerEntityManagerFactoryBean() factoryBean.dataSource = dataSource factoryBean.managedTypes = managedTypes - return factoryBean; + return factoryBean } ---- ====== From 136aba93b9b36090578497a8a08dcf9ce634fd14 Mon Sep 17 00:00:00 2001 From: RollW <73376341+Roll-W@users.noreply.github.com> Date: Sun, 20 Oct 2024 23:04:33 +0800 Subject: [PATCH 3/3] Update Kotlin code. --- framework-docs/modules/ROOT/pages/core/aot.adoc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/framework-docs/modules/ROOT/pages/core/aot.adoc b/framework-docs/modules/ROOT/pages/core/aot.adoc index ef8799484799..7d0d30c919b2 100644 --- a/framework-docs/modules/ROOT/pages/core/aot.adoc +++ b/framework-docs/modules/ROOT/pages/core/aot.adoc @@ -435,7 +435,7 @@ Kotlin:: class UserConfiguration { @Bean - ClientFactoryBean myClient() = ClientFactoryBean(...) + fun myClient() = ClientFactoryBean(...) } ---- @@ -500,7 +500,7 @@ Kotlin:: fun customDBEntityManagerFactory(dataSource: DataSource): LocalContainerEntityManagerFactoryBean { val factoryBean = LocalContainerEntityManagerFactoryBean() factoryBean.dataSource = dataSource - factoryBean.packagesToScan= "com.example.app" + factoryBean.setPackagesToScan("com.example.app") return factoryBean } ---- @@ -544,7 +544,7 @@ Kotlin:: fun customDBEntityManagerFactory(dataSource: DataSource, managedTypes: PersistenceManagedTypes): LocalContainerEntityManagerFactoryBean { val factoryBean = LocalContainerEntityManagerFactoryBean() factoryBean.dataSource = dataSource - factoryBean.managedTypes = managedTypes + factoryBean.setManagedTypes(managedTypes) return factoryBean } ----