Announcement Announcement Module
Collapse
No announcement yet.
ContextLoaderListener and applicationContext Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ContextLoaderListener and applicationContext

    Hi there - newbie to Spring here.

    SHORT FORM:

    How do I get the applicationContext when using ContextLoaderListener?

    LONGER FORM:

    I'm working on a project, Hibernate+Servlets+JSP, and discovered Spring little by little while working on the project. I at first had everything working using a static DAO Utility class that went something like this:

    public class DAOUtil {
    private static appcontext = new FileSystemXmlApplicationContext(...);

    public static userDAO getUserDAO() {
    return (UserDAO) DAOUtil.appcontext.getBean("UserDAO");
    }
    }

    However, I started running into the dreaded "lazy initialization exceptions," and started reading posts about how my life would be all happy and beautiful if I just started using the OpenSessionInView pattern.

    So now, I've configured my web.xml file to load the ContextLoaderListener as a listener, and set the OpenSessionInViewFilter class as a filter. No problem there, the server starts up happy & everything.

    Now, as I understand it, the ContextLoaderListener takes care of initializing Spring, wiring my beans, etc.

    My question is: In my DAOUtil class, how do I now get a reference to the ApplicationContext that was set up when Tomcat booted?

  • #2
    Most of the time you don't need access to the ApplicationContext. Instead you let Spring wire everything together for you.
    Maybe you want to have a look at one of the sample applications that spring delivers.

    Do you have existing servlets or are you using the Spring MVC dispatcher servlet?

    Comment


    • #3
      Continuing

      Okay, So I think I understand and agree with what you're saying about letting Spring wire it all together. My question is, more specifically, how do I get the beans it have been wired together?

      Say in my applicationContext.xml, I have:

      <bean id="UserDAO" class="some.Class">
      <property name="SessionFactory"><ref bean="anotherbean" /> </property>
      </bean>

      If I'm using the ContextLoaderListener to initialize Spring (instead of using ClassPathXMLApplicationContext, which returns an object...)

      .. upon what object do I call .getBean("UserDao") to get the UserDAO Bean?

      Comment


      • #4
        Originally posted by Darren H
        Okay, So I think I understand and agree with what you're saying about letting Spring wire it all together. My question is, more specifically, how do I get the beans it have been wired together?
        You could use the WebApplicationContextUtils to access the web application context:

        http://www.springframework.org/docs/...textUtils.html

        Comment


        • #5
          Almost!

          WebApplicationContextUtils is almost perfect, other than the fact you have to pass in a ServletContext, which isn't available at code levels that don't know/care if they're in a Servlet.

          I guess I have some refactoring ahead of me to pass down either the ServletContext or ApplicationContext... But at least now I know how to go forward.

          Thanks for the help!

          Comment


          • #6
            Originally posted by Darren H
            WebApplicationContextUtils is almost perfect, other than the fact you have to pass in a ServletContext, which isn't available at code levels that don't know/care if they're in a Servlet.

            I guess I have some refactoring ahead of me to pass down either the ServletContext or ApplicationContext... But at least now I know how to go forward.

            Thanks for the help!
            If you are going to refactor then I would suggest you to try to adapt to a less intrusive solution. Spring is all about Inversion of control: Instead of your code trying to lookup other beans you ask Spring to inject those beans for you.

            Are you building a Web application? What kind of Web framework are you using. Spring offers is as well integration into Struts applications...

            It might be worth to look into such solutions...

            Comment

            Working...
            X