当前位置: 首页 > 知识库问答 >
问题:

未绑定==>concur KV Kotlin Spring Boot@ConfigurationProperties@ConstructorBinding

钮安歌
2023-03-14

我需要把我们的application.yml配置转移到哈希公司的领事那里。我已经创建了一个简单的Spring Boot应用程序来完成所需的更改。它将配置数据从application.yml加载到强类型类。

但是,在对应用程序进行注释之后。yml文件和引导中的启用领事。yml文件,当我尝试访问Concur KV中的相同配置时,配置数据总是空的,Concur KV在我的系统上作为Docker映像运行。我一定错过了一些简单的东西,但我完全不知道它是什么。以下是相关信息。

////////////////////////////////////////
// MyConsulApplication.kt
////////////////////////////////////////
@SpringBootApplication(scanBasePackages = ["com.example.myconsul"])
class MyConsulApplication

fun main(args: Array<String>) {
    runApplication<MyConsulApplication>(*args)
}
////////////////////////////////////////
// application.yml
////////////////////////////////////////
#myconsul:
#  message: howdy
////////////////////////////////////////
// bootstrap.yml
////////////////////////////////////////
server:
  port: 8080

spring:
  application:
    name: myconsul
  profiles:
    active: default
  cloud:
    consul:
      host: localhost
      port: 8500
      config:
        enabled: true  # Set to false to use the local application.yml file rather than the Consul configuration server.
        prefix: config
        defaultContext: application
      discovery:
        enabled: true
        instanceId: ${spring.application.name}:${random.value}
        healthCheckPath: /
        healthCheckInterval: 30s
////////////////////////////////////////
// AppProperties.kt
////////////////////////////////////////
@ConstructorBinding
//@RefreshScope
//@Scope("singleton")  // This is the default, but I wanted to be explicit about it.
@EnableConfigurationProperties(AppProperties::class)
@ConfigurationProperties("myconsul")
data class AppProperties(val message: String?) {

    @PostConstruct
    fun postConstruct() {
        println("====> $message")
    }
}
////////////////////////////////////////
// Consul KT Export
////////////////////////////////////////
[
    {
        "key": "config/myconsul/message",
        "flags": 0,
        "value": "SGFwcHkgSGFsbG93ZWVuIQ=="
    }
]
////////////////////////////////////////
// build.gradle.kts Dependencies
////////////////////////////////////////
extra["springCloudVersion"] = "Hoxton.BUILD-SNAPSHOT"

dependencies {
    // kapt dependencies required for IntelliJ auto complete of kotlin config properties class
    kapt("org.springframework.boot:spring-boot-configuration-processor")
    annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
    implementation("org.springframework.cloud:spring-cloud-starter-consul-config")
    implementation("org.springframework.cloud:spring-cloud-starter-consul-discovery")

    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
}
////////////////////////////////////////
// Console Logging of Launch
////////////////////////////////////////

2019-11-02 22:49:04.510  INFO 5401 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.retry.annotation.RetryConfiguration' of type [org.springframework.retry.annotation.RetryConfiguration$$EnhancerBySpringCGLIB$$1d6cbd94] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-02 22:49:04.519  INFO 5401 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$43c3286c] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

 :: Spring Boot ::  (v2.2.1.BUILD-SNAPSHOT)

