Announcement Announcement Module
Collapse
No announcement yet.
Who loads beans in j2ee service layer Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Who loads beans in j2ee service layer

    Hi

    I have basic question relation to loading of beans in different layers

    Just like we have ContextLoadedListener - a boot up class for web layer to load all beans in web layer and if we use spring mvc all other controllers tie well with that - and all classes in web layer know who is the appln context and how to get handle to it. This part I understand.

    Now in service layer if it is implemented using an appln server, then who and how do we load all the beans from configuration files. Do we manually write a startup class to do that? And if we do , then how do services (POJOs ) in service layer get handle to the context that loaded them so that if required they can get access to other beans

    Regards
    Ajay

  • #2
    How is the service layer invoked? If its EJBs it would be done in the ejbCreate. If its used from a web application (including web based remoting), you would specify the service layer as a parent context to the web context.

    If you have other singletons that aren't directly used by either of those (like background tasks) then a startup/shutdown class would be used.

    Comment


    • #3
      parent context to web context

      how to set the parent context to a web context? do u have a sample code that explains or shows that?

      thanks
      ajay

      Comment


      • #4
        Here is an example from the Spring JPetStore sample application:
        Code:
        	<!--
        	  - Location of the XML file that defines the root application context.
        	  - Applied by ContextLoaderServlet.
        		-
        		- Can include "/WEB-INF/dataAccessContext-local.xml" for a single-database
        		- context, or "/WEB-INF/dataAccessContext-jta.xml" for a two-database context.
        	  -->
        	<context-param>
        		<param-name>contextConfigLocation</param-name>
        		<param-value>
        			/WEB-INF/dataAccessContext-local.xml  /WEB-INF/applicationContext.xml
        		</param-value>
        		<!--
        		<param-value>
        			/WEB-INF/dataAccessContext-jta.xml  /WEB-INF/applicationContext.xml
        		</param-value>
        		-->
        	</context-param>

        Comment


        • #5
          Oh, BTW that's in the web.xml as one of the parameters for the ContextLoaderListener

          Comment


          • #6
            The example pasted above doesn't show the use of a parent context (maybe copy-paste error?). To use a parent context with the standard ContextLaoder you need to set the locatorFactorySelector and parentContextKey parameters as well.

            The former is a resource locator (e.g. classpath: url) to a bean factory configuration containing a bean that *itself* is an application context. The latter is the id in that bean factory of the context you want to be the parent.
            E.g.

            Code:
            <context-param>
            	<param-name>locatorFactorySelector </param-name>
            	<param-value>classpath:beanRefContext.xml</param-value>
            </context-param>
            <context-param>
            	<param-name>parentContextKey </param-name>
            	<param-value>parent.context.key</param-value>
            </context-param>
            beanRefContext.xml:

            Code:
            <beans>
                <bean id="parent.context.key" lazy-init="true"
            		class="org.springframework.context.support.ClassPathXmlApplicationContext">
            	 <!-- bootstrap from bean definitions in this file -->
                 <constructor-arg><value>classpath:cfgContext.xml</value></constructor-arg>
               </bean>
            </beans>
            The javadocs for ContextLoader have the details.

            Comment

            Working...
            X