0

I have a web application context(for DISPATCHER) and also Web Services context(for MESSAGE DISPATCHER) in a web application. I have one bean which is singleton and i need that bean in both contexts. if i specify the bean as singleton in both contexts then it is not singleton any more. please suggest a solution or guide me in right direction.

<servlet>
    <servlet-name>ws</servlet-name>
    <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>        
    <init-param>
        <param-name>transformWsdlLocations</param-name>
        <param-value>true</param-value>
    </init-param>       
</servlet>

<servlet-mapping>
    <servlet-name>ws</servlet-name>
    <url-pattern>/service/*</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        
</servlet>

<servlet-mapping>
    <servlet-name>mvc</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>mvc</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
Venkat
  • 59
  • 1
  • 6

2 Answers2

2

If I understand your question correctly, you have a web application context (e.g. *-servlet.xml) and a root application context (e.g. applicationContext.xml). Web application context extends application context, so that it can access beans from the parent, but not the other way around, so beans which need to be accessed in both should be in applicationContext.xml

See:

Edit:

In your web.xml you have two ServletContexts, but no root context. The answer to What is the difference between ApplicationContext and WebApplicationContext in Spring MVC? has an excellent explanation of this, but in short you will need to load the root application context by adding the following to web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> 

which will then load the root application context from applicationContext.xml. Beans in the root application context will be accessible in both ServletContexts. You would usually want only web related beans (Controllers etc) in you ServletContexts, and shared business logic in the root application context.

Community
  • 1
  • 1
trf
  • 1,279
  • 1
  • 13
  • 33
  • one is webapplication context(spring mvc) another is for webservices applicationcontext(org.springframework.ws.transport.http.MessageDispatcherServlet) which is for creating webservices using spring. but let me try with your suggestion Thanks – Venkat Oct 25 '13 at 10:33
1

If the bean needs to be used in two different contexts, define it in a separate application context xml file, then create three application contexts:

Root context - contains shared bean(s)

App context 1. e.g. web app. Has the root context as a parent. App context 2. e.g. web services interface, also has the root context as a parent.

The bean will be instantiated once when the root application context is created. Both the child app contexts can then use that singleton bean.

Hedley
  • 1,060
  • 10
  • 24
  • Hedley, please can you suggest how to load this third context. currently i am loading two contexts it with respective servletname-context.xml file specifying in wex.xml. i am confused with import. import will eliminate redundant specifying the same bean in both xml files right? not the single instance in both contexts? – Venkat Oct 25 '13 at 09:10
  • Yeah, sorry, I shouldn't have used the word "import". I've tweaked the answer so it isn't so confusing, but it looks like trf has given a good explanation now anyay. – Hedley Oct 28 '13 at 10:15