I'm sure I'm expecting too much when using the JNDITemplate in Spring Boot, but when I try to bind an object to the JNDI context I get an exception.
@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);
};
}
The exception is:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
The template does not have a JNDI context. But the question is, how is the context to be provided?
Background I want to configure the javax.mail session for a logback smtp appender. The smtp appender is able to use the mail session from JNDI. See logback SMTPAppender the sessionViaJNDI property.
The exception is because you haven't specified an initial context factory.
Please take a look at some of the providers listed here https://docs.oracle.com/cd/A97688_16/generic.903/a97690/jndi.htm#1005621.
Spring has a JndiRmiServiceExporter
that makes RMI easy, but I think the choice depends on what exactly you're trying to accomplish.