如何在Spring Boot中初始化JndiTemplate

时间:2017-07-31 15:27:50

标签: java spring spring-boot jndi

我确信在Spring Boot中使用JNDITemplate时我期望得太多,但是当我尝试将对象绑定到JNDI上下文时,我得到了一个异常。

@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:需要在中指定类名   环境或系统属性,或作为applet参数,或在   应用程序资源文件:java.naming.factory.initial

模板没有JNDI上下文。但问题是,如何提供背景?

背景 我想为logback smtp appender配置javax.mail会话。 smtp appender能够使用JNDI的邮件会话。请参阅logback SMTPAppender sessionViaJNDI 属性。

1 个答案:

答案 0 :(得分:1)

例外情况是因为您尚未指定初始上下文工厂。

请查看此处列出的一些提供商https://docs.oracle.com/cd/A97688_16/generic.903/a97690/jndi.htm#1005621

Spring有JndiRmiServiceExporter使RMI变得容易,但我认为选择取决于你究竟想要完成的事情。

相关问题