【发布时间】:2019-08-27 14:55:52
【问题描述】:
我想将我的配置属性从 YAML 文件传递给注释值,如下所示:@SendTo(value = "${config.ws.topic}"),但出现错误
无法解析占位符 config.ws.topic 等 ..
我的代码:
@MessageMapping("/chat.register")
@SendTo("${config.websocket.topic}")
public Message addUser(@Payload Message message,
SimpMessageHeaderAccessor headerAccessor) {
headerAccessor.getSessionAttributes().put("username", message.getSender());
return message;
}
属性文件:
server:
address: 127.0.0.1
port: 8080
config:
websocket:
endpoint: /ns/ws/endpoint
appPrefix: /ns/ws
topic: /ns/ws/ns-topic
道具配置类:
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;
}
}
您能否告诉我如何将配置属性传递给注释@SendTo?
【问题讨论】:
-
你到底想做什么?您是否正在尝试将 application.yml 中的值映射到您的 props cofig 类?
-
@Coder 我只想将值从 application.yml 设置到我的控制器类,即注释。我想将应用程序 yaml (namly config.websocket.topic) 中的值映射到我的控制器类中的注释 SendTo。我想这样做是因为当我在属性配置文件中更改主题的名称时,它也在控制器类中发生了更改。
标签: java spring-boot annotations