简体   繁体   English

如何将属性占位符传递给YAML配置文件中的注释?

[英]How can I pass properties placeholder to annotations from YAML config file?

I want to pass my config properties from YAML file to annotations value like this: @SendTo(value = "${config.ws.topic}") , but get an error 我想将配置属性从YAML文件传递给注释值,例如: @SendTo(value = "${config.ws.topic}") ,但出现错误

Could not resolve placeholder config.ws.topic etc .. 无法解析占位符config.ws.topic等。

My Code: 我的代码:

@MessageMapping("/chat.register")
@SendTo("${config.websocket.topic}")
public Message addUser(@Payload Message message,
                       SimpMessageHeaderAccessor headerAccessor) {
    headerAccessor.getSessionAttributes().put("username", message.getSender());
    return message;

}

Prop-es file: 道具文件:

server:
  address: 127.0.0.1
  port: 8080

config:
  websocket:
    endpoint: /ns/ws/endpoint
    appPrefix: /ns/ws
    topic: /ns/ws/ns-topic

Props config class: 道具配置类:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(value = "config.websocket")
public class WebSocketConfigurationProperties {
  private String endpoint;
  private String appPrefix;
  private String topic;

public String getEndpoint() {
    return endpoint;
}

public void setEndpoint(String endpoint) {
    this.endpoint = endpoint;
}

public String getAppPrefix() {
    return appPrefix;
}

public void setAppPrefix(String appPrefix) {
    this.appPrefix = appPrefix;
}

public String getTopic() {
    return topic;
}

public void setTopic(String topic) {
    this.topic = topic;
} 
}

Could you please advise me on how to pass the config properties to annotations @SendTo ? 您能建议我如何将配置属性传递给注释@SendTo吗?

If you are trying to map values from application.yml to your configuration class you can simply make use of @Value for this purpose. 如果您尝试将值从application.yml映射到配置类,则可以简单地使用@Value来实现此目的。

In your controller simply create variables which will hold the information from application.yml like as specified below 在您的控制器中,只需创建变量即可保存来自application.yml的信息,如下所示

@Value("${config.ws.topic}")
String topic;

and your controller will look as below 您的控制器将如下所示

@MessageMapping("/chat.register")
@SendTo(topic)
public Message addUser(@Payload Message message,
                       SimpMessageHeaderAccessor headerAccessor) {
    headerAccessor.getSessionAttributes().put("username", message.getSender());
    return message;

}

Edit 1: Due to the following error Attribute value must be constant , there is a work around to solve the issue. 编辑1:由于以下错误, 属性值必须恒定 ,可以解决此问题。

@Value("${config.ws.topic}")
String topic;

public static final String TOPIC_VALUE = "" + topic;

and your controller will look as below 您的控制器将如下所示

@MessageMapping("/chat.register")
@SendTo(TOPIC_VALUE)
public Message addUser(@Payload Message message,
                       SimpMessageHeaderAccessor headerAccessor) {
    headerAccessor.getSessionAttributes().put("username", message.getSender());
    return message;


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 @Scope 和 @Collection 注释中传递来自 application.properties 的值? - How can I pass a value from application.properties in @Scope and @Collection annotations? 如何使用注释从配置文件中使用变量限定autowired属性? - How can I qualify an autowired property with a variable from a config file using annotations? 如何在Spring 4中使用注释重新加载属性文件? - How can I reload properties file in Spring 4 using annotations? 如何在没有 spring 框架的情况下读取配置文件 (*.yaml *.properties) - How to read config file (*.yaml *.properties) without spring framework 如何从控制器类更改 yaml 文件中占位符的值 - How to Change value of placeholder in yaml file from Controller Class 如何从该配置文件获取属性到另一个Java类? - How can I get the properties from this config file to another java class? 从配置文件进行Yaml解析 - Yaml Parsing from a config file 我可以将参数从属性文件传递给 Android Apk 吗? - Can I pass a parameter to an Android Apk from a properties file? 无法使用注释从.properties文件中提取值 - Can't pull values from .properties file using annotations 如何使用@WithMockUser并从我的属性文件中传递我的用户名和密码? - How can I use @WithMockUser and pass my username and password from my properties file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM