2015-06-24 43 views
1

比方說,我有我的主類這樣的:重複調用Destroy和New Context是否很糟糕?

public class TestTasker 
{ 
    private static GenericXmlApplicationContext context; // to be accessed by static main 

    public void runTask() 
    { 
     System.out.println("Current time is: " + new Date()); 

     context.destroy(); 
     initContext(); 

     // if I do this instead.. 
     // context.refresh(); 
     // I'd get an exception: 
     // SEVERE: Invocation of method 'runTask' on target class [class spring.task.test.TestTasker] failed 
     // java.lang.IllegalStateException: GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once 

     // EDIT2: by using ClassPathXmlApplicationContext instead of generic context, 
     // I can call context.refresh() with no error. 
    } 

    private static initContext() 
    { 
     context = new GenericXmlApplicationContext("AppContext.xml"); 
    } 

    public static void main(String[] args) 
    { 
     initContext(); 
    } 
} 

AppContext.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" 
    xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd 
     http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> 

    <!-- Task Scheduler --> 
    <task:annotation-driven /> 
    <bean id="testTasker" class="spring.task.test.TestTasker" /> 
    <util:properties id="applicationProps" location="application.properties" /> 
    <context:property-placeholder 
     properties-ref="applicationProps" /> 
    <task:scheduled-tasks> 
     <task:scheduled ref="testTasker" method="runTask" 
      cron="#{applicationProps['cron.expression']}" /> 
    </task:scheduled-tasks> 

</beans> 

application.properties

cron.expression=*/5 * * * * ? 

威爾,會有負面的負面影響等等,如內存泄漏,如果我這樣做(重複調用destroy和new context)?

有沒有更好的解決辦法?我只需要Spring來刷新 AppContext自動/定期..

編輯:編輯主類的代碼。調用context.refresh()會給這個例外,

SEVERE: Invocation of method 'runTask' on target class [class spring.task.test.TestTasker] failed java.lang.IllegalStateException: GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once

EDIT2:使用ClassPathXmlApplicationContext,而不是一般的方面,我可以調用雖然context.refresh(),沒有錯誤。

+0

爲什麼不直接調用refresh()然後在上下文中,如果你想這樣做? – dunni

+0

我不認爲這可能會導致內存泄漏或任何不好的情況,但這樣做很不常見。通常你爲整個應用程序創建一個應用程序上下文。也許如果你分享用例,我們可以找到更好的解決方案。 –

+0

@dunni我無法調用上下文。刷新(),因爲它給出了一個異常: SEVERE:對目標類[class spring.task.test.TestTasker]調用方法'runTask'失敗 java.lang.IllegalStateException:GenericApplicationContext不支持多次刷新嘗試:只需調用'刷新'一次 –

回答

0

GenericXmlApplicationContext.refresh()的問題是,它不支持多重刷新。你能分享一下你的更新代碼嗎?看起來它在初始化後不止一次被調用。

這裏是春天文檔讀取

與此相反,對於每個刷新創建一個新的內部的BeanFactory實例等ApplicationContext實現,這種情況下的內部Bean工廠從一開始就使用權,可以註冊豆關於它的定義。 AbstractApplicationContext.refresh()只能被調用一次。

對於XML bean定義的典型情況,只需使用ClassPathXmlApplicationContext或FileSystemXmlApplicationContext,它們更容易設置 - 但靈活性較差,因爲您可以爲XML bean定義使用標準資源位置,而不是混合任意bean定義格式。 Web環境中的等價物是XmlWebApplicationContext。

對於應該以可刷新方式讀取特殊bean定義格式的自定義應用程序上下文實現,請考慮從AbstractRefreshableApplicationContext基類派生。

+0

嗨,我的意思是通過調用'main'實際上只是調用'context = new context' 但我懶得甚至複製粘貼:p我會修復代碼,這樣就不會有混亂。 另外,我使用'static'因爲'static main'只能訪問靜態變量。 –

+0

問題是關於銷燬和創建上下文是個好主意。這不是關於主要和靜態字段的使用。請重新考慮編輯您的答案。 *這裏寫的代碼*可能不是實際的代碼,因此可以忽略。 – NewUser

+0

@Bhupi感謝提示,通過使用'ClassPathXmlApplicationContext'而不是通用的上下文,我可以調用'context.refresh()'而不會出錯。再次感謝。爲了回答關於代碼的問題,上面的代碼真的是整個項目。我創建了一個小項目來測試Spring Task Scheduler。我幾天前剛剛開始學習Spring:p –

1

你可以使用context.refresh();

它應該銷燬已經創建的單身人士,如果它失敗,以避免搖晃資源。換句話說,在調用該方法之後,應該實例化全部或者全部的單例。

+0

嗨,如果我調用context.refresh()我得到一個異常: 'SEVERE:在目標類[類pring.task.test.TestTasker]上調用方法'runTask'失敗 java.lang.IllegalStateException:GenericApplicationContext做不支持多次刷新嘗試:只需調用'刷新'一次' –

相關問題