Skip to content

Add Kotlin examples in aot.adoc #33761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions framework-docs/modules/ROOT/pages/core/aot.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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.
Expand Down Expand Up @@ -371,6 +397,15 @@ Java::
// ...
}
----

Kotlin::
+
[source,kotlin,indent=0,subs="verbatim,quotes"]
----
class ClientFactoryBean<T : AbstractClient> : FactoryBean<T> {
// ...
}
----
======

A concrete client declaration should provide a resolved generic for the client, as shown in the following example:
Expand All @@ -391,6 +426,19 @@ Java::

}
----

Kotlin::
+
[source,kotlin,indent=0,subs="verbatim,quotes"]
----
@Configuration(proxyBeanMethods = false)
class UserConfiguration {

@Bean
fun myClient() = ClientFactoryBean<MyClient>(...)

}
----
======

If the `FactoryBean` bean definition is registered programmatically, make sure to follow these steps:
Expand All @@ -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]]
Expand All @@ -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.setPackagesToScan("com.example.app")
return factoryBean
}
----
======

To make sure the scanning occurs ahead of time, a `PersistenceManagedTypes` bean must be declared and used by the
Expand All @@ -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.setManagedTypes(managedTypes)
return factoryBean
}
----
======

[[aot.hints]]
Expand All @@ -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.
Expand Down