2012-09-24 120 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(); 
    } 

    //... 
} 

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

相關問題