Announcement Announcement Module
Collapse
No announcement yet.
Newbie: Accessing app ctx in legacy coccon application Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Newbie: Accessing app ctx in legacy coccon application

    I have a sprawling cocoon-based web application. Very big. I am beginning to sprinkle in some spring. A test servlet runs HelloWorld-grade tests, including a oracle db access. Wonderful.



    In a simple servlet, I use:
    ApplicationContext context = new ClassPathXmlApplicationContext("application-config.xml");
    to establish a context, then anything I do within that servlet is all wired together and recognized:
    Bip b = (Bip)context.getBean("theBip");
    b.printData();

    But scattered about the application (all under the upmbrella of a Cocoon servlet) I have opportunities to create a service and use it for data access, etc. If I simply take a class and implement ApplicationContextAware, that does not deliver the context to my class. And if I create a new ClassPathXmlApplicationContext, it seems to be a different application context than the one created in the test servlet (which is runnning in the same jboss instance).

    Any suggestions on how I could make my misc class "aware" of the context, and get me ready to "getBean()"?

    Thanks

  • #2
    Code:
    In a simple servlet, I use:
    ApplicationContext context = new ClassPathXmlApplicationContext("application-config.xml");
    to establish a context, then anything I do within that servlet is all wired together and recognized:
    Bip b = (Bip)context.getBean("theBip");
    b.printData();
    You are better of by using a ContextLoaderListener, that way the ApplicationContext gets published in the ServletContext and can be retrieved by other classes (which have access to the ServletContext).

    web.xml
    Code:
    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    servlet
    Code:
    ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
    Bip b = (Bip)context.getBean("theBip");
    b.printData();
    Next to that the code above is probably something you want to have in your init method of your servlet. You only want to retrieve the Bip bean once and then use it in your doGet/doPost methods.

    Something similair you could do in your cocoon classes, if you have access the the ServletContext and/or the current HttpServletRequest you can use either the WebApplicationContextUtils or the RequestContextUtils to gain access to the ApplicationContext. (It has been I while since I used Cocoon).

    If that isn't possible you can always use an ApplicationContextHolder to gain access to the ApplicationContext. Register it in your ApplicationContext, it will get the ApplicationContext injected due to the ApplicationContextAware stuff.

    Code:
    public final class ApplicationContextHolder implements ApplicationContextAware {
      
      private static ApplicationContext context;
    
      public void setApplicationContext(ApplicationContext applicationContext) {
        context=applicationContext;
      }
    
      public static ApplicationContext getApplicationContext() {
        if (context == null) {
          throw new IllegalStateException("ApplicationContext isn't properly initialized!");
        }
        return context;
      }
    
      public static Object getBean(String beanName) {
        return getApplicationContext().getBean(beanName);
      }
    
    }
    Last edited by Marten Deinum; Jul 29th, 2008, 06:37 PM.

    Comment

    Working...
    X