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

  • ApplicationContext confusion?

    I am now building a web application with OpenSessionInViewFilter, the following is my web.xml :
    <context-param>
    <param-name>
    contextConfigLocation
    </param-name>
    <param-value>
    /WEB-INF/src/dataAccessContext.xml
    /WEB-INF/src/contentContext.xml
    /WEB-INF/src/scheduleContext.xml
    /WEB-INF/src/securityContext.xml
    </param-value>
    </context-param>

    <filter>
    <filter-name>Character Encoding</filter-name>
    <filter-class>com.raykey.web.filters.EncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>GB2312</param-value>
    </init-param>
    </filter>

    <filter-mapping>
    <filter-name>Character Encoding</filter-name>
    <url-pattern>*.do</url-pattern>
    </filter-mapping>

    <filter>
    <filter-name>HibernateSpringFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate.support.Op enSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>HibernateSpringFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
    <listener-class>org.springframework.web.context.ContextLoade rListener </listener-class>
    </listener>

    And I reference the bean in my Java file using the following way:

    ApplicationContext ctx;
    String[] paths = {"dataAccessContext.xml",
    "contentContext.xml",
    "scheduleContext.xml",
    "securityContext.xml"};
    ctx = new ClassPathXmlApplicationContext(paths);
    return ctx.getBean(name);


    I want to know whether I am right! If I am wrong , please tell me what to do to let me access the beans in Strtuts Actions, thanks very much!

    BTW, if I am right, but why I will got two sessionFactory in one application, when I acess the OpenSessionInViewFilter, I can get a SessionFactory, but when I access the real DAOIMPL, it will use another sessionFactory, so the Session will be not find! Can anybody help me?

  • #2
    your web.xml seems OK
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/src/dataAccessContext.xml
    /WEB-INF/src/contentContext.xml
    /WEB-INF/src/scheduleContext.xml
    /WEB-INF/src/securityContext.xml
    </param-value>
    </context-param>

    <listener>
    <listener-class>org.springframework.web.context.ContextLoade rListener </listener-class>
    </listener>
    This will create a Spring webContext. So you can use it to get your beans.
    ApplicationContext ctx;
    String[] paths = {"dataAccessContext.xml",
    "contentContext.xml",
    "scheduleContext.xml",
    "securityContext.xml"};
    ctx = new ClassPathXmlApplicationContext(paths);
    return ctx.getBean(name);
    there is absolutely no need to do this because of:
    1. each time this code is executed, Spring will load the xml files, parses the content and creates the beans. It will do it over and over!!!
    2. you already asked spring to create the webContext that hold your beans, why not you it

    please tell me what to do to let me access the beans in Strtuts Action
    the easy way is to make your Actions extend
    Code:
      org.springframework.web.struts.ActionSupport
      or
      org.springframework.web.struts.DispatchActionSupport
    then from your action you can access your beans using:
    Code:
      myBean = &#40;MyBean&#41; webApplicationContext.getBean&#40;"myBean"&#41;;
    BTW, if I am right, but why I will got two sessionFactory in one application, when I acess the OpenSessionInViewFilter, I can get a SessionFactory, but when I access the real DAOIMPL, it will use another sessionFactory, so the Session will be not find! Can anybody help me?
    You have two sessionFactories because you have two contexts, one created by your listener and an other created by your code.

    HTH

    Comment


    • #3
      In your code you can get the application context created by ContextLoaderListener from ServletContext using this code snippet:

      ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContex t(servletContext);

      Comment


      • #4
        Yesterday, I have made it by myself, but it doesn't look like so much elegance!

        There is a lot of things to do in Spring! hehe ..... Anyway, Thanks very,very much!

        Comment

        Working...
        X