Announcement Announcement Module
Collapse
No announcement yet.
Accessing Spring Beans from the spring-servlet.xml Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accessing Spring Beans from the spring-servlet.xml

    Hi all!

    I would like to access an already initialized spring-bean defined in the spring-servlet.xml which is used in the creation of a servlet in web.xml

    so far I've seen:
    ApplicationContext context = WebApplicationContextUtils.getWebApplicationContex t(getServletContext());Object myDao = context.getBean("daoBeanName");

    but the thing is that it does not work
    getServletContext() is not recognized by java for some reason...

    any help would be greatly appreciated!

    Thanks,
    Ron

  • #2
    getServletContext() is not recognized by java for some reason...
    when you are creating a servlet (thus during webapp initialization) the servlet context isn't fully created, so I don't think you can get it.

    Aside from that, Spring servlet context is initialized when the DispatcherServlet is loaded in the servlet context, so you would have to make sure your servlet is always loaded after DispatcherServlet...

    And finally, just out of curiosity: why would you need to even define a custom made servlet if you're using Spring Web Mvc? I think you're a little confused...

    Comment


    • #3
      I would like to access an already initialized spring-bean defined in the spring-servlet.xml which is used in the creation of a servlet in web.xml
      If you want to access a bean defined in the application context, implement ApplicationContextAware

      Comment

      Working...
      X