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

前端 未结 4 2074
情话喂你
情话喂你 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:52

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

    Q: When should I use Session.getDefaultInstance and when should I use Session.getInstance?

    A: Almost all code should use Session.getInstance. The Session.getDefaultInstance method 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.getInstance to avoid this problem.

提交回复
热议问题