title: Spring Boot - JNDI value lookup
body: <pre><code>@SpringBootApplication
  public class SampleTomcatJndiApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(SampleTomcatJndiApplication.class, args);
      }
  
      @Bean
      public TomcatEmbeddedServletContainerFactory tomcatFactory() {
          return new TomcatEmbeddedServletContainerFactory() {
  
              @Override
              protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                      Tomcat tomcat) {
                  tomcat.enableNaming();
                  return super.getTomcatEmbeddedServletContainer(tomcat);
              }
  
              @Override
              protected void postProcessContext(Context context) {
                  ContextResource resource = new ContextResource();
                  resource.setName("jdbc/myDataSource");
                  resource.setType(DataSource.class.getName());
                  resource.setProperty("driverClassName", "your.db.Driver");
                  resource.setProperty("url", "jdbc:yourDb");
  
                  context.getNamingResources().addResource(resource);
  
                  ContextEnvironment contextEnv = new ContextEnvironment();
                  contextEnv.setName("test/value");
                  contextEnv.setType("java.lang.String");
                  contextEnv.setOverride(false);
                  contextEnv.setValue("testing");
                  context.getNamingResources().addEnvironment(contextEnv);
              }
          };
      }
  
      @Bean(destroyMethod="")
      public DataSource jndiDataSource() throws IllegalArgumentException, NamingException {
          JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
          bean.setJndiName("java:comp/env/jdbc/myDataSource");
          bean.setProxyInterface(DataSource.class);
          bean.setLookupOnStartup(false);
          bean.afterPropertiesSet();
          return (DataSource)bean.getObject();
      }
  </code></pre>
  
  <p>In the above code is there a way that I can access the <strong>test/value</strong> from a bean (Just as Datasource Bean Works) ??? </p>
  
  <p>I have tried many approaches but nothing seems to work. I am able to access the test/value from a controller using ( <code>new InitialContext().lookup("java:comp/env/test/value")</code> ). </p>