title: Spring boot JNDI datasource lookup failure - Name comp/env/jdbc not found in context "java:"
body: <p>I have setup a spring boot (v 1.1.9) application to deploy as a WAR file. And I'm trying to integrate this web application with an existing data service module (added as a maven dependency).</p>
  
  <p>Environment trying to deploy: <strong>WebSphere Application Server 8.5.5.4</strong></p>
  
  <p>The issue I'm facing is an application start-up failure when try to look-up a JNDI dataSource (<strong>jdbc/fileUploadDS</strong>) as below within the dependent data service module.</p>
  
  <pre><code>@Configuration
  @Profile("prod")
  public class JndiDataConfig implements DataConfig {
  
      @Bean
      public DataSource dataSource() throws NamingException {
            Context ctx = new InitialContext();
            return (DataSource) ctx.lookup("java:comp/env/jdbc/fileUploadDS");
      }
  
  }
  </code></pre>
  
  <p>My Spring Boot configuration:</p>
  
  <pre><code>@Configuration
  @ComponentScan(basePackages = { "au.com.aiaa.fileupload.data.*", "demo" })
  @EnableAutoConfiguration(exclude = { HibernateJpaAutoConfiguration.class, DataSourceAutoConfiguration.class })
  public class SampleApplication extends SpringBootServletInitializer {
  
      public static void main(String[] args) {
          SpringApplication.run(applicationClass, args);
      }
  
      @Override
      protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
          return application.sources(applicationClass);
      }
  
      private static Class&lt;SampleApplication&gt; applicationClass = SampleApplication.class;
  
      @Bean
      public static Properties fileUploadJndiProperties() throws NamingException {
          JndiObjectFactoryBean jndiFactoryBean = new JndiObjectFactoryBean();
          jndiFactoryBean.setJndiName("props/FileUploadProperties");
          jndiFactoryBean.setExpectedType(Properties.class);
          jndiFactoryBean.setLookupOnStartup(true);
          jndiFactoryBean.afterPropertiesSet();
          return (Properties) jndiFactoryBean.getObject();
      }
  
  }
  </code></pre>
  
  <p>Note that I'm able to lookup props/FileUploadProperties successfully. But failing to do the same for a datasource.</p>
  
  <p>My doubt is it is trying to load a <strong>EmbeddedWebApplicationContext</strong> which is not what I want.</p>
  
  <p><strong>The stack trace is:</strong></p>
  
  <pre><code>Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource au.com.aiaa.fileupload.data.dao.configuration.JndiDataConfig.dataSource() throws javax.naming.NamingException] threw exception; nested exception is **javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context "java:".**
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:301)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)
          at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
          at **org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)**
          at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
          at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:142)
          at org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:89)
          at org.springframework.boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:51)
          at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)
  
  
  ..................
  
  Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource au.com.aiaa.fileupload.data.dao.configuration.JndiDataConfig.dataSource() throws javax.naming.NamingException] threw exception; nested exception is **javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context "java:".**
          at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188)
          at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586)
          ... 132 common frames omitted
  Caused by: javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context "java:".
          at com.ibm.ws.naming.ipbase.NameSpace.getParentCtxInternal(NameSpace.java:1970)
          at com.ibm.ws.naming.ipbase.NameSpace.retrieveBinding(NameSpace.java:1377)
          at com.ibm.ws.naming.ipbase.NameSpace.lookupInternal(NameSpace.java:1220)
          at com.ibm.ws.naming.ipbase.NameSpace.lookup(NameSpace.java:1142)
          at com.ibm.ws.naming.urlbase.UrlContextImpl.lookupExt(UrlContextImpl.java:1436)
          at com.ibm.ws.naming.java.javaURLContextImpl.lookupExt(javaURLContextImpl.java:477)
          at com.ibm.ws.naming.java.javaURLContextRoot.lookupExt(javaURLContextRoot.java:485)
          at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:370)
          at org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:161)
          at javax.naming.InitialContext.lookup(InitialContext.java:436)
          at au.com.aiaa.fileupload.data.dao.configuration.JndiDataConfig.dataSource(JndiDataConfig.java:41)
          at au.com.aiaa.fileupload.data.dao.configuration.JndiDataConfig$$EnhancerBySpringCGLIB$$8001dbbe.CGLIB$dataSource$0(&lt;generated&gt;)
          at au.com.aiaa.fileupload.data.dao.configuration.JndiDataConfig$$EnhancerBySpringCGLIB$$8001dbbe$$FastClassBySpringCGLIB$$3c9e0518.invoke(&lt;generated&gt;)
          at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
          at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312)
          at au.com.aiaa.fileupload.data.dao.configuration.JndiDataConfig$$EnhancerBySpringCGLIB$$8001dbbe.dataSource(&lt;generated&gt;)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
          at java.lang.reflect.Method.invoke(Method.java:611)
          at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
  </code></pre>
  
  <p>What am I missing here? Even when I try to explicitly define the dataSource bean method in SampleApplication.java like below it fails with the same error.</p>
  
  <pre><code>@Bean
      public static DataSource dataSource() throws NamingException {
          JndiObjectFactoryBean jndiFactoryBean = new JndiObjectFactoryBean();
          jndiFactoryBean.setJndiName("java:comp/env/jdbc/fileUploadDS");
          jndiFactoryBean.setExpectedType(DataSource.class);
          jndiFactoryBean.setLookupOnStartup(true);
          jndiFactoryBean.setResourceRef(true);
          jndiFactoryBean.afterPropertiesSet();
          return (DataSource) jndiFactoryBean.getObject();
      }
  </code></pre>
  
  <p>I referred <a href="https://github.com/wilkinsona/spring-boot-sample-tomcat-jndi" rel="noreferrer">this</a> and it says we need to set enableNaming() on servlet container? Can I do something similar for non-embedded web application context? Or is it purely a WAS 8.5 issue??</p>