Java Session 类中的 getDefaultInstance() 和 getInstance() 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4184204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:26:34  来源:igfitidea点击:

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

javasessionjavamail

提问by Gnanam

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

Session.getDefaultInstance(props, authenticator)和 和有getInstance(props, authenticator)什么区别?一般来说,你什么时候会选择一个?

I also read Java doc on getDefaultInstance(props, authenticator), but still couldn't able to make out the difference distinctly/clearly.

我还阅读了关于getDefaultInstance(props, authenticationator) 的Java 文档,但仍然无法清楚地/清楚地分辨出差异。

Hope experts can help me in understanding this better.

希望专家能帮助我更好地理解这一点。

UPDATE:Actual reason that triggered to ask this question is: We've used Session.getDefaultInstance()method in some places within our web-based application. Sometimes, it throws java.lang.SecurityException: Access to default session denied, on quick googling, it suggested to use Session.getInstance()method instead. Hence, when one would choose one over the other?

更新:触发提出这个问题的实际原因是:我们在Session.getDefaultInstance()基于 Web 的应用程序中的某些地方使用了方法。有时,它会抛出java.lang.SecurityException: Access to default session denied,在快速谷歌搜索时,它建议改用Session.getInstance()方法。因此,什么时候一个人会选择一个而不是另一个?

采纳答案by Codemwnci

If you read the documentation, you will see that

如果你阅读文档,你会看到

getDefaultInstanceGet the default Session object. If a default has not yet been setup, a new Session object is created and installed as the default.

getDefaultInstance获取默认的 Session 对象。如果尚未设置默认值,则会创建一个新的 Session 对象并将其安装为默认值。

Therefore, if one does not already exist, it call getInstance()

因此,如果一个不存在,它会调用 getInstance()

getInstanceGet a new Session object.

getInstance获取一个新的 Session 对象。

So, a new session object is created, regardless of whether one already exists.

因此,会创建一个新的会话对象,而不管它是否已经存在。

回答by Manikanta Vasu

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

回答by Vova Perebykivskyi

For me, it was very important to use getInstance()instead of getDefaultInstance().

对我来说,使用getInstance()而不是getDefaultInstance().

Because after mail session properties was changed, mail session still was storing old properties.

因为在邮件会话属性更改后,邮件会话仍然存储旧属性。

So getDefaultInstance()- it is looks like Singleton.

所以getDefaultInstance()- 它看起来像单身人士。

As docs said:

正如文档所说:

Note also that the Properties object is used only the first time this method is called, when a new Session object is created. Subsequent calls return the Session object that was created by the first call, and ignore the passed Properties object. Use the getInstance method to get a new Session object every time the method is called.

另请注意,Properties 对象仅在第一次调用此方法时使用,即创建新的 Session 对象时。后续调用返回由第一次调用创建的 Session 对象,并忽略传递的 Properties 对象。每次调用该方法时,使用 getInstance 方法获取一个新的 Session 对象。

回答by Yasin Okumu?

FAQ says: https://javaee.github.io/javamail/FAQ#getdefaultinstance

常见问题说:https: //javaee.github.io/javamail/FAQ#getdefaultinstance

Q: When should I use Session.getDefaultInstanceand when should I use Session.getInstance?

A: Almost all code should use Session.getInstance. The Session.getDefaultInstancemethod creates a new Session the first time it's called, using the Properties that are passed. Subsequent calls will return that original Session and ignore any Properties you pass in. If you want to create different Sessions with different properties, Session.getDefaultInstance won't do that. If some other code in the same JVM (e.g., in the same app server) has already created the default Session with their properties, you may end up using their Session and your properties will be ignored. This often explains why your property settings seem to be ignored. Always use Session.getInstanceto avoid this problem.

问:什么时候用Session.getDefaultInstance,什么时候用Session.getInstance

A:几乎所有的代码都应该使用Session.getInstance. 该 Session.getDefaultInstance方法在第一次被调用时使用传递的属性创建一个新会话。后续调用将返回原始会话并忽略您传入的任何属性。如果您想创建具有不同属性的不同会话,Session.getDefaultInstance 不会这样做。如果同一 JVM 中的一些其他代码(例如,在同一应用服务器中)已经使用它们的属性创建了默认 Session,您可能最终会使用它们的 Session 并且您的属性将被忽略。这通常解释了为什么您的属性设置似乎被忽略了。始终使用 Session.getInstance以避免此问题。