2012-05-25 37 views
0

有人知道如何顯示服務器啓動時加載的屬性(通過某個監聽器或類似的東西)?如何顯示服務器啓動時加載的屬性

這是我有:

1-文件名爲project.properties與配置參數。

2 - ...這是由彈簧加載有:

<context:property-placeholder location="WEB-INF/project.properties" /> 

-3-酮監聽器,我想可能是閱讀聲明的屬性並記錄他們一個合適的位置。

public class StartListener implements ServletContextListener { 

    Logger logger = LoggerFactory.getLogger(this.getClass().getName()); 

    public void contextInitialized(ServletContextEvent sce) { 
     //Here read the properties and do the logging of it 
    } 
//... 
} 

任何幫助將大大升值。

回答

1

根據我的情況,你需要做的是當服務器啓動時,你想記錄一些屬性。

要實現這一目標,首先需要在dispatcher-servlet.xml文件中添加以下行。

<context:annotation-config /> 

然後用@PostConstruct註釋的任何方法的頂部,你想加載的屬性和記錄它們。您可以在應用程序的任何控制器或服務類中執行此操作。

當你這樣做的時候,這個春天會自動檢測到這個註解,並且每當它完成加載應用程序時,它就會調用這個函數並做你在那裏描述的任何事情。

希望這可以幫助你。

乾杯。

0

感謝,但最後,我要做到這一點的聽衆:

公共類StartListener實現了ServletContextListener {

Logger logger = LoggerFactory.getLogger(this.getClass().getName()); 

public void contextInitialized(ServletContextEvent sce) { 
    ServletContext sc = sce.getServletContext(); 

    Properties props = new Properties(); 
    try { 
     props.load(sc.getResourceAsStream("/WEB-INF/project.properties")); 
     logger.info(props.entrySet().toString()); 
    } catch (Exception e) { 
     logger.error("......"); 
    } 
} 

//... 

}

相關問題