2013-03-01 37 views
0

我最喜歡的一個Spring特性是如何處理從文件加載的屬性。你只需要建立一個bean像下面以類似Spring的方式處理Seam的屬性

<bean id="propertyConfigurer" 
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location" value="/WEB-INF/app.properties" /> 
</bean> 

一個現在,您可以通過使用XML(見下文)或註釋以將來自app.properties加載到你的bean的屬性值。

<bean class="com.example.PersonDaoImpl"> 
    <property name="maxResults" value="${results.max}"/> 
</bean> 

results.max是屬性之一。

我對此功能非常感興趣,因爲它使我能夠創建相當靈活的應用程序,只需更改一個屬性即可打開/關閉某些功能 - 無需重新部署應用程序。

現在我正在與JBoss Seam合作,我一直在努力尋找一種方法來使用這個框架來做類似的事情。

有誰知道該怎麼做?如果沒有,有沒有人有任何想法,我可以如何使用Seam以一種很好的方式處理屬性(我已經看到了一些方法 - 但沒有一個足夠好)。

謝謝。

回答

0

如果沒有合適的方式使用您的軟件堆棧(是否真的沒有依賴注入!?)。我會說:使用Google Guice(https://code.google.com/p/google-guice/,https://code.google.com/p/google-guice/wiki/Motivation?tm=6)!

吉斯的壞事:你可能需要閱讀很多東西來了解它是如何工作的,以及你可以用它做什麼。但是它的運行後,你將簡單地注入你的Properties對象在你需要它:

class YourClass { 
    @Inject Properties myProperties; 

    @Inject 
    public YourClass() { ... } 

    public void someMethod() { 
    use the property 
    } 
} 

,或者如果您需要在構造函數中的屬性,你也可以這樣做:

class YourClass { 
    final Properties myProperties; 

    @Inject 
    public YourClass(Properties myProperties) { 
    this.myProperties = myProperties; 
    } 

    public void someMethod() { 
    use the property 
    } 
} 

使用吉斯也許強制你重構你的整個應用程序。

但是,如果你已經有一些DI框架,你應該簡單地使用它:)