简体   繁体   English

使用 Spring Boot,如何在 @JmsListener 注释中注入 JMS 目标?

[英]Using Spring Boot, how can I inject a JMS destination in @JmsListener annotation?

Using Spring Boot, how can I inject my JMS topic from a config property?使用 Spring Boot,如何从配置属性中注入我的 JMS 主题? I want to define the topic/queue name in application.properties to be injected at runtime.我想在application.properties中定义要在运行时注入的主题/队列名称。

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) {
        ...
    }
}

Try this:尝试这个:

@JmsListener(destination = "${my-topic}")

@JmsListener(destination = "\${config.property.key}") , This should take the topic/queue name from configs. @JmsListener(destination = "\${config.property.key}") ,这应该从配置中获取主题/队列名称。

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

相关问题 如何在 Spring boot 中向 JMSListener Annotation 动态添加不同的目的地? - How to add different destination dynamically to JMSListener Annotation in Spring boot? 如何让 Spring JMS 从注解@JmsListener 中选择目标队列名称 - How to make Spring JMS pick destination queue name from annotation @JmsListener 如何在@JmsListener使用的Spring Boot中访问活动的JMS连接/会话 - How to acces to the active JMS Connection/Session in Spring Boot that @JmsListener uses 在 Spring Boot 上即时修改 @JMSListener 目标 - Modify @JMSListener destination on-the-fly on Spring Boot Spring Boot @JmsListener 拦截器 - Spring Boot @JmsListener Interceptor 如何在Spring Boot中将配置属性注入Spring Retry注释? - How to inject config properties in Spring Boot to Spring Retry annotation? spring 引导如何使用 @Autowired 没有 @Component 注释和 xml 配置注入 ApplicationContext 和 JdbcTemplate 的实例? - How does spring boot inject the instance of ApplicationContext and JdbcTemplate using @Autowired without @Component annotation and xml configuration? 如何从 Spring Boot 2 中的库中注入依赖项? - How can I inject dependency from a library in Spring Boot 2? 如何使用@Value Spring 注释注入 Map? - How to inject a Map using the @Value Spring Annotation? 如何在 Spring Boot 的查询注释中使用我的参数? - How can I use my parameter in query annotation in Spring Boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM