JaC*_*hNo 6 java jmstemplate spring-jms spring-boot
我希望能够从application.properties设置@JMSlistener目标
我的代码看起来像这样
@Service
public class ListenerService {
private Logger log = Logger.getLogger(ListenerService.class);
@Autowired
QueueProperties queueProperties;
public ListenerService(QueueProperties queueProperties) {
this.queueProperties = queueProperties;
}
@JmsListener(destination = queueProperties.getQueueName() )
public void listenQueue(String requestJSON) throws JMSException {
log.info("Received " + requestJSON);
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我建造时
Error:(25, 60) java: element value must be a constant expression
Run Code Online (Sandbox Code Playgroud)
小智 8
使用属性占位符要容易得多。
@JmsListener(destination = "${mq.queue}")
public void onMessage(Message data) {
}
Run Code Online (Sandbox Code Playgroud)
您不能在当前bean中引用字段,但是可以使用SpEL表达式在应用程序上下文中引用另一个bean ...
@SpringBootApplication
public class So49368515Application {
public static void main(String[] args) {
SpringApplication.run(So49368515Application.class, args);
}
@Bean
public ApplicationRunner runner(JmsTemplate template, Foo foo) {
return args -> template.convertAndSend(foo.getDestination(), "test");
}
@JmsListener(destination = "#{@foo.destination}")
public void listen(Message in) {
System.out.println(in);
}
@Bean
public Foo foo() {
return new Foo();
}
public class Foo {
public String getDestination() {
return "foo";
}
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以使用属性占位符${...}。
| 归档时间: |
|
| 查看次数: |
2693 次 |
| 最近记录: |