[英]How to define field value of an annotation from property file
I'm playing with spring and activemq and use the following method to receive messages from message broker: 我正在使用spring和activemq,并使用以下方法从消息代理接收消息:
@JmsListener(destination = "sample.queue")
public void receiveQueue(String text) {
System.out.println(text);
}
I just thought it would be nice to be able to configure destination
from my application.properties
. 我只是认为能够从我的
application.properties
配置destination
会很好。 Is there any way to do that ? 有什么办法吗?
Ok, I found the way. 好的,我找到了路。 Suppose
message-consumer.destination
property from application.properties
defines the desired destination, then it would be as simple as this: 假设来自
application.properties
message-consumer.destination
属性定义了所需的目的地,那么它就这么简单:
@JmsListener(destination = "${message-consumer.destination}")
public void receiveQueue(String text) {
System.out.println(text);
}
Below are my old thoughts on how to externalize queue destination: 以下是我关于如何外部化队列目标的旧想法:
Here is the message consumer. 这是消息使用者。
@Component
public class Consumer implements MessageListener {
@Override
public void onMessage(Message message) {
}
}
Here is the jms configuration: 这是jms配置:
@Configuration
@EnableJms
public class JmsConfiguration implements JmsListenerConfigurer {
@Value("${message-consumer.destination}")
private String destination;
@Inject
private MessageListener messageListener;
@Override
public void configureJmsListeners(JmsListenerEndpointRegistrar registrar) {
SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
endpoint.setId("audit.logging");
endpoint.setDestination(destination);
endpoint.setMessageListener(messageListener);
registrar.registerEndpoint(endpoint);
}
I don't think you can add a variable to this but even if you can I think a better solution is to use a DestinationResolver as this is what the destination is used for. 我认为您不能为此添加变量,但是即使您认为更好的解决方案是使用DestinationResolver,因为这是目标的用途。
There is some explanation of this in the Spring documentation . 在Spring文档中对此有一些解释。
It's possible by reading the properties file at class level: 可以通过在类级别读取属性文件来实现:
Create a file 'my.properties' in your resources dir: 在资源目录中创建文件“ my.properties”:
destination = sample.queue
And a wrapper class: 还有一个包装类:
public class MyProperties {
private static final RespourceBundle BUNDLE = RespourceBundle.getBundle("/my.properties");
public static String destination() {
return BUNDLE.getProperty("destination");
}
}
Then change your code like this: 然后像这样更改代码:
@JmsListener(destination = MyProperties.destination())
public void receiveQueue(String text) {
System.out.println(text);
}
Haven't tested it, but I think that should work. 还没有测试过,但是我认为应该可以。
EDIT: I'm sure I've used constants in annotations before. 编辑:我确定我之前在注解中使用过常量。 Maybe this static method is causing the problem.
也许此静态方法导致了问题。 Try this:
尝试这个:
public class MyProperties {
private static final RespourceBundle BUNDLE = RespourceBundle.getBundle("/my.properties");
public static final String DESTINATION = BUNDLE.getProperty("destination");
}
and: 和:
@JmsListener(destination = MyProperties.DESTINATION)
public void receiveQueue(String text) {
System.out.println(text);
}
EDIT: one last attempt 编辑:最后一次尝试
public class MyProperties {
private static final RespourceBundle BUNDLE = RespourceBundle.getBundle("/my.properties");
public static final String DESTINATION;
static {
DESTINATION = BUNDLE.getProperty("destination");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.