Announcement Announcement Module
Collapse
No announcement yet.
How to use Spring IOC to set a property in the Base class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment

    Working...
    X