views:

57

answers:

3

I am integrating Spring MVC into an existing project I have been working on. By integrating, I mean I am rewriting the project using Spring, and using much of my old code. I have already setup the environment and have began working on it. I will refer to this project as ProjectX.

I have already setup and configured my ProjectX-servlet.xml that holds the view-resolver bean, and the controller beans, etc. I want to set up an applicationContext.xml file that I can place all my DAO beans in such as ...

<bean id="MemberDAO" class="com.xxx.xxx.MemberDAO"/>
<bean id="ProductDAO" class="com.xxx.xxx.ProductDAO"/>

I want these values to be in the applicationContext.xml so that in my controllers I can do the following.

public SomeController extends SimpleFormController{

   private MemberDAO memberDao;
   private ProductDAO productDao;

   ...getter/setter methods for memberDao;

   ...getter/setter methods for productDao;

and the values will be available(injecting them into the controllers)

I have configured the controllers in the ProjectX-servlet.xml like the following definition.

<bean name="/SomeController.thm" class="com.xxx.xxx.controllers.SomeController">
      <property name="memberDao" ref="MemberDAO"/>
      <property name="productDao" ref="ProductDAO"/> 
</bean>

I believe I need to configure something such as the following in my web.xml so that it knows to load the application context.

  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>

  <servlet>
   <servlet-name>context</servlet-name>
   <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
  </servlet>

My question is, what do I have to do following creating an applicationContext.xml file, to be able to do what I showed above and inject beans such as the ProductDAO and MemberDAO into my controlellers which are configured in the ProjectX-servlet.xml

I have been using Spring MVC for a contract for a couple months and am comfortable with how to use it, but I am new to configuring it on my own, for my own use, so I would appreciate if any advice or answers were explained a little easier for me.

Thank you

+1  A: 

Unless I'm misunderstanding, the solution you're looking for is to use an import statement in your applicationContext.xml. This effectively combines the two XML files into a single context, allowing you to reference beans in either.

Ex:

<import resource="classpath:foo/bar/ProjectX-servlet.xml" />

You may or may not want to use "classpath." See section 3.2.2.1 in the Spring docs for more details.

RonU
+1  A: 

You don't have to do anything special. You can continue injecting beans defined in applicationcontext.xml into the beans defined in xx-servlet.xml as if all of them are declared in same file. Do remember to use the attribute ref instead of ref-local as below.

<bean id="mycontroller" class="x.y.z.CustomerController>
   <property name="service" ref="myservice"/><!--myservice defined in applicationcontext-->
</bean>
chedine
+1  A: 

By convention, the name you give to your instance of DispatcherServlet will be associated with {name}-servlet.xml. This context will be a child to applicationContext.xml as you described, meaning it will have access to beans in applicationContext.xml.

Try the following in your web.xml:

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

<servlet-mapping>
        <servlet-name>ProjectX</servlet-name>
        <url-pattern>/projectx/*</url-pattern>
</servlet-mapping>
James Earl Douglas