Results 1 to 3 of 3

Thread: Accessing Spring Beans from the spring-servlet.xml

  1. #1
    Join Date
    Oct 2010
    Posts
    3

    Default 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. #2
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    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...

  3. #3
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    287

    Default

    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
    Amila Domingo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •