Announcement Announcement Module
Collapse
No announcement yet.
Same bean id in different wars in same JVM Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Same bean id in different wars in same JVM

    Hi,

    If there are 2 diff wars in a single server and the bean (singleton) ids are same across the wars ...what happens during the access of the bean??

    Which bean would be invoked?????

    Regards

  • #2
    You should have no conflict between the bean names at all. Each war will have its own instance of the Spring IoC container with no visibility to the other war's container and beans.

    If you still have questions about this, please respond with a bit more detail about your setup and how you're instantiating the container and accessing beans.

    For example, are you directly accessing the container as follows?

    Code:
    ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
    FooService fooService = (FooService) ctx.getBean("fooService");
    // ...
    If so, where in your webapp are you doing this?

    Or perhaps you're using Spring MVC and your container/beans are getting instantiated for you by the DispatcherServlet?

    At any rate, what happens in each war is going to be isolated and free of naming collisions. This is not just because things are happening in different classloaders, but because each war has its very own instance of the Spring IoC container.

    Comment


    • #3
      Yes true enough....each should hv diff instances....

      But my spring jars are at ear level..and this ear has 2 wars...

      in each war...the name of the beans....in spring file...and web.xml is also the same...

      i hv 2 spring config files,2 web.xml -one each for each war

      I am using Spring MVC...and dispatcher servlet...

      When i deploy the app..the beans are getting deployed fine...for each war...

      so i hv 2 singleton with the same id....in my server...of course diff context..

      only the contexxt differs....

      First i hit http:// the http version of the bean "say beanA" gets hit
      Second i hit https:// the http version of the bean "beanA" gets hit

      i am using WAS 5.1 server both of them has parent_last as classloader

      Wonder why the https bean in not getting called??

      Comment


      • #4
        It's a bit hard for me to follow your setup, and I'm not all that familiar with EAR-style deployments, but it sounds like your issue is probably WAS configuration-related. Are you expecting http requests to be routed to the webapp in war A, and https requests to be routed to the webapp in war B? Is this happening properly?

        If you're continuing to have trouble, perhaps you could put together a very simple example of the problem and attach it here? At this point, I would need to see your setup to help further.

        Comment


        • #5
          i cant share the code...but i will generally explain the structure

          Dates.ear
          sample.war (for http)
          sample_https.war (for https)

          Spring config file with the beans are same for both the wars...(same ids)
          web.xml is also the same

          Deploying is fine...no error in the logs

          when i hit http.. ..http controller bean is invoked
          when i hit https.. ..even then http controller bean is invoked

          Comment


          • #6
            Again, this looks more like a WAS configuration issue.

            If you remove the sample_https.war from the ear, what happens? Do your http:// requests still make it through? If this is the case, then there's something off with your deployment/configuration. The key thing is to ensure that https requests are actually being routed to the sample_https.war.

            Comment


            • #7
              Initially i had only the https version..at that time...it was hitting through the https controller bean.....

              and later the http version came into the picture....but under a diff context...

              Comment


              • #8
                Sorry, I'm having a hard time determining the cause of the problem based on our conversation here. If you can create a very simple example that replicates your problem and attach it (I understand you can't share your company's code, but you could create a standalone example), I could take a look at it and diagnose the problem. Otherwise, I'm afraid I'm not going to be of any help.

                Regards,

                - Chris

                Comment


                • #9
                  The web.xml in both the cases is same

                  <web-app>

                  <servlet>
                  <servlet-name>ProdServlet</servlet-name>
                  <servlet-class>org.....DispatcherServlet</servlet-class>
                  </servlet>

                  <servlet-mapping>
                  <servlet-name>ProdServlet</servlet-name>
                  <url-pattern>/products</url-pattern>
                  </servlet-mapping>

                  </web-app>

                  ProdServlet-servlet.xml fot http version war

                  <?xml version="1.0" encoding="UTF-8"?>
                  <beans>

                  <bean id="products" class="com.xxx.ProductController"/

                  <bean id="mappingNonSecureBean" class="com.xxx.NonSecureMapping">

                  <property name="paramName" value="type"/>

                  <property name="urlKeys">

                  <entry key="products">
                  <value> <ref bean="products"/></value>
                  </entry>
                  </property>


                  </bean>

                  <bean id="viewResolver" class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
                  <property name="prefix" value="/WEB-INF/jsp/"/>
                  <property name="suffix" value=".jsp"/>
                  </bean>>

                  </bean>



                  ProdServlet-servlet.xml fot https version war

                  <?xml version="1.0" encoding="UTF-8"?>
                  <beans>

                  <bean id="products" class="com.xxx.ProductController"/

                  <bean id="mappingSecureBean" class="com.xxx.SecureMapping">

                  <property name="paramName" value="type"/>

                  <property name="urlKeys">

                  <entry key="products">
                  <value> <ref bean="products"/></value>
                  </entry>
                  </property>


                  </bean>

                  <bean id="viewResolver" class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
                  <property name="prefix" value="/WEB-INF/jsp/"/>
                  <property name="suffix" value=".jsp"/>
                  </bean>>

                  </bean>





                  com.xxx.SecureMapping / com.xxx.NonSecureMapping


                  public class SecureMapping extends AbstractUrlHandlerMapping {


                  private String paramName;
                  private Map urlKeys;

                  public void setUrlKeys(Map paramName) {
                  this.urlKeys = paramName;
                  }


                  public void setParamName(String paramName) {
                  this.paramName = paramName;
                  }

                  @Override
                  protected Object getHandlerInternal(HttpServletRequest request) {
                  String paramValue=request.getParameter(paramName);
                  return urlKeys.get(paramValue);
                  }
                  }


                  Here when i hit the http version of the application...

                  it goes through the com.xxx.NonSecureMapping class and the appln is routed to teh corresponding http bean products.

                  But when i hit https....it goes through com.xxx.SecureMapping class and the bean that gets invoked is the http bean....ProductController...


                  Wonder why????

                  Comment


                  • #10
                    Here when i hit the http version of the application...

                    it goes through the com.xxx.NonSecureMapping class and the appln is routed to teh corresponding http bean products.

                    But when i hit https....it goes through com.xxx.SecureMapping class and the bean that gets invoked is the http bean....ProductController...

                    Wonder why????
                    In your config for http, you declare a NonSecureMapping bean and inject into it a map with key 'products' and reference a ProductController bean.

                    In your config for https, you declare a SecureMapping bean and inject into it a map with key 'products' and reference a ProductController bean.

                    As far as I can tell, your bean declaration for 'products' in both contexts uses the exact same ProductController class.

                    Thus, when coming into your application via http OR https, you end up using an instance of this ProductController class. It does not, however, mean that it is the same instance somehow shared across webapps as you alluded to earlier. It simply means that you have two objects of the same type, one in each container.

                    I hope I haven't missed something - does this help clear the issue up for you? If it does not, what is the actual problem you're experiencing? i.e.: why is this behavior an issue for you?

                    Comment


                    • #11
                      Thus, when coming into your application via http OR https, you end up using an instance of this ProductController class. It does not, however, mean that it is the same instance somehow shared across webapps as you alluded to earlier. It simply means that you have two objects of the same type, one in each container.

                      I hope I haven't missed something - does this help clear the issue up for you? If it does not, what is the actual problem you're experiencing? i.e.: why is this behavior an issue for you?

                      I agree that there should be 2 objects of same type in each within its own app class loader

                      But the problem i face is....

                      When i hit the http

                      it goes through the NonSecureMapping bean and gets the products bean instance of http

                      And when i hit the https

                      it goes through the SecureMapping bean and
                      gets the products bean instance of http it is getting routed to the http bean.....

                      The classloader policy for both the app is parent_last in WAS 5.1

                      Regards

                      Comment


                      • #12
                        I've asked some other folks to take a look at this thread. Unfortunately, it's not clear to me what's happening here. Stay tuned for further responses.

                        One question in the meantime: How do you know that the 'products' bean you get when hitting https is the bean that was configured in the http war's container? Given that the 'products' bean in both the http and https containers are instances of the same class, I'm interested to see how you're certain that you're hitting the one that was configured in the http container.

                        Comment


                        • #13
                          I had put the System.out.pritnln statements in handleRequest method of both the controller beans...

                          in http version.... " In HTTP BEAN instance "
                          in https version.... " In HTTPS BEAN instance "


                          Also System.out.pritnln statements in the SecureMapping and NonSecureMapping beans


                          So when i hit https...it goes through SecureMapping though ...(figured it SOP statements )

                          then comes to the http product instance

                          and in console....it prints " In HTTP BEAN instance "


                          Both the wars are embedded in a single ear

                          Regards
                          Last edited by A Kumar; Oct 10th, 2007, 02:55 PM.

                          Comment


                          • #14
                            Just to clarify, are all of your application bean classes local to each web app (i.e., in WEB-INF/classes or WEB-INF/lib), or do you have some/all deployed at the ear level?

                            It might be helpful to gather more info about which classloader is being used to load your bean classes. I believe that WAS has a classloader viewer utility which can help with this. See here for more details:

                            http://www.redbooks.ibm.com/redpapers/pdfs/redp4307.pdf

                            I assume your preference is to have a distinct application context for each web app with separate bean instances, but if you are instead interested in having instances of common beans shared between the web apps, you might take a look at this option:

                            http://blog.interface21.com/main/200...g-application/

                            Comment


                            • #15
                              Originally posted by Mike Bingham View Post
                              Just to clarify, are all of your application bean classes local to each web app (i.e., in WEB-INF/classes or WEB-INF/lib), or do you have some/all deployed at the ear level?

                              classes are local to each ...war....but they have the same names
                              and the bean ids are same in the config files across the wars

                              It might be helpful to gather more info about which classloader is being used to load your bean classes. I believe that WAS has a classloader viewer utility which can help with this. See here for more details:

                              http://www.redbooks.ibm.com/redpapers/pdfs/redp4307.pdf
                              I will have a look at this...


                              I assume your preference is to have a distinct application context for each web app with separate bean instances
                              Yes....

                              Comment

                              Working...
                              X