Skip to content

Commit fc0597a

Browse files
committed
Configure Kotlin Spring plugin for proper proxy support
Update both Maven and Gradle build configurations to properly support Kotlin with Spring Framework. The Kotlin Spring plugin automatically handles making configuration classes open for proxying, eliminating the need for manual 'open' modifiers. Update maven generation scripts to handle kotlin plugin. Add execution compile section so classes are included in the jar. This can be tested via mvn clean package or to run mvn exec:java -Dexec.mainClass="org.springframework.integration.samples.helloworld.PollerApp" Changes include: - Add kotlin-spring and kotlin-allopen plugins to build configurations - Remove unnecessary 'open' modifiers from @configuration classes - Configure Kotlin Maven plugin with Spring compiler plugin - Simplify QueueChannel initialization to use default capacity - Update MessageProcessorMessageSource to use simpler lambda syntax - Adjust logging configuration to reduce noise from Spring framework
1 parent af71c1e commit fc0597a

File tree

6 files changed

+91
-27
lines changed

6 files changed

+91
-27
lines changed

basic/helloworld-groovy/src/main/groovy/org/springframework/integration/samples/helloworld/HelloWorldConfig.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class HelloWorldConfig {
4343

4444
@Bean
4545
MessageChannel outputChannel() {
46-
new QueueChannel(10)
46+
new QueueChannel()
4747
}
4848

4949
@Bean

basic/helloworld-kotlin/pom.xml

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@
6464
<dependency>
6565
<groupId>org.jetbrains.kotlin</groupId>
6666
<artifactId>kotlin-stdlib</artifactId>
67-
<version>${kotlin.version}</version>
67+
<version>2.2.21</version>
6868
</dependency>
6969
<dependency>
7070
<groupId>org.jetbrains.kotlin</groupId>
7171
<artifactId>kotlin-reflect</artifactId>
72-
<version>${kotlin.version}</version>
72+
<version>2.2.21</version>
7373
</dependency>
7474
<dependency>
7575
<groupId>org.junit</groupId>
@@ -183,7 +183,6 @@
183183
</dependencies>
184184
<properties>
185185
<java.version>17</java.version>
186-
<kotlin.version>2.2.21</kotlin.version>
187186
</properties>
188187
<repositories>
189188
<repository>
@@ -198,11 +197,21 @@
198197
</repository>
199198
</repositories>
200199
<build>
200+
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
201+
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
201202
<plugins>
202203
<plugin>
203204
<groupId>org.jetbrains.kotlin</groupId>
204205
<artifactId>kotlin-maven-plugin</artifactId>
205-
<version>${kotlin.version}</version>
206+
<version>2.2.21</version>
207+
<configuration>
208+
<args>
209+
<arg>-Xjsr305=strict</arg>
210+
</args>
211+
<compilerPlugins>
212+
<plugin>spring</plugin>
213+
</compilerPlugins>
214+
</configuration>
206215
<executions>
207216
<execution>
208217
<id>compile</id>
@@ -219,12 +228,13 @@
219228
</goals>
220229
</execution>
221230
</executions>
222-
<configuration>
223-
<jvmTarget>17</jvmTarget>
224-
<args>
225-
<arg>-Xjdk-release=17</arg>
226-
</args>
227-
</configuration>
231+
<dependencies>
232+
<dependency>
233+
<groupId>org.jetbrains.kotlin</groupId>
234+
<artifactId>kotlin-maven-allopen</artifactId>
235+
<version>2.2.21</version>
236+
</dependency>
237+
</dependencies>
228238
</plugin>
229239
</plugins>
230240
</build>

basic/helloworld-kotlin/src/main/kotlin/org/springframework/integration/samples/helloworld/HelloWorldConfig.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import org.springframework.messaging.MessageChannel
3131
*/
3232
@Configuration(proxyBeanMethods = false)
3333
@EnableIntegration
34-
open class HelloWorldConfig {
34+
class HelloWorldConfig {
3535

3636
/**
3737
* Creates the input channel for inbound messages.
@@ -43,19 +43,19 @@ open class HelloWorldConfig {
4343
* @return A [DirectChannel] instance for synchronous inbound message delivery
4444
*/
4545
@Bean
46-
open fun inputChannel() = DirectChannel()
46+
fun inputChannel() = DirectChannel()
4747

4848
/**
4949
* Creates the output channel for outbound messages.
5050
*
51-
* A [QueueChannel] with capacity of 10 messages provides asynchronous,
51+
* A [QueueChannel] with default capacity provides asynchronous,
5252
* buffered message delivery. Results from the integration flow are queued
5353
* and available for downstream consumption.
5454
*
55-
* @return A [QueueChannel] instance with capacity of 10 messages
55+
* @return A [QueueChannel] instance with default capacity.
5656
*/
5757
@Bean
58-
open fun outputChannel() = QueueChannel(10)
58+
fun outputChannel() = QueueChannel()
5959

6060
/**
6161
* Creates the Hello World business service.
@@ -66,7 +66,7 @@ open class HelloWorldConfig {
6666
* @return A [HelloService] instance
6767
*/
6868
@Bean
69-
open fun helloService() = HelloService()
69+
fun helloService() = HelloService()
7070

7171
/**
7272
* Defines the main integration flow for message processing.
@@ -77,7 +77,7 @@ open class HelloWorldConfig {
7777
* @return An IntegrationFlow representing the complete message flow
7878
*/
7979
@Bean
80-
open fun helloWorldFlow(inputChannel: MessageChannel, outputChannel: MessageChannel, helloService: HelloService) =
80+
fun helloWorldFlow(inputChannel: MessageChannel, outputChannel: MessageChannel, helloService: HelloService) =
8181
integrationFlow(inputChannel) {
8282
handle(helloService, "sayHello")
8383
channel(outputChannel)

basic/helloworld-kotlin/src/main/kotlin/org/springframework/integration/samples/helloworld/PollerConfig.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import org.springframework.context.annotation.Configuration
2121
import org.springframework.integration.channel.NullChannel
2222
import org.springframework.integration.config.EnableIntegration
2323
import org.springframework.integration.dsl.integrationFlow
24-
import org.springframework.integration.endpoint.MessageProcessorMessageSource
2524
import org.springframework.integration.handler.LoggingHandler
2625

2726
/**
@@ -32,7 +31,7 @@ import org.springframework.integration.handler.LoggingHandler
3231
*/
3332
@Configuration(proxyBeanMethods = false)
3433
@EnableIntegration
35-
open class PollerConfig {
34+
class PollerConfig {
3635

3736
/**
3837
* Defines a polling-based integration flow for periodic message generation.
@@ -45,9 +44,9 @@ open class PollerConfig {
4544
* @return An IntegrationFlow that periodically generates and logs timestamps, then discards them.
4645
*/
4746
@Bean
48-
open fun pollerFlow() =
47+
fun pollerFlow() =
4948
integrationFlow(
50-
MessageProcessorMessageSource { System.currentTimeMillis() },
49+
{ System.currentTimeMillis() },
5150
{ poller { it.fixedDelay(20000).maxMessagesPerPoll(2) } }) {
5251
log(LoggingHandler.Level.INFO, "org.springframework.integration.samples.helloworld")
5352
channel(NullChannel())

basic/helloworld-kotlin/src/main/resources/log4j2.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
</Console>
77
</Appenders>
88
<Loggers>
9-
<Logger name="org.springframework" level="info"/>
10-
<Root level="info">
9+
<Logger name="org.springframework" level="warn"/>
10+
<Logger name="org.springframework.integration.samples.helloworld" level="info"/>
11+
<Root level="warn">
1112
<AppenderRef ref="Console"/>
1213
</Root>
1314
</Loggers>

build.gradle

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ buildscript {
1010
classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion"
1111
classpath 'org.gretty:gretty:4.1.10'
1212
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:2.2.21'
13+
classpath 'org.jetbrains.kotlin:kotlin-allopen:2.2.21'
1314
}
1415
}
1516

@@ -178,6 +179,56 @@ subprojects { subproject ->
178179
}
179180
}
180181

182+
if (subproject.plugins.hasPlugin('org.jetbrains.kotlin.jvm')) {
183+
def kotlinVersion = '2.2.21'
184+
def plugins = asNode().build?.find()?.plugins?.find()
185+
if (!plugins) {
186+
plugins = asNode().appendNode('build').with {
187+
appendNode('sourceDirectory', '${project.basedir}/src/main/kotlin')
188+
appendNode('testSourceDirectory', '${project.basedir}/src/test/kotlin')
189+
appendNode('plugins')
190+
}
191+
}
192+
193+
plugins.appendNode('plugin')
194+
.with {
195+
appendNode('groupId', 'org.jetbrains.kotlin')
196+
appendNode('artifactId', 'kotlin-maven-plugin')
197+
appendNode('version', kotlinVersion)
198+
appendNode('configuration').with {
199+
appendNode('args').with {
200+
appendNode('arg', '-Xjsr305=strict')
201+
}
202+
appendNode('compilerPlugins').with {
203+
appendNode('plugin', 'spring')
204+
}
205+
}
206+
appendNode('executions').with {
207+
appendNode('execution').with {
208+
appendNode('id', 'compile')
209+
appendNode('phase', 'compile')
210+
appendNode('goals').with {
211+
appendNode('goal', 'compile')
212+
}
213+
}
214+
appendNode('execution').with {
215+
appendNode('id', 'test-compile')
216+
appendNode('phase', 'test-compile')
217+
appendNode('goals').with {
218+
appendNode('goal', 'test-compile')
219+
}
220+
}
221+
}
222+
appendNode('dependencies').with {
223+
appendNode('dependency').with {
224+
appendNode('groupId', 'org.jetbrains.kotlin')
225+
appendNode('artifactId', 'kotlin-maven-allopen')
226+
appendNode('version', kotlinVersion)
227+
}
228+
}
229+
}
230+
}
231+
181232
def pomDeps = asNode().dependencies.find()
182233
if (!pomDeps) {
183234
pomDeps = asNode().appendNode('dependencies')
@@ -620,7 +671,8 @@ project('helloworld-groovy') {
620671
api "org.springframework.integration:spring-integration-groovy"
621672
testImplementation "org.apache.logging.log4j:log4j-core-test:$log4jVersion"
622673
testImplementation "org.awaitility:awaitility:$awaitilityVersion"
623-
testImplementation("org.assertj:assertj-core:$assertjVersion") }
674+
testImplementation("org.assertj:assertj-core:$assertjVersion")
675+
}
624676

625677
tasks.register ('runHelloWorldApp', JavaExec) {
626678
mainClass = 'org.springframework.integration.samples.helloworld.HelloWorldApp'
@@ -637,7 +689,8 @@ project('helloworld-groovy') {
637689
project('helloworld-kotlin') {
638690
description = 'Hello World Sample for Kotlin Developers'
639691

640-
apply plugin: 'org.jetbrains.kotlin.jvm'
692+
apply plugin: 'kotlin'
693+
apply plugin: 'kotlin-spring'
641694

642695
dependencies {
643696
api "org.apache.logging.log4j:log4j-core:$log4jVersion"
@@ -646,7 +699,8 @@ project('helloworld-kotlin') {
646699
api "org.jetbrains.kotlin:kotlin-reflect"
647700
testImplementation "org.apache.logging.log4j:log4j-core-test:$log4jVersion"
648701
testImplementation "org.awaitility:awaitility:$awaitilityVersion"
649-
testImplementation("org.assertj:assertj-core:$assertjVersion") }
702+
testImplementation("org.assertj:assertj-core:$assertjVersion")
703+
}
650704

651705
tasks.register ('runHelloWorldApp', JavaExec) {
652706
mainClass = 'org.springframework.integration.samples.helloworld.HelloWorldApp'

0 commit comments

Comments
 (0)