2010-11-15 25 views
14

Session.getDefaultInstance(props, authenticator)getInstance(props, authenticator)有什麼區別?一般來說,你什麼時候會選擇一個呢?Session類中的getDefaultInstance()和getInstance()有什麼區別?

我也閱讀了關於getDefaultInstance(props, authenticator)的Java文檔,但仍然無法清晰地區分差異。

希望專家能幫助我更好地理解這一點。

更新:引發此問題的實際原因是:我們在基於Web的應用程序的某些地方使用了Session.getDefaultInstance()方法。有時,它會拋出java.lang.SecurityException: Access to default session denied,在Google上快速提示時,建議使用Session.getInstance()方法。因此,當一個人選擇一個在另一個之上?

+1

http://download.oracle.com/javaee/6/api/javax/mail/Session.html#getInstance(java.util.Properties)此文檔可以幫助你理解差異 – 2010-11-15 12:24:39

+0

@ org.life .java:我找不到任何區別?你能指點我一些特定的句子/段嗎? – Gnanam 2010-11-15 12:59:30

+0

'getInstance()':獲取一個新的Session對象。 ,'getDefaultInstance()':獲取默認的Session對象。如果尚未設置默認值,則會創建並安裝一個新的Session對象作爲默認值。 – 2010-11-15 13:11:46

回答

16

如果你讀了文件,你會看到

getDefaultInstance 獲取默認Session對象。如果尚未設置默認值,則會創建並安裝一個新的Session對象作爲默認值。

因此,如果一個已經不存在,它調用的getInstance()

的getInstance 得到一個新的Session對象。

因此,無論是否已經存在,都會創建一個新的會話對象。

+0

問這個問題的實際原因在這裏解釋:我們在基於Web的應用程序的一些地方使用了'Session.getDefaultInstance()'方法。有時,它會拋出'java.lang.SecurityException:訪問默認會話被拒絕',在快速上網搜索時,它建議使用'Session.getInstance()'方法。這就是我在這裏問過什麼時候會選擇另一個的原因嗎? – Gnanam 2010-11-16 12:25:25

+0

我可以看到使用defaultinstance的唯一原因是它阻止了無需創建多個實例的需要,但除此之外沒有任何區別。如果您在使用權限方面遇到問題,那麼只需使用getInstance(),它不會引起您的問題。但是,您的應用程序應該將其存儲在全局級別,而不是在每次需要時創建新實例。 – Codemwnci 2010-11-16 12:40:47

+0

根據doc,'getDefaultInstance():獲取默認的Session對象。如果尚未設置默認值,則會創建一個新的Session對象並將其作爲默認值進行安裝。「對於getDefaultInstance()的後續調用是否不使用之前創建的會話? – Gnanam 2010-11-16 13:34:15

2

原因 此錯誤在javax.mail.Session.java中的getDefaultInstance方法中引發。根據此源代碼,當默認會話對象已經初始化,但驗證器實例被更新或更改,或者默認會話對象的類加載器與參數驗證器不同時,會發生此錯誤。可能使用java郵件的默認會話實例的java源代碼被重新編譯並重新加載,或者重複的javamail類庫被包含到環境的Classpath中。 它給了妥善的解決辦法

javax.mail.Session.java file 
    public static synchronized Session getDefaultInstance(Properties props, 
             Authenticator authenticator) { 
     if (defaultSession == null) 
      defaultSession = new Session(props, authenticator); 
     else { 
      // have to check whether caller is allowed to see default session 
      if (defaultSession.authenticator == authenticator) 
       ;  // either same object or both null, either way OK 
      else if (defaultSession.authenticator != null && 
        authenticator != null && 
        defaultSession.authenticator.getClass().getClassLoader() == 
         authenticator.getClass().getClassLoader()) 
       ;  // both objects came from the same class loader, OK 
      else 
       // anything else is not allowed 
       throw new SecurityException("Access to default session denied"); 
     } 

     return defaultSession; 
    } 
2

對我來說,這是使用的getInstance()代替getDefaultInstance()非常重要。

因爲郵件會話屬性發生更改後,郵件會話仍然存儲舊屬性。

所以getDefaultInstance() - 它看起來像Singleton。

由於文檔說:

還要注意屬性對象用於僅在第一次調用此方法時,將創建一個新的Session對象時。後續調用返回由第一個調用創建的Session對象,並忽略傳遞的Properties對象。每次調用方法時,使用getInstance方法獲取新的Session對象。

+0

此關鍵信息位於getDefaultInstance(Properties,Authenticator)的文檔中,而不是getDefaultInstance(Properties)的文檔中。 – ARX 2018-01-26 17:18:14

1

常見問題解答說:https://javaee.github.io/javamail/FAQ#getdefaultinstance

問:什麼時候應該使用Session.getDefaultInstance時候應該我 使用Session.getInstance

答:幾乎所有的代碼都應該使用Session.getInstanceSession.getDefaultInstance方法使用傳遞的屬性創建一個新的會話,該會話是第一個調用它的時間 。之後的 調用將返回原始會話,並忽略您傳入的任何屬性 。如果要使用不同的 屬性創建不同的會話,則Session.getDefaultInstance不會這樣做。如果同一個JVM中的其他代碼(例如,在同一個應用程序服務器中)已經使用它們的屬性創建了默認會話,則可能會使用它們的Session結束 ,並且您的屬性將被忽略。這通常是 解釋了爲什麼您的屬性設置似乎被忽略。 始終使用 Session.getInstance來避免此問題。

相關問題