What is the difference between getDefaultInstance() and getInstance() in Session class?

前端 未结 4 2030
情话喂你
情话喂你 2021-02-04 03:57

What is the difference between Session.getDefaultInstance(props, authenticator) and getInstance(props, authenticator)? In general, when will you choos

4条回答
  •  日久生厌
    2021-02-04 04:48

    Cause This error is raised in the getDefaultInstance method in javax.mail.Session.java. According to this source code, this error occures when the default session object is already initialized, but authenticator instance is renewed or changed, or the class loader of the default session object is different from the argument authentificator's. Maybe the java source code using the default session instance of the java mail is recompiled and reloaded, or duplicate javamail class libraries are included into the Classpath of the environment. it gives proper solution

    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;
       }
    

提交回复
热议问题