Announcement Announcement Module
Collapse
No announcement yet.
Embedding BeanFactories Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Embedding BeanFactories

    Just wondering if it is possible to embed one bean factory into another. I have multiple servlets and corresponding contexts, and would like to reuse some already defined beans as opposed to having to copy and paste them.

    Thanks in advance.

    Kiran Keshav
    Columbia Genome Center
    New York, New York
    10027

  • #2
    You can use the ContextLoaderListener to load a parent application context (containing DAOs, service objects, etcetera). Keep in mind that the xxx-servlet.xml file is the web application context containing your web-related beans. The applicationContext.xml can be shared across multiple xxx-servlet.xml files (multiple servlets).

    If you want, you can also tweak the contextConfigLocation setting of the dispatcher servlet (init parameter) to get reusable contexts for a servlet (web-related beans, not just DAOs, service objects).

    rgds,
    Alef

    Comment


    • #3
      Ok, but can I import one xxx-servlet.xml into another?

      As a design decision, I have created different web contexts to handle different client types. For example, I have a template web context (xxx-servlet.xml), another with Controller beans to deal with web requests, and another with the various types of remoting beans. I have done this to put a logical boundary on the code. Are there any performance gains/hits with this rationale?

      Regards,

      Kiran

      Comment


      • #4
        Thanks, Alef.

        I am assuming the ContextLoaderListener will default to using the applicationContext.xml if it doesn't find the contextConfigLocation. As you stated below, this context will then be available to the xxx-servlet.xml files. I have defined my service objects and DAO's in the applicationContext.xml, and my remoting services in the xxx-servlet.xml file. Will the xxx-servlet.xml beans have access to those of the applicationContext.xml?

        For instance, in my applicatonContext.xml if I have

        <bean id="arraydesignMan" class="bus.ArrayDesignManager">
        ...
        <property name="arrayDesignManagerDao">
        <ref bean="arraydesignManDaoHib"/>
        </property>
        </bean>

        <bean id="arraydesignManDaoHib"class="db.ArrayDesignManD aoHib">
        ...
        <property name="sessionFactory">
        <ref bean="mySessionFactory"/>
        </property>
        </bean>

        <bean id="mySessionFactory"
        class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
        ...
        <property name="dataSource">
        <ref bean="dataSource"/>
        </property>
        </bean>

        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
        <property name="driverClassName">
        <value>com.mysql.jdbc.Driver</value>
        </property>
        ...
        </bean>


        In the xxx-servlet.xml file I have

        <bean name="/ArrayDesignService" class="org.springframework.remoting.caucho.Hessian ServiceExporter">
        <property name="service">
        <ref bean="arraydesignMan"/>
        </property>
        <property name="serviceInterface">
        <value>bus.ArrayDesignManagerInterface</value>
        </property>
        </bean>

        Will the "/ArrayDesignService" in the xxx-servlet.xml file be able to see the "arraydesignMan" in the applicatonContext.xml?

        rgds,

        -Kiran

        Comment


        • #5
          Problem solved.
          Answer: The applicationContext.xml is "globally available" to the web contexts. You can either use the default location of the applicationContext.xml (/WEB-INF/) or set the contextConfigLocation in the the web.xml file to specify your own location.

          rgds,

          Kiran

          Comment

          Working...
          X