title: JBoss 7 Override JNDI Datasource
body: <p>I have a Spring Web Application with Maven 3 and Datasources/Beans configured for <strong>local</strong> development on a h2 Database. For Testing there exists a JBoss AS 7.1 Server.</p>
  
  <p>Is it possible to override a Bean / DataSource in a special JBoss XML-File, so that for local (IDE) matters a DriverManagerDataSource is used (see first code posting), and a JndiObjectFactoryBean (second one), if my Application is deployed on a JBoss?</p>
  
  <p><strong>applicationContext.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  &lt;!-- And so on --&gt;"&gt;
  
  &lt;!--GENERAL--&gt;
  &lt;context:annotation-config/&gt;
  &lt;context:component-scan base-package="com.mysuperapp"/&gt;
  
  &lt;!--DATASOURCES--&gt;
  &lt;bean id="activitiDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="org.h2.Driver"/&gt;
      &lt;property name="url" value="jdbc:h2:file:h2\activiti;MVCC=TRUE"/&gt;
      &lt;property name="username" value="sa"/&gt;
      &lt;property name="password" value=""/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="hibernateDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="org.h2.Driver"/&gt;
      &lt;property name="url" value="jdbc:h2:file:h2\hibernate;AUTO_SERVER=TRUE"/&gt;
      &lt;property name="username" value="sa"/&gt;
      &lt;property name="password" value=""/&gt;
  &lt;/bean&gt;
  
  &lt;!--TXMANAGEMENT--&gt;
  
  &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="persistenceUnitName" value="mysuperappPersistenceUnit"/&gt;
      &lt;property name="dataSource" ref="hibernateDataSource"/&gt;
      &lt;property name="jpaVendorAdapter"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/&gt;
      &lt;/property&gt;
      &lt;property name="packagesToScan" value="com.mysuperapp.model"/&gt;
      &lt;property name="jpaPropertyMap"&gt;
          &lt;map&gt;
              &lt;entry key="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/&gt;
              &lt;entry key="hibernate.hbm2ddl.auto" value="update"/&gt;
          &lt;/map&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;!-- And so on --&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>custom applicationContext.xml datasources for JBoss</strong></p>
  
  <pre><code>&lt;bean id="activitiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
      &lt;property name="jndiName" value="java:jboss/datasources/activitiDataSource"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="hibernateDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
      &lt;property name="jndiName" value="java:jboss/datasources/hibernateDataSource"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Any help is greatly appreciated.</p>