6

我正在使用以下注释:

@ActivationConfigProperty(
    propertyName = "connectionParameters", 
    propertyValue = "host=127.0.0.1;port=5445,host=127.0.0.1;port=6600"),
public class TestMDB implements MessageDrivenBean, MessageListener

我想提取这些 IP 地址和端口中的每一个并将它们存储在一个文件中jmsendpoints.properties......然后动态加载它们。像这样的东西:

@ActivationConfigProperty(
    propertyName = "connectionParameters", 
    propertyValue = jmsEndpointsProperties.getConnectionParameters()),
public class TestMDB implements MessageDrivenBean, MessageListener

有没有办法做到这一点?

4

2 回答 2

13

,注释处理器(您正在使用的基于注释的框架)需要实现一种处理占位符的方法。


例如,类似的技术在Spring

@Value("#{systemProperties.dbName}")

这里Spring实现了一种解析特定语法的方法,在这种情况下,它转换为类似于System.getProperty("dbName");

于 2012-09-24T15:45:04.893 回答
1

注释并非设计为在运行时可修改,但您可以利用字节码工程库(例如ASM)来动态编辑注释值。

相反,我建议创建一个可以修改这些值的界面。

public interface Configurable {
    public String getConnectionParameters();
}

public class TestMDB implements MessageDrivenBean, MessageListener, Configurable {

    public String getConnectionParameters() {
        return jmsEndpointsProperties.getConnectionParameters();
    }

    //...
}

您可能希望创建一个更面向键值的接口,但这是它的一般概念。

于 2012-09-24T15:49:17.290 回答