Search code examples
spring-bootjndi

How to set equivalent of web.xml JNDI <env-entry> in a Spring Boot project?


Referring to this SO answer, I'd like to setup the equivalent of this web.xml configuration in a JSF / JoinFaces / SpringBoot application (that doesn't have web.xml).

<env-entry>
    <env-entry-name>jsf/ClientSideSecretKey</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>[AES key in Base64 format]</env-entry-value>
</env-entry>

Any pointers?


Solution

  • Essentially <env-entry> declares a web application context attribute.

    You can initialize your servlet context and provide the equivalent servlet context attributes in your Spring Boot application.

    For that purpose, you can register a bean that implements the ServletContextInitializer interface (or WebApplicationInitializer if your app has to be deployed in a traditional servlet container). For example:

    public class JsfServletContextInitializer implements ServletContextInitializer {
    
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
        servletContext.setAttribute("jsf/ClientSideSecretKey", "[AES key in Base64 format]");
      }
    
    }
    

    Do not forget to register it as a bean in your configuration.