2019-11-02 22:49:05.521  INFO 5401 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='consul', propertySources=[ConsulPropertySource {name='config/myconsul,default/'}, ConsulPropertySource {name='config/myconsul/'}, ConsulPropertySource {name='config/application,default/'}, ConsulPropertySource {name='config/application/'}]}
2019-11-02 22:49:05.526  INFO 5401 --- [           main] c.e.myconsul.MyConsulApplicationKt       : The following profiles are active: default
2019-11-02 22:49:06.453  WARN 5401 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'bus-env' contains invalid characters, please migrate to a valid format.
2019-11-02 22:49:06.658  WARN 5401 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'bus-refresh' contains invalid characters, please migrate to a valid format.
2019-11-02 22:49:06.745  WARN 5401 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'service-registry' contains invalid characters, please migrate to a valid format.
2019-11-02 22:49:06.901  INFO 5401 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=6becbf98-b3f5-3e17-8c2f-cd8e1a8f2d5d
2019-11-02 22:49:06.956  INFO 5401 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'errorChannel' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created.
2019-11-02 22:49:06.961  INFO 5401 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'taskScheduler' has been explicitly defined. Therefore, a default ThreadPoolTaskScheduler will be created.
2019-11-02 22:49:06.965  INFO 5401 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'integrationHeaderChannelRegistry' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created.
2019-11-02 22:49:07.006  INFO 5401 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.retry.annotation.RetryConfiguration' of type [org.springframework.retry.annotation.RetryConfiguration$$EnhancerBySpringCGLIB$$1d6cbd94] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-02 22:49:07.041  INFO 5401 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationChannelResolver' of type [org.springframework.integration.support.channel.BeanFactoryChannelResolver] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-02 22:49:07.046  INFO 5401 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationDisposableAutoCreatedBeans' of type [org.springframework.integration.config.annotation.Disposables] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-02 22:49:07.058  INFO 5401 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.integration.config.IntegrationManagementConfiguration' of type [org.springframework.integration.config.IntegrationManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-02 22:49:07.063  INFO 5401 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration$IntegrationJmxConfiguration' of type [org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration$IntegrationJmxConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-02 22:49:07.070  INFO 5401 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration' of type [org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-02 22:49:07.074  INFO 5401 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'mbeanServer' of type [com.sun.jmx.mbeanserver.JmxMBeanServer] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-02 22:49:07.090  INFO 5401 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$43c3286c] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-02 22:49:07.594  WARN 5401 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2019-11-02 22:49:07.594  INFO 5401 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2019-11-02 22:49:07.601  INFO 5401 --- [           main] c.netflix.config.DynamicPropertyFactory  : DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@83b0d0f
2019-11-02 22:49:07.752  INFO 5401 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
2019-11-02 22:49:07.946  INFO 5401 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
====> null
2019-11-02 22:49:08.105  WARN 5401 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2019-11-02 22:49:08.105  INFO 5401 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2019-11-02 22:49:08.287  INFO 5401 --- [           main] c.f.c.c.BeanFactoryAwareFunctionRegistry : Looking up function 'null' with acceptedOutputTypes: []
2019-11-02 22:49:08.474  WARN 5401 --- [           main] ockingLoadBalancerClientRibbonWarnLogger : You already have RibbonLoadBalancerClient on your classpath. It will be used by default. As Spring Cloud Ribbon is in maintenance mode. We recommend switching to BlockingLoadBalancerClient instead. In order to use it, set the value of `spring.cloud.loadbalancer.ribbon.enabled` to `false` or remove spring-cloud-starter-netflix-ribbon from your project.
2019-11-02 22:49:08.507  WARN 5401 --- [           main] eactorLoadBalancerClientRibbonWarnLogger : You have RibbonLoadBalancerClient on your classpath. LoadBalancerExchangeFilterFunction that uses it under the hood will be used by default. Spring Cloud Ribbon is now in maintenance mode, so we suggest switching to ReactorLoadBalancerExchangeFilterFunction instead. In order to use it, set the value of `spring.cloud.loadbalancer.ribbon.enabled` to `false` or remove spring-cloud-starter-netflix-ribbon from your project.
2019-11-02 22:49:08.555  INFO 5401 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'configWatchTaskScheduler'
2019-11-02 22:49:08.565  INFO 5401 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'catalogWatchTaskScheduler'
2019-11-02 22:49:08.668  INFO 5401 --- [           main] o.s.c.s.m.DirectWithAttributesChannel    : Channel 'myconsul-1.springCloudBusInput' has 1 subscriber(s).
2019-11-02 22:49:08.777  INFO 5401 --- [           main] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageChannel springCloudBusInput
2019-11-02 22:49:08.860  INFO 5401 --- [           main] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageChannel errorChannel
2019-11-02 22:49:08.896  INFO 5401 --- [           main] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageChannel nullChannel
2019-11-02 22:49:08.910  INFO 5401 --- [           main] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageChannel springCloudBusOutput
2019-11-02 22:49:08.925  INFO 5401 --- [           main] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageHandler org.springframework.cloud.stream.binding.StreamListenerMessageHandler@7497a554
2019-11-02 22:49:08.973  INFO 5401 --- [           main] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageHandler errorLogger
2019-11-02 22:49:09.058  INFO 5401 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
2019-11-02 22:49:09.058  INFO 5401 --- [           main] o.s.i.channel.PublishSubscribeChannel    : Channel 'myconsul-1.errorChannel' has 1 subscriber(s).
2019-11-02 22:49:09.058  INFO 5401 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started bean '_org.springframework.integration.errorLogger'
2019-11-02 22:49:09.121  INFO 5401 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='consul', propertySources=[ConsulPropertySource {name='config/myconsul,default/'}, ConsulPropertySource {name='config/myconsul/'}, ConsulPropertySource {name='config/application,default/'}, ConsulPropertySource {name='config/application/'}]}
2019-11-02 22:49:09.157  INFO 5401 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {message-handler:outbound.springCloudBus} as a subscriber to the 'springCloudBusOutput' channel
2019-11-02 22:49:09.157  INFO 5401 --- [           main] o.s.c.s.m.DirectWithAttributesChannel    : Channel 'myconsul-1.springCloudBusOutput' has 1 subscriber(s).
2019-11-02 22:49:09.157  INFO 5401 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started bean 'outbound.springCloudBus'
2019-11-02 22:49:09.170  INFO 5401 --- [           main] o.s.c.c.b.ConsulInboundMessageProducer   : started org.springframework.cloud.consul.binder.ConsulInboundMessageProducer@878feb2
2019-11-02 22:49:09.339  INFO 5401 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 8080
2019-11-02 22:49:09.375  INFO 5401 --- [           main] o.s.c.c.s.ConsulServiceRegistry          : Registering service with consul: NewService{id='myconsul-9d80f0a272c1126ed0ec586ee888be1f', name='myconsul', tags=[secure=false], address='192.168.0.11', meta=null, port=8080, enableTagOverride=null, check=Check{script='null', interval='30s', ttl='null', http='http://192.168.0.11:8080/', method='null', header={}, tcp='null', timeout='null', deregisterCriticalServiceAfter='null', tlsSkipVerify=null, status='null'}, checks=null}
2019-11-02 22:49:09.404  INFO 5401 --- [           main] c.e.myconsul.MyConsulApplicationKt       : Started MyConsulApplicationKt in 6.34 seconds (JVM running for 7.778)

共有2个答案

班安平
2023-03-14

因此,我通过创建一个java项目并使其工作,解决了这个问题。接下来,基于我的Java项目,我创建了一个反应式的Kotlin版本。这两个项目都能正常工作,并且是独立的。我希望他们能帮助别人。以下是项目的链接:

javaconcur键值示例

Kotlin领事键值示例

冯新知
2023-03-14

AppProperties类不是Spring管理的bean。您需要添加@EnableConfigurationProperties(AppProperties::class)到Spring管理bean:

@SpringBootApplication(scanBasePackages = ["com.example.myconsul"])
@EnableConfigurationProperties(AppProperties::class)
class MyConsulApplication

之后,您的属性类将由Spring上下文管理。

如果您希望它是可变的,并且可以通过@Reresscope刷新,那么您需要删除@ConstructorBinding并重写您的AppProperties,如下所示:

@ConstructorBinding
@RefreshScope
@ConfigurationProperties("myconsul")
class AppProperties {
    lateinit var message: String

    @PostConstruct
    fun postConstruct() {
        println("====> $message")
    }
}
 类似资料:
  • 我想通过使用@ConfigurationProperties注释将我的Application.Properties自动绑定到一个类中。首先,我尝试了@value注释,并能够将属性值注入类变量。但是,@ConfigurationProperties没有将属性注入到值中。 我的应用程序.属性: application.java ConfigBinder.java 输出: 这样的执行到底出了什么问题?

  • 如何在Spring Boot中将属性绑定到POJO? POJO类是第三方库类-我不能将@ConfigurationProperties放在那里 我知道一些实用程序类,如DataBinder,可以帮助,但可能有一个更快的方法。提前谢谢你。

  • 我试过各种组合。如果我将注释更改为,我不会得到错误,但为空。 救命啊!!

  • 问题内容: 在Python中,有没有办法绑定未绑定的方法而不调用它? 我正在编写一个程序,对于某个类,我决定将所有按钮的数据分组为类级别的元组列表是一件好事,如下所示: 问题是,因为所有的值r都是未绑定方法,所以我的程序爆炸得很厉害,我哭了。 我正在网上寻找解决方案,该方案应该是一个相对直接,可解决的问题。不幸的是我找不到任何东西。现在,我正在解决此问题,但是没有人知道是否存在一种干净,健康,Py

  • 问题内容: 我已经做了适当的命名空间声明,但依然面临着错误 有一个类似的问题在这里,但我力得到答案 这是我的xml文档,是我的名称空间不正确吗? 问题答案: 将名称空间声明添加到应用程序上下文文件中的标记定义中

  • 我有一个名为LibraryConfig的带有注释的配置属性类。它使用内部类作为属性/配置结构的类型定义。当类是内部类而不是独立类时,我得到“元素[…]“未绑定”错误/异常。为什么会这样?我如何修复它? application.yml 库配置。Java语言