[英]Kotlin spring-boot @ConfigurationProperties
I'm trying to create the following bean AmazonDynamoDBAsyncClientProvider
.我正在尝试创建以下 bean AmazonDynamoDBAsyncClientProvider
。 I've application.properties that defines endpoint
and tablePrefix
which I'm trying to inject using @ConfigurationProperties
我有定义endpoint
和tablePrefix
,我试图使用@ConfigurationProperties
注入它们
Following is the code snippet for the same.以下是相同的代码片段。 When I run my spring-boot app it doesn't work.当我运行 spring-boot 应用程序时,它不起作用。
I've tried doing the same ConfigurationProperties
class using a regular java class which does set those properties but when it comes to AmazonDynamoDBAsyncClientProvider
, the properties are empty.我已经尝试使用常规 java 类来执行相同的ConfigurationProperties
类,该类确实设置了这些属性,但是当涉及到AmazonDynamoDBAsyncClientProvider
,这些属性为空。 What am I missing here?我在这里缺少什么?
@Component
open class AmazonDynamoDBAsyncClientProvider @Autowired constructor(val dynamoDBConfiguration: DynamoDBConfig){
@Bean open fun getAmazonDBAsync() = AmazonDynamoDBAsyncClientBuilder.standard()
.withEndpointConfiguration(
AwsClientBuilder.EndpointConfiguration(dynamoDBConfiguration.endpoint, dynamoDBConfiguration.prefix))
.build()
}
here is the kotlin bean that I'm trying to autowire with configuration这是我尝试使用配置自动装配的 kotlin bean
@Component
@ConfigurationProperties(value = "dynamo")
open class DynamoDBConfig(var endpoint: String="", var prefix: String="")
finally heres the regular java bean that does get populated with ConfigurationProperties
but when it gets Autowired
I see those properties being empty/null最后继承人的常规 java bean 确实填充了ConfigurationProperties
但是当它被自动Autowired
时,我看到这些属性为空/空
@Component
@ConfigurationProperties("dynamo")
public class DynamoDBConfiguration {
private String endpoint;
private String tablePrefix;
public String getEndpoint() {
return endpoint;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public String getTablePrefix() {
return tablePrefix;
}
public void setTablePrefix(String tablePrefix) {
this.tablePrefix = tablePrefix;
}
}
Have you tried getting rid of the @Component annotation on your ConfigurationProperties class?您是否尝试过去掉 ConfigurationProperties 类上的 @Component 注释?
Here is what I have done with Kotlin and Spring, hope it helps.这是我对 Kotlin 和 Spring 所做的,希望对您有所帮助。
I am trying to leverage the kotlin-spring and kotlin-allopen gradle plugin我正在尝试利用 kotlin-spring 和 kotlin-allopen gradle 插件
dependencies {
classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlinVersion"
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion"
}
apply plugin: 'kotlin-spring'
apply plugin: 'kotlin-noarg'
noArg {
annotation("your.noarg.annotation.package.NoArg")
}
They do make spring development with kotlin a lot easier.它们确实使使用 kotlin 的 Spring 开发变得更加容易。
@ConfigurationProperties("dynamo")
@NoArg
data class DynamoDBConfiguration(var endpoint: String, var prefix: String)
I tried your configuration class and it gets populated.我试过你的配置类,它被填充了。 I think your mistake is in the way you are trying to create the bean, the function needs to be in a class annotated with @Configuration
, this should work:我认为您的错误在于您尝试创建 bean 的方式,该函数需要在用@Configuration
注释的类中,这应该有效:
@Configuration
class Beans {
@Bean
fun getAmazonDBAsync(config: DynamoDBConfiguration) =
AmazonDynamoDBAsyncClientBuilder.standard()
.withEndpointConfiguration(
AwsClientBuilder.EndpointConfiguration(config.endpoint, config.prefix)
)
.build()
}
Spring will inject the config for you, as long as you annotate the config with @Component
, like you did above. Spring 会为你注入配置,只要你像上面那样用@Component
注释配置。
I had a similar problem and fixed it this way:我有一个类似的问题,并以这种方式修复它:
I defined the configuration properties class
with lateinit
vars:我使用lateinit
vars 定义了配置属性class
:
@ConfigurationProperties(prefix = "app")
open class ApplicationConfigProperties {
lateinit var publicUrl: String
}
Then configured a bean in my spring boot application:然后在我的spring boot应用中配置了一个bean:
@SpringBootApplication
open class Application {
@Bean open fun appConfigProperties() = ApplicationConfigProperties()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.