Results 1 to 4 of 4

Thread: ApplicationContext confusion?

  1. #1
    Join Date
    Aug 2004
    Posts
    5

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    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
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    Aug 2004
    Location
    Brno, Czech Republic
    Posts
    48

    Default

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

    ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContex t(servletContext);

  4. #4
    Join Date
    Aug 2004
    Posts
    5

    Default

    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!

Similar Threads

  1. problems loading applicationcontext
    By andrea_z in forum Web
    Replies: 6
    Last Post: Feb 8th, 2006, 08:25 AM
  2. Replies: 2
    Last Post: Oct 13th, 2005, 09:58 AM
  3. Replies: 4
    Last Post: Jun 8th, 2005, 06:22 AM
  4. Obtaining ApplicationContext for web and batch apps
    By Thomas Vaught in forum Container
    Replies: 10
    Last Post: May 23rd, 2005, 07:09 AM
  5. TransactionProxyFactory and ApplicationContext
    By newToSpring in forum Container
    Replies: 9
    Last Post: Oct 18th, 2004, 03:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •