春季-使用applicationContext.xml和XXXXX-servlet.xml [英] Spring - using applicationContext.xml and XXXXX-servlet.xml

查看:57
本文介绍了春季-使用applicationContext.xml和XXXXX-servlet.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Spring MVC集成到我正在从事的现有项目中.通过集成,我的意思是我将使用Spring并使用许多旧代码来重写项目.我已经设置好环境并开始研究它.我将这个项目称为 ProjectX .

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.

我已经设置并配置了保存视图解析器bean和控制器bean的 ProjectX-servlet.xml .我想设置 applicationContext.xml 文件,我可以将所有DAO bean放入...

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"/>

我希望这些值位于 applicationContext.xml 中,以便在控制器中可以执行以下操作.

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)

我已经按照以下定义在 ProjectX-servlet.xml 中配置了控制器.

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>

我相信我需要在 web.xml 中配置如下内容,以便它知道加载应用程序上下文.

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>

我的问题是,创建applicationContext.xml文件之后,我必须做什么,才能执行上面显示的内容,并注入 ProductDAO MemberDAO 进入我的控制器,这些控制器在 ProjectX-servlet.xml

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

我已经使用Spring MVC签订合同了几个月,并且对如何使用它感到很满意,但是对于自己配置自己使用,我还是陌生的,因此,如果有任何建议或答案,我将不胜感激.对我来说更容易解释了.

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.

谢谢

推荐答案

您无需执行任何特殊操作.您可以继续将applicationcontext.xml中定义的bean注入到xx-servlet.xml中定义的bean中,就像所有它们都在同一文件中声明一样.请记住要使用属性 ref 代替如下所示的 ref-local .

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>

这篇关于春季-使用applicationContext.xml和XXXXX-servlet.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