2012-09-24 232 views
3

我使用下面的注释:将动态参数传递给注释?

@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 

有没有办法做到这一点?

+1

你可以有'propertyValue =“jmsendpoints.properties ConnectionParameter”'你动态解析。 –

回答

9

编号注解处理器(您使用的基于注解的框架)需要实现处理占位符的方法。


作为一个例子,类似的技术在Spring

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

这里Spring被实现实现到的方法解析该特定语法,在这种情况下转化为类似的东西,以System.getProperty("dbName");

1

注释的设计不是在运行时可修改的,但是您可能能够使用字节码工程库,例如ASM为了动态地编辑注解值。

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

public interface Configurable { 
    public String getConnectionParameters(); 
} 

public class TestMDB implements MessageDrivenBean, MessageListener, Configurable { 

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

    //... 
} 

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

相关问题