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

  • Introducing ApplicationContextAware

    Hello. I am very new to Spring and AOP in general. I am writing a web application for which I have created several services. For one of these services, I would like to be able to (optionally) initialize some properties using items from the ServletContext.

    It appears that the best way to get the ServletContext during initialization is for the object in question to implement ApplicationContextAware. I do not want to do that directly in the service class because I do not want to couple it to any Spring classes at all. Because of that, I have attempted--I believe successfully--to use Spring AOP to introduce the ApplicationContextAware interface to the service instance. When I iterate over the resulting proxy's interfaces, ApplicationContextAware IS listed.

    Problem is, ApplicationContextAware.setApplicationContext() is never invoked on this object. I assume that I am running into an initialization ordering problem here (maybe the proxy is created AFTER the postprocessor does its magic on the application context beans?)

    Is it possible to do this? Inject ApplicationContextAware into an object in the web application context XML file and have it catch a setApplicationContext() call? If not, is there any other loosely-coupled way for me to get the ServletContext to the service bean during the initialization process?

    Thanks in advance to anyone who can help. I also apologize in advance if this is covered elsewhere. I have been looking for several hours and have been unsuccessful...but that doesn't guarantee that I didn't just look in the wrong places. :-)

  • #2
    Re: Introducing ApplicationContextAware

    Originally posted by mattj65816
    It appears that the best way to get the ServletContext during initialization is for the object in question to implement ApplicationContextAware. I do not want to do that directly in the service class because I do not want to couple it to any Spring classes at all.
    Sometimes it's better to simply do what is easiest. There's a somewhat amusing discussion about not implementing Spring interfaces at http://forum.springframework.org/showthread.php?t=11871.

    Incidentally, what do you need ServletContext for? If inside a Servlet, could you not call getServletContext() or getServletConfig().getServletContext()?
    Last edited by robyn; May 19th, 2006, 06:05 AM.

    Comment


    • #3
      Here's the "why" (I think it's a valid use...bear with me.)

      One of the services needs to write data to a temporary directory on the filesystem. I would like to be able to use javax.servlet.context.tempdir as a default if no other directory is explicitly configured. The aspect grabs the application context, then the servlet context, calls ServletContext.getAttribute("javax.servlet.context .tempdir") to get the string, then passes its value to the service via the corresponding property setter. That way there is no dependency between the service and Spring, nor is there one between the service and the Servlet/ServletContext. The service simply takes a string path, not knowing that it came from a ServletContext.

      I have (temporarily?) solved this by making the ServiceLocator ApplicationContextAware and implementing its setApplicationContext() method in such a way that it "broadcasts" the context to all ApplicationContextAware service references it holds. The ApplicationContextAware mixin applied to the service picks this up and calls the service's setter appropriately.

      This creates a dependency between the service locator and Spring, but that seems acceptable because the locator is basically just a bean that holds onto a bunch of service references.

      Comment

      Working...
      X