我確定我在Spring Boot中使用JNDITemplate時期望過多,但是當我嘗試將對象綁定到JNDI上下文時,我得到一個異常。如何初始化Spring引導中的JndiTemplate
@Bean
CommandLineRunner initJndi() {
return (args) -> {
Properties props = new Properties();
props.put("mail.smtp.host", "mail.bla.com");
...
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
});
JndiTemplate template = new JndiTemplate();
template.bind("Session", session);
};
}
唯一的例外是:
javax.naming.NoInitialContextException:需要 環境或系統屬性指定的類名,或者作爲一個小程序參數,或在 應用程序資源文件:JAVA .naming.factory.initial
該模板沒有JNDI上下文。但問題是,如何提供上下文?
背景 我想爲SMTP的logback附加目的地配置javax.mail會議。 smtp appender能夠使用來自JNDI的郵件會話。見logback SMTPAppendersessionViaJNDI財產。
爲什麼你需要JNDI?如果只是郵件使用Spring郵件抽象,那麼你不需要它。 –