Announcement Announcement Module
Collapse
No announcement yet.
Referencing ServletContext in applicationContext.xml Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Referencing ServletContext in applicationContext.xml

    I am trying to instantiate an instance of the FopFactory as a singleton in my applicationContext.xml. But to get the servlet-context: functionality of Fop to work, I have to pass in a ServletContextURIResolver that has been configured with the current Servlet Context, and I can't find a way to get that reference:

    So far I have:

    Code:
      <bean id="fopFactory" class="org.apache.fop.apps.FopFactory" factory-method="newInstance">
        <property name="uriResolver">
          <bean class="org.apache.fop.servlet.ServletContextURIResolver">
            <constructor-arg ref="servletContext"/>
          </bean>
        </property>
      </bean>
    But I don't know how to create the bean that is currently referred to as servletContext in the Constructor Arg above. Any ideas?
    (*Chris*)

  • #2
    Maybe?

    I don't know if this is the right way to go, but it seems to work. I created the following BeanFactoryPostProcessor and had it register the singleton with the factory. It seems to be working, but I was really hoping for something that was already built into the system. Please let me know if I've gone atray.

    Code:
    public class ServletContextBeanInjector implements BeanFactoryPostProcessor, ServletContextAware {
      private ServletContext servletContext;
    
      /**
       * ServletContextAware: Inject the Servlet Context
       * 
       * @param context The Servlet Context
       */
      public void setServletContext (ServletContext ctx) {
        this.servletContext = ctx;
      } //setServletContext
      
      /**
       * BeanFactoryPostProcessor: Post Process the Bean Factory
       * 
       * @param factory The Bean Factory
       */
      public void postProcessBeanFactory (ConfigurableListableBeanFactory factory) {
        factory.registerSingleton("servletContext",servletContext);
      }//postProcessBeanFactory
    
    } //*ServletContextBeanInjector
    Then I just define it in my applicationContext.xml and things seem to work...

    Code:
      <bean id="servletContextBeanInjector" class="com.package.ServletContextBeanInjector"/>
    (*Chris*)

    Comment


    • #3
      Why so complex???? Simple create a subclass of the ServletContextURIResolver let that implement ServletContextAware and it should work.

      Comment


      • #4
        Ummm?

        I don't really see how that is less complex , but it's definitely less powerful. It would solve the one specific problem in a very specific way, but each other object that I need to pass the servletContext too would have to be solved in another one-off solution. I think mine's simpler, so far...
        (*Chris*)

        Comment

        Working...
        X