Announcement Announcement Module
Collapse
No announcement yet.
Separating Service Configuration Files From Web layer configurations Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Separating Service Configuration Files From Web layer configurations

    I am working on a typical 3 tier architecture.

    I have business layer which is made up of services implemented as POJOs.
    They are configured as beans in spring's xml file e.g. service-layer.xml
    The beans from service layer refer to DAOs which are defined as beans in another spring file called application-hibernate.xml

    So i have 2 xml files - service.xml and appln-hibernate.xml and beans from service.xml refer to beans from appln-hibernate.xml

    Now in Web layer, my action classes, refer to beans defined in service.xml

    I dont want all the beans in 1 context.

    Can I load web layer beans in 1 context using web based contextloader and make them refer beans loaded in another context which i loaded using classpathxmlcontext?

    My problem is If i dont mention all the xml files to web layer then my action beans fail to load because it says service beans definiton not found

    There is a sligth confusin for me. I want to separate the beans definitons for web and service layers and i want beans in web layer access beans defined in service layer without definiting service layer dependent beans.

    I hope I have not confused everyone.

  • #2
    Hi Ajay,

    In your web.xml, the following should allow you to keep the three separate configuration files:

    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    
    <!DOCTYPE web-app PUBLIC
    	"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    	"http://java.sun.com/dtd/web-app_2_3.dtd">
    
    <web-app>
    
      <!-- other web.xml elements -->
    
      <context-param>
        <param-name>contextConfigLocation</param-name>
          <param-value>
            /WEB-INF/application-hibernate.xml /WEB-INF/service-layer.xml
          </param-value>
        </context-param>
    
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    
      <!-- other web.xml elements -->
    
    </web-app>
    I don't know what you mean when you say "My problem is If i dont mention all the xml files to web layer then my action beans fail to load because it says service beans definiton not found". If your web beans (e.g. controllers) depend on service layer beans defined in a different context, you have to let Spring know somehow about those beans. The above XML does just that.

    Check out the samples/JPetstore/war/WEB-INF/web.xml file (from the Spring distribution) as an example. That sample application also breaks the context files into three separate ones (web, service, and dao).

    -Arthur Loder

    Comment


    • #3
      Arthur,

      What I am trying to say is foll:

      Lets say I have ActionBean that depends on ServiceBean
      And ServiceBean internally depends on a DAO bean

      Now DAO is not directly used by web action beans and I dont want to expose the definiton of DAO beans to my web layer because no one is using them directly.

      Now I have logically separated the bean definitons in my 3 files. for for wb , other for service and last one is for hibernate. Thats fine. thats logical separation for good maintenance.

      But I still have to load beans from all these 3 files in 1 context because my action bean if referring to service bean and for service bean to load properly , my dao bean definition has to be there.

      So my web layer indirectly came to know of all beans that are used in DAO layer.

      So later when i want to physically separate the layers, i wil eb forced to copy the definition of dao beans to web layer even though no one is usign in that layer. I mean when I introduce remote access to my service layer then I dont want the DAO beans defns in my web layer to know. Later I should be able to easily introduce remote access to my service layer. And when I do that why should my web layer beans know the beans on which my service layer beans dependent upon.

      Thanks
      Ajay

      Comment


      • #4
        Originally posted by Ajay View Post
        But I still have to load beans from all these 3 files in 1 context because my action bean if referring to service bean and for service bean to load properly , my dao bean definition has to be there.

        So my web layer indirectly came to know of all beans that are used in DAO layer.

        So later when i want to physically separate the layers, i wil eb forced to copy the definition of dao beans to web layer even though no one is usign in that layer.
        I don't really understand what you are saying here. I mean, your web implementation classes are dependent on the service interfaces, and your service implementation classes are dependent on the dao interfaces. Good. And you are also able to separate the bean definitions into separate files. Good. What is the problem here? You do not like that your web beans can see the dao beans because they are defined in a parent context?

        -Arthur Loder

        Comment


        • #5
          Let me try to explain this again.

          Lets say I have Action bean A - using Service Bean S - Now both beans will be defined in one applicationContext.xml which is loaded by ContextListener.
          The above beans are defined in my Web Layer.

          Now if my Service Bean S is using a DAO bean D which will be executed in my Business Layer. Now if i decide to separate my Business Layer and Web Layer then I will have 2 context files. one for my web layer and one for my service layer. My web layer context file will have definitions of action beans and service beans.

          My service layer context file will have DAO beans definitions.

          I dont have to put the DAO bean definition in my web context file because no one will be using it in that layer. You got the last part correct, I dont want the DOA beans in web context. Can I do that? And if i try to do that then it says failed to define service bean in my web context.

          Comment


          • #6
            If you want your layers to be truly independant, you need some sort of bootstrap code in charge of loading up the application context. So the solution in the business tier is to hide the corresponding application context behind a good old "service factory". Make this factory implements the Spring FactoryBean interface and provide a constructor taking the application context filename as a parameter. From there you should be able to easily inject your services in your web layer using a different application context. The factory just needs to retrieve the different services objets from the given application context xml file. I do that all the time and it works great. It's also a great way to expose remote or local services to a client not using Spring without coupling it to a given protocol or implementation.

            By the way, DAO are part of the business layer since they know about your business objets. Hibernate by itself is the persistence layer. Of course, your domain objets should be independant of your DAO implementations but theoricaly and contrarily to popular belief, they are part of the domain model (as described in Domain Driven Design).

            Originally posted by Ajay View Post
            Let me try to explain this again.

            Lets say I have Action bean A - using Service Bean S - Now both beans will be defined in one applicationContext.xml which is loaded by ContextListener.
            The above beans are defined in my Web Layer.

            Now if my Service Bean S is using a DAO bean D which will be executed in my Business Layer. Now if i decide to separate my Business Layer and Web Layer then I will have 2 context files. one for my web layer and one for my service layer. My web layer context file will have definitions of action beans and service beans.

            My service layer context file will have DAO beans definitions.

            I dont have to put the DAO bean definition in my web context file because no one will be using it in that layer. You got the last part correct, I dont want the DOA beans in web context. Can I do that? And if i try to do that then it says failed to define service bean in my web context.
            Last edited by madtree; Oct 19th, 2006, 05:01 PM.

            Comment


            • #7
              Hi Ajay,

              Originally posted by Ajay View Post
              I dont have to put the DAO bean definition in my web context file because no one will be using it in that layer. You got the last part correct, I dont want the DOA beans in web context. Can I do that? And if i try to do that then it says failed to define service bean in my web context.
              Sorry, but I am still a little unsure what you are saying. Have you looked at the JPetstore sample application? It has three different context files, each for a different "layer":
              1. jpetstore\war\WEB-INF\petstore-servlet.xml (for web beans)
              2. jpetstore\war\WEB-INF\applicationContext.xml (for service beans)
              3. jpetstore\war\WEB-INF\dataAccessContext-local.xml (for DAO beans)
              Also, notice that the web beans refer to the "petStore" service bean, even though it is defined in a different context. And notice that the "petStore" service bean refers to dao beans defined in the dao context.

              You talk about having to copy the beans from one context into another context, so I am not 100% sure you see that this can be avoided. Is the above situation not satisfactory?

              -Arthur Loder

              Comment


              • #8
                Arthur

                I was going through the example u had mentioned in email. How the beans are separated in 3 different context files. Logically they are kept separate.

                Now beans in applicationContext.xml refer to beans defined in dataAccessContext-local.xml. And both are loaded by context loader listener

                org.springframework.web.context.ContextLoaderListe ner

                Right?

                Means all the beans are loaded by this one contextloaderlistener which is in web layer.

                Now I am coming back to my old question. This time I will try to be more clear.

                Lets say I want 2 different context objects. One for Web Layer and one for service Layer and I want beans from one context refer to beans defined and loaded in another context. Can I do that?

                When I saw the pet example - both context files are passed to contextloaderlistener.

                I want to pass only 1 file applicationContext.xml file to ContextLoaderListerner and another SingletonClass that loads bean in another context using ClasspathXMLContextXXXX.

                Is this right way to do it? Even though I am keeping bean definitions separate in 3 files still they are loaded by 1 application context. And tomorrow if i decide to physically separate the service layer and make it remote then would it not be tough to change if web beans want to remotely refer to beans defined in another context?

                I hope i m not confusing you again

                Comment

                Working...
                X