-
Notifications
You must be signed in to change notification settings - Fork 40.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix detection of Autowired constructor with Kotlin
Previously, the import selector wrongly assumed that we should not use constructor injection with Kotlin. Rather than looking up for the primary constructor, we retrieve available constructors on the Java counter-part. This commit applies the same logic as in the constructor parameter binder and checks for the primary constructor for Kotlin types. See gh-8762
- Loading branch information
Showing
2 changed files
with
111 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...amework/boot/context/properties/KotlinEnableConfigurationPropertiesImportSelectorTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package org.springframework.boot.context.properties | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.beans.factory.support.DefaultListableBeanFactory | ||
import org.springframework.beans.factory.support.GenericBeanDefinition | ||
import org.springframework.core.type.AnnotationMetadata | ||
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory | ||
|
||
/** | ||
* Tests for `EnableConfigurationPropertiesImportSelector`. | ||
* | ||
* @author Stephane Nicoll | ||
*/ | ||
@Suppress("unused") | ||
class KotlinEnableConfigurationPropertiesImportSelectorTests { | ||
|
||
private val registrar = EnableConfigurationPropertiesImportSelector.ConfigurationPropertiesBeanRegistrar() | ||
|
||
private val beanFactory = DefaultListableBeanFactory() | ||
|
||
|
||
@Test | ||
fun `type with default constructor should register generic bean definition`() { | ||
this.registrar.registerBeanDefinitions( | ||
getAnnotationMetadata(TestConfiguration::class.java), this.beanFactory) | ||
val beanDefinition = this.beanFactory.getBeanDefinition( | ||
"foo-org.springframework.boot.context.properties.KotlinEnableConfigurationPropertiesImportSelectorTests\$FooProperties") | ||
assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition::class.java) | ||
} | ||
|
||
@Test | ||
fun `type with autowired on constructor should register generic bean definition`() { | ||
this.registrar.registerBeanDefinitions( | ||
getAnnotationMetadata(TestConfiguration::class.java), this.beanFactory) | ||
val beanDefinition = this.beanFactory.getBeanDefinition( | ||
"bar-org.springframework.boot.context.properties.KotlinEnableConfigurationPropertiesImportSelectorTests\$BarProperties") | ||
assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition::class.java) | ||
} | ||
|
||
@Test | ||
fun `type with primary constructor and no autowired should register configuration properties bean definition`() { | ||
this.registrar.registerBeanDefinitions( | ||
getAnnotationMetadata(TestConfiguration::class.java), this.beanFactory) | ||
val beanDefinition = this.beanFactory.getBeanDefinition( | ||
"baz-org.springframework.boot.context.properties.KotlinEnableConfigurationPropertiesImportSelectorTests\$BazProperties") | ||
assertThat(beanDefinition).isExactlyInstanceOf(ConfigurationPropertiesBeanDefinition::class.java) | ||
} | ||
|
||
@Test | ||
fun `type with no primary constructor should register generic bean definition`() { | ||
this.registrar.registerBeanDefinitions( | ||
getAnnotationMetadata(TestConfiguration::class.java), this.beanFactory) | ||
val beanDefinition = this.beanFactory.getBeanDefinition( | ||
"bing-org.springframework.boot.context.properties.KotlinEnableConfigurationPropertiesImportSelectorTests\$BingProperties") | ||
assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition::class.java) | ||
} | ||
|
||
private fun getAnnotationMetadata(source: Class<*>): AnnotationMetadata { | ||
return SimpleMetadataReaderFactory().getMetadataReader(source.name) | ||
.annotationMetadata | ||
} | ||
|
||
|
||
@EnableConfigurationProperties(FooProperties::class, BarProperties::class, BazProperties::class, BingProperties::class) | ||
class TestConfiguration | ||
|
||
@ConfigurationProperties(prefix = "foo") | ||
class FooProperties | ||
|
||
@ConfigurationProperties(prefix = "bar") | ||
class BarProperties @Autowired constructor(val foo: String) | ||
|
||
@ConfigurationProperties(prefix = "baz") | ||
class BazProperties(val name: String?, val counter: Int = 42) | ||
|
||
@ConfigurationProperties(prefix = "bing") | ||
class BingProperties { | ||
|
||
constructor() | ||
|
||
constructor(@Suppress("UNUSED_PARAMETER") foo: String) | ||
|
||
} | ||
|
||
} |