Skip to content

Commit 6c93c67

Browse files
roll-wsdeleuze
authored andcommitted
Add Kotlin code samples to the AOT documentation
Closes gh-33761
1 parent 599d534 commit 6c93c67

File tree

1 file changed

+97
-0
lines changed
  • framework-docs/modules/ROOT/pages/core

1 file changed

+97
-0
lines changed

framework-docs/modules/ROOT/pages/core/aot.adoc

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,19 @@ Java::
290290
291291
}
292292
----
293+
294+
Kotlin::
295+
+
296+
[source,kotlin,indent=0,subs="verbatim,quotes"]
297+
----
298+
@Configuration(proxyBeanMethods = false)
299+
class UserConfiguration {
300+
301+
@Bean
302+
fun myInterface(): MyInterface = MyImplementation()
303+
304+
}
305+
----
293306
======
294307

295308
In the example above, the declared type for the `myInterface` bean is `MyInterface`.
@@ -314,6 +327,19 @@ Java::
314327
315328
}
316329
----
330+
331+
Kotlin::
332+
+
333+
[source,kotlin,indent=0,subs="verbatim,quotes"]
334+
----
335+
@Configuration(proxyBeanMethods = false)
336+
class UserConfiguration {
337+
338+
@Bean
339+
fun myInterface() = MyImplementation()
340+
341+
}
342+
----
317343
======
318344

319345
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::
371397
// ...
372398
}
373399
----
400+
401+
Kotlin::
402+
+
403+
[source,kotlin,indent=0,subs="verbatim,quotes"]
404+
----
405+
class ClientFactoryBean<T : AbstractClient> : FactoryBean<T> {
406+
// ...
407+
}
408+
----
374409
======
375410

376411
A concrete client declaration should provide a resolved generic for the client, as shown in the following example:
@@ -391,6 +426,19 @@ Java::
391426
392427
}
393428
----
429+
430+
Kotlin::
431+
+
432+
[source,kotlin,indent=0,subs="verbatim,quotes"]
433+
----
434+
@Configuration(proxyBeanMethods = false)
435+
class UserConfiguration {
436+
437+
@Bean
438+
fun myClient() = ClientFactoryBean<MyClient>(...)
439+
440+
}
441+
----
394442
======
395443

396444
If the `FactoryBean` bean definition is registered programmatically, make sure to follow these steps:
@@ -412,6 +460,16 @@ Java::
412460
// ...
413461
registry.registerBeanDefinition("myClient", beanDefinition);
414462
----
463+
464+
Kotlin::
465+
+
466+
[source,kotlin,indent=0,subs="verbatim,quotes"]
467+
----
468+
val beanDefinition = RootBeanDefinition(ClientFactoryBean::class.java)
469+
beanDefinition.setTargetType(ResolvableType.forClassWithGenerics(ClientFactoryBean::class.java, MyClient::class.java));
470+
// ...
471+
registry.registerBeanDefinition("myClient", beanDefinition)
472+
----
415473
======
416474

417475
[[aot.bestpractices.jpa]]
@@ -433,6 +491,19 @@ Java::
433491
return factoryBean;
434492
}
435493
----
494+
495+
Kotlin::
496+
+
497+
[source,kotlin,indent=0,subs="verbatim,quotes"]
498+
----
499+
@Bean
500+
fun customDBEntityManagerFactory(dataSource: DataSource): LocalContainerEntityManagerFactoryBean {
501+
val factoryBean = LocalContainerEntityManagerFactoryBean()
502+
factoryBean.dataSource = dataSource
503+
factoryBean.setPackagesToScan("com.example.app")
504+
return factoryBean
505+
}
506+
----
436507
======
437508

438509
To make sure the scanning occurs ahead of time, a `PersistenceManagedTypes` bean must be declared and used by the
@@ -458,6 +529,25 @@ Java::
458529
return factoryBean;
459530
}
460531
----
532+
533+
Kotlin::
534+
+
535+
[source,kotlin,indent=0,subs="verbatim,quotes"]
536+
----
537+
@Bean
538+
fun persistenceManagedTypes(resourceLoader: ResourceLoader): PersistenceManagedTypes {
539+
return PersistenceManagedTypesScanner(resourceLoader)
540+
.scan("com.example.app")
541+
}
542+
543+
@Bean
544+
fun customDBEntityManagerFactory(dataSource: DataSource, managedTypes: PersistenceManagedTypes): LocalContainerEntityManagerFactoryBean {
545+
val factoryBean = LocalContainerEntityManagerFactoryBean()
546+
factoryBean.dataSource = dataSource
547+
factoryBean.setManagedTypes(managedTypes)
548+
return factoryBean
549+
}
550+
----
461551
======
462552

463553
[[aot.hints]]
@@ -479,6 +569,13 @@ Java::
479569
----
480570
runtimeHints.resources().registerPattern("config/app.properties");
481571
----
572+
573+
Kotlin::
574+
+
575+
[source,kotlin,indent=0,subs="verbatim,quotes"]
576+
----
577+
runtimeHints.resources().registerPattern("config/app.properties")
578+
----
482579
======
483580

484581
A number of contracts are handled automatically during AOT processing.

0 commit comments

Comments
 (0)