使用 Spring Boot,如何在 @JmsListener 注释中注入 JMS 目标?
Using Spring Boot, how can I inject a JMS destination in @JmsListener annotation?
使用 Spring 引导,如何从配置 属性 中注入我的 JMS 主题?我想在 application.properties
中定义要在运行时注入的 topic/queue 名称。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
@Service
public class DispatcherService {
@Autowired ConfigProperties cp;
private final String dest = "my-topic";
private final String dest2 = cp.getTopic();
// WORKS
@JmsListener(destination = "my-topic")
public void receive1(String message) {
...
}
// WORKS
@JmsListener(destination = dest)
public void receive2(String message) {
...
}
// DOES NOT WORK - "Attribute must be constant"
@JmsListener(destination = dest2)
public void receive3(String message) {
...
}
// DOES NOT WORK - can I inject this here, somehow?
@JmsListener(destination = @Value("${topic}"))
public void receive4(String message) {
...
}
}
试试这个:
@JmsListener(destination = "${my-topic}")
@JmsListener(destination = "${config.property.key}")
,这应该从配置中获取 topic/queue 名称。
使用 Spring 引导,如何从配置 属性 中注入我的 JMS 主题?我想在 application.properties
中定义要在运行时注入的 topic/queue 名称。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
@Service
public class DispatcherService {
@Autowired ConfigProperties cp;
private final String dest = "my-topic";
private final String dest2 = cp.getTopic();
// WORKS
@JmsListener(destination = "my-topic")
public void receive1(String message) {
...
}
// WORKS
@JmsListener(destination = dest)
public void receive2(String message) {
...
}
// DOES NOT WORK - "Attribute must be constant"
@JmsListener(destination = dest2)
public void receive3(String message) {
...
}
// DOES NOT WORK - can I inject this here, somehow?
@JmsListener(destination = @Value("${topic}"))
public void receive4(String message) {
...
}
}
试试这个:
@JmsListener(destination = "${my-topic}")
@JmsListener(destination = "${config.property.key}")
,这应该从配置中获取 topic/queue 名称。