Results 1 to 2 of 2

Thread: How to use Spring IOC to set a property in the Base class

  1. #1
    Join Date
    May 2005
    Posts
    4

    Default How to use Spring IOC to set a property in the Base class

    Code:
    public interface BaseDAO
    {
      public void setDataSource(DataSource dataSource);
    }
    
    public class BaseDAOImpl implements BaseDAO
    {
      public DataSource dataSource;
    
      public void setDataSource(DataSource dataSource)
      {
        this.dataSource = dataSource;
      }
    }
    
    public class UserDAOImpl extends BaseDAOImpl implements UserDAO
    {
      ...
    }
    
    public class CustomerDAOImpl extends BaseDAOImpl implements CustomerDAO
    {
      ...
    }

    Now within my dataAccessContext-jdbc.xml

    Code:
    <!--  JNDI lookup of datasouce -->
    <bean id="dataSource"                                                                         class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName">
        <value>jdbc/jhs</value>
      </property>
    </bean>
    
    <bean id="userDAO" class="com.xyz.dao.impl.UserDAOImpl">
      <property name="dataSource">
        <ref local="dataSource"/>
      </property>
    </bean>
    
    <bean id="customerDAO" class="com.xyz.dao.impl.CustomerDAOImpl">
      <property name="dataSource">
        <ref local="dataSource"/>
      </property>
    </bean>
    Does anyone know a way to set the dataSource property for the BaseDAOImpl once, and not repeat setting the dataSource in each of the sub classes that derive from BaseDAOImpl?

    Thanks for your help.

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    The use of "template" bean definitions (specifying parent) should work. Alternative you could use autowiring, which would avoid the need to refer to the datasource explicity in each case.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

Similar Threads

  1. ERROR: Context initialization failed
    By makhlo in forum Architecture
    Replies: 8
    Last Post: Jul 11th, 2008, 01:41 AM
  2. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  3. Unit testing with JOTM and JtaTransactionManager
    By lalle in forum Architecture
    Replies: 1
    Last Post: Oct 15th, 2005, 09:05 AM
  4. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  5. Replies: 4
    Last Post: Aug 17th, 2005, 04:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •