Results 1 to 6 of 6

Thread: Load application context for web services

  1. #1
    Join Date
    Jul 2007
    Posts
    12

    Default Load application context for web services

    I defined a class for loading context to use from web services. I doubt whether it is correct or not. Within web service I call it like: SpringUtil.getBean("xxxx").methodXXX();

    public class SpringUtil {

    static ApplicationContext springContext = null;

    public static ApplicationContext getContext() {
    if (springContext == null)
    springContext = new ClassPathXmlApplicationContext("beans.xml");

    return springContext;
    }

    public static Object getBean(String beanName) {
    return getContext().getBean(beanName);
    }
    }

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Why?! A Web Service normally runs in a Container from there you can load a context with the ContextLoaderListener and setup everything from there. Inject your beans instead of looking them up etc.

    If you really fancy the lookup then use the WebApplicationContextUtils to retrieve a reference to the context loaded by the ContextLoaderListener.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jul 2007
    Posts
    12

    Default

    You are right, but I'm only curious if I use something like this, also not only for web services, is there anything wrong for thread-safety or some other Spring philosophy.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    It depends if it all belongs to one application I would want to make sure that there is only one application context. However with this you might run into the fact that you have multiple instances (a singelton per classloader).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Jul 2007
    Posts
    12

    Default

    Thanks for your replies

  6. #6
    Join Date
    Jul 2007
    Posts
    12

    Default

    Quote Originally Posted by mdeinum View Post
    Why?! A Web Service normally runs in a Container from there you can load a context with the ContextLoaderListener and setup everything from there. Inject your beans instead of looking them up etc.

    If you really fancy the lookup then use the WebApplicationContextUtils to retrieve a reference to the context loaded by the ContextLoaderListener.
    I defined the ContextLoaderListener in web.xml but how will i get context in a java class (web service).

Posting Permissions

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