我可以使用 @EventListener 读取 Micronauts @Singleton class 中的应用程序属性吗?
Can I read application properties in a Micronauts @Singleton class with @EventListener?
我有一个 @Singleton
class,在我的 Micronaut 项目中有一个用 @EventListener
注释的方法:
@Singleton
class Bot {
@Inject
private val environment: Environment? = null
@EventListener
fun initialize(event: ServerStartupEvent) {
val key = environment?.getProperty("discord.api.key", String::class.java)?.get().orEmpty()
// code that is using the key variable
}
}
密钥在application.yml
文件中:
discord:
api:
key: MY_API_KEY // key is here as it is, without " or ' quotes, tried to enclose it with quotes - it doesn't work either
但是当我尝试 运行 应用程序时,出现错误:
java.lang.IllegalArgumentException: Invalid token, make sure you're using the token from the developer portal Bot section and not the application client secret or public key.
at discord4j.common.util.TokenUtil.getSelfId(TokenUtil.java:35)
at discord4j.rest.RestResources.<init>(RestResources.java:58)
at discord4j.core.CoreResources.<init>(CoreResources.java:44)
at discord4j.core.DiscordClientBuilder.lambda$create[=13=](DiscordClientBuilder.java:45)
at discord4j.rest.RestClientBuilder.build(RestClientBuilder.java:249)
at discord4j.core.DiscordClientBuilder.build(DiscordClientBuilder.java:85)
at discord4j.core.DiscordClientBuilder.build(DiscordClientBuilder.java:73)
at com.codenamite.majorbot.MajorBot.initialize(MajorBot.kt:23)
at com.codenamite.majorbot.$MajorBotDefinition$$exec1.invokeInternal(Unknown Source)
at io.micronaut.context.AbstractExecutableMethod.invoke(AbstractExecutableMethod.java:151)
at io.micronaut.context.DefaultBeanContext$BeanExecutionHandle.invoke(DefaultBeanContext.java:3414)
at io.micronaut.aop.chain.AdapterIntroduction.intercept(AdapterIntroduction.java:81)
at io.micronaut.aop.chain.MethodInterceptorChain.proceed(MethodInterceptorChain.java:96)
at com.codenamite.majorbot.MajorBot$ApplicationEventListener$initialize1$Intercepted.onApplicationEvent(Unknown Source)
at io.micronaut.context.DefaultBeanContext.notifyEventListeners(DefaultBeanContext.java:1326)
at io.micronaut.context.DefaultBeanContext.publishEvent(DefaultBeanContext.java:1311)
at io.micronaut.http.server.netty.NettyHttpServer.fireStartupEvents(NettyHttpServer.java:512)
at io.micronaut.http.server.netty.NettyHttpServer.start(NettyHttpServer.java:355)
at io.micronaut.http.server.netty.NettyHttpServer.start(NettyHttpServer.java:114)
at io.micronaut.runtime.Micronaut.lambda$start(Micronaut.java:77)
at java.base/java.util.Optional.ifPresent(Optional.java:183)
at io.micronaut.runtime.Micronaut.start(Micronaut.java:75)
at com.codenamite.ApplicationKt.main(Application.kt:8)
Caused by: java.lang.NumberFormatException: For input string: ""
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Long.parseLong(Long.java:702)
at java.base/java.lang.Long.parseLong(Long.java:817)
at discord4j.common.util.TokenUtil.getSelfId(TokenUtil.java:32)
... 22 common frames omitted
Caused by: java.lang.NumberFormatException: For input string: ""
部分告诉我密钥为空,因此使用了 .orEmpty()
部分。
我不知道发生了什么,我想到的唯一一个想法是当这段代码 运行ning 时属性不可用,但我不知道如何解决这个问题......或者也许还有其他事情?我运行没主意了...
我建议使用任一配置注入:https://docs.micronaut.io/latest/guide/index.html#valueAnnotation
@Context
class Bot(@Value("${discord.api.key}") String key) {
init {
// code that is using the key variable
}
}
或者我更喜欢的配置属性
https://docs.micronaut.io/latest/guide/index.html#configurationProperties
你的机器人看起来像这样:
@ConfigurationProperties("discord")
class DiscordConfig {
@NotBlank
var token: String = ""
}
@Context
class Bot(config: DiscordConfig) {
init {
val client = DiscordClient.create(config.token)
// code that is using the key variable
}
}
我认为没有必要使用服务器启动事件。
如果您用“上下文”注释您的“单例”,则该 bean 在启动时实例化,这与服务器启动事件完全相同。
使用构造函数注入可以确保这些东西在实例化时就在那里。
奥利
我有一个 @Singleton
class,在我的 Micronaut 项目中有一个用 @EventListener
注释的方法:
@Singleton
class Bot {
@Inject
private val environment: Environment? = null
@EventListener
fun initialize(event: ServerStartupEvent) {
val key = environment?.getProperty("discord.api.key", String::class.java)?.get().orEmpty()
// code that is using the key variable
}
}
密钥在application.yml
文件中:
discord:
api:
key: MY_API_KEY // key is here as it is, without " or ' quotes, tried to enclose it with quotes - it doesn't work either
但是当我尝试 运行 应用程序时,出现错误:
java.lang.IllegalArgumentException: Invalid token, make sure you're using the token from the developer portal Bot section and not the application client secret or public key.
at discord4j.common.util.TokenUtil.getSelfId(TokenUtil.java:35)
at discord4j.rest.RestResources.<init>(RestResources.java:58)
at discord4j.core.CoreResources.<init>(CoreResources.java:44)
at discord4j.core.DiscordClientBuilder.lambda$create[=13=](DiscordClientBuilder.java:45)
at discord4j.rest.RestClientBuilder.build(RestClientBuilder.java:249)
at discord4j.core.DiscordClientBuilder.build(DiscordClientBuilder.java:85)
at discord4j.core.DiscordClientBuilder.build(DiscordClientBuilder.java:73)
at com.codenamite.majorbot.MajorBot.initialize(MajorBot.kt:23)
at com.codenamite.majorbot.$MajorBotDefinition$$exec1.invokeInternal(Unknown Source)
at io.micronaut.context.AbstractExecutableMethod.invoke(AbstractExecutableMethod.java:151)
at io.micronaut.context.DefaultBeanContext$BeanExecutionHandle.invoke(DefaultBeanContext.java:3414)
at io.micronaut.aop.chain.AdapterIntroduction.intercept(AdapterIntroduction.java:81)
at io.micronaut.aop.chain.MethodInterceptorChain.proceed(MethodInterceptorChain.java:96)
at com.codenamite.majorbot.MajorBot$ApplicationEventListener$initialize1$Intercepted.onApplicationEvent(Unknown Source)
at io.micronaut.context.DefaultBeanContext.notifyEventListeners(DefaultBeanContext.java:1326)
at io.micronaut.context.DefaultBeanContext.publishEvent(DefaultBeanContext.java:1311)
at io.micronaut.http.server.netty.NettyHttpServer.fireStartupEvents(NettyHttpServer.java:512)
at io.micronaut.http.server.netty.NettyHttpServer.start(NettyHttpServer.java:355)
at io.micronaut.http.server.netty.NettyHttpServer.start(NettyHttpServer.java:114)
at io.micronaut.runtime.Micronaut.lambda$start(Micronaut.java:77)
at java.base/java.util.Optional.ifPresent(Optional.java:183)
at io.micronaut.runtime.Micronaut.start(Micronaut.java:75)
at com.codenamite.ApplicationKt.main(Application.kt:8)
Caused by: java.lang.NumberFormatException: For input string: ""
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Long.parseLong(Long.java:702)
at java.base/java.lang.Long.parseLong(Long.java:817)
at discord4j.common.util.TokenUtil.getSelfId(TokenUtil.java:32)
... 22 common frames omitted
Caused by: java.lang.NumberFormatException: For input string: ""
部分告诉我密钥为空,因此使用了 .orEmpty()
部分。
我不知道发生了什么,我想到的唯一一个想法是当这段代码 运行ning 时属性不可用,但我不知道如何解决这个问题......或者也许还有其他事情?我运行没主意了...
我建议使用任一配置注入:https://docs.micronaut.io/latest/guide/index.html#valueAnnotation
@Context
class Bot(@Value("${discord.api.key}") String key) {
init {
// code that is using the key variable
}
}
或者我更喜欢的配置属性 https://docs.micronaut.io/latest/guide/index.html#configurationProperties
你的机器人看起来像这样:
@ConfigurationProperties("discord")
class DiscordConfig {
@NotBlank
var token: String = ""
}
@Context
class Bot(config: DiscordConfig) {
init {
val client = DiscordClient.create(config.token)
// code that is using the key variable
}
}
我认为没有必要使用服务器启动事件。 如果您用“上下文”注释您的“单例”,则该 bean 在启动时实例化,这与服务器启动事件完全相同。 使用构造函数注入可以确保这些东西在实例化时就在那里。
奥利